PyTorch documentation
https://pytorch.org/docs/stable/index.html
0. softmax - torch.nn - torch.nn.functional
-
torch.nn.functional.softmax (Python function, intorch.nn.functional )
定义的是一个函数。torch.nn.functional 中定义的是函数,由def function( ) 定义,是一个固定的运算公式。 -
torch.nn.Softmax (Python class, intorch.nn )
定义的是一个类。torch.nn 中定义的是类,以class xx 来定义的,可以提取变化的学习参数。
train 和 test 阶段运行方法一致时,尽量用
深度学习中权重需要不断更新,需要采用类的方式,以确保能在参数发生变化时仍能使用之前定好的运算步骤。如果模型有可学习的参数,应该使用
| torch.nn.name | torch.nn.functional.name |
|---|---|
| 类 | 函数 |
| 结构中包含所需要初始化的参数 | 在函数外定义并初始化相应参数,并作为参数传入 |
| 在 |
在 |
1. softmax - torch.nn.functional.softmax (Python function, in torch.nn.functional )
https://pytorch.org/docs/stable/nn.functional.html
Applies a
应用
Softmax is defined as:
Softmax(xi?)=∑j?exp(xj?)exp(xi?)?
It is applied to all slices along dim, and will re-scale them so that the elements lie in the range
它应用于
See
https://pytorch.org/docs/stable/nn.html#torch.nn.Softmax
1.1 Parameters
返回
This function doesn’t work directly with
此函数不能直接与
2. softmax - torch.nn.Softmax (Python class, in torch.nn )
https://pytorch.org/docs/stable/nn.html
Applies the
将
Softmax is defined as:
Softmax(xi?)=∑j?exp(xj?)exp(xi?)?
2.1 Shape
Input: (
Output: (
2.2 Returns
a Tensor of the same dimension and shape as the input with values in the range
2.3 Parameters
用来计算 Softmax 的尺寸 (因此,沿
This module doesn’t work directly with
3. Examples

3.1 example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | (pt-1.4_py-3.6) yongqiang@yongqiang:~$ python Python 3.6.10 |Anaconda, Inc.| (default, May 8 2020, 02:54:21) [GCC 7.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import torch >>> import torch.nn >>> >>> obj = torch.nn.Softmax(dim=1) >>> input = torch.randn(2, 3) >>> input tensor([[ 0.0414, -0.7946, 1.5696], [ 0.2665, -0.2709, -2.0720]]) >>> >>> output = obj(input) >>> output tensor([[0.1655, 0.0717, 0.7628], [0.5950, 0.3476, 0.0574]]) >>> >>> exit() (pt-1.4_py-3.6) yongqiang@yongqiang:~$ |
3.2 example
执行算子后,在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | (pt-1.4_py-3.6) yongqiang@yongqiang:~$ python Python 3.6.10 |Anaconda, Inc.| (default, May 8 2020, 02:54:21) [GCC 7.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import torch >>> import torch.nn.functional as F >>> >>> input = torch.randn(2, 3, 4) >>> input tensor([[[-0.3976, 0.4142, -0.5061, -0.4063], [-0.2401, -1.5699, 1.4867, -1.9940], [ 0.5525, -1.4140, -1.2408, -0.6638]], [[-1.7829, 0.1077, 0.8127, -2.8241], [ 1.4750, 0.5804, 1.1887, -0.6570], [ 0.2279, 0.9583, -1.9489, -0.5876]]]) >>> >>> output_0 = F.softmax(input, dim=0) >>> output_0 tensor([[[0.7999, 0.5760, 0.2110, 0.9182], [0.1525, 0.1043, 0.5740, 0.2080], [0.5805, 0.0853, 0.6700, 0.4810]], [[0.2001, 0.4240, 0.7890, 0.0818], [0.8475, 0.8957, 0.4260, 0.7920], [0.4195, 0.9147, 0.3300, 0.5190]]]) >>> >>> output_1 = F.softmax(input, dim=1) >>> output_1 tensor([[[0.2102, 0.7703, 0.1134, 0.5057], [0.2461, 0.1059, 0.8322, 0.1034], [0.5437, 0.1238, 0.0544, 0.3909]], [[0.0290, 0.2022, 0.3969, 0.0524], [0.7543, 0.3244, 0.5780, 0.4574], [0.2167, 0.4734, 0.0251, 0.4903]]]) >>> >>> output_2 = F.softmax(input, dim=2) >>> output_2 tensor([[[0.1945, 0.4381, 0.1745, 0.1928], [0.1416, 0.0375, 0.7964, 0.0245], [0.6239, 0.0873, 0.1038, 0.1849]], [[0.0468, 0.3098, 0.6269, 0.0165], [0.4389, 0.1794, 0.3296, 0.0521], [0.2753, 0.5716, 0.0312, 0.1218]]]) >>> >>> exit() (pt-1.4_py-3.6) yongqiang@yongqiang:~$ |
3.3 example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | (pt-1.4_py-3.6) yongqiang@yongqiang:~$ python Python 3.6.10 |Anaconda, Inc.| (default, May 8 2020, 02:54:21) [GCC 7.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import torch >>> import torch.nn.functional as F >>> >>> input = torch.randn(2, 3) >>> input tensor([[ 0.6812, 0.7187, -2.9340], [-0.0287, 0.5622, -0.7260]]) >>> >>> output_0 = F.softmax(input, dim=0) # 每一列执行 softmax 运算 >>> output_0 tensor([[0.6704, 0.5390, 0.0990], [0.3296, 0.4610, 0.9010]]) >>> >>> output_1 = F.softmax(input, dim=1) # 每一行执行 softmax 运算 >>> output_1 tensor([[0.4842, 0.5027, 0.0130], [0.3027, 0.5466, 0.1507]]) >>> >>> exit() (pt-1.4_py-3.6) yongqiang@yongqiang:~$ |
3.4 example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | (pt-1.4_py-3.6) yongqiang@yongqiang:~$ python Python 3.6.10 |Anaconda, Inc.| (default, May 8 2020, 02:54:21) [GCC 7.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import torch >>> import torch.nn.functional as F >>> >>> input = torch.tensor([[1, 2], [3, 4]], dtype=torch.float) >>> input tensor([[1., 2.], [3., 4.]]) >>> >>> output_0 = F.softmax(input, dim=0) >>> output_0 tensor([[0.1192, 0.1192], [0.8808, 0.8808]]) >>> >>> output_1 = F.softmax(input, dim=1) >>> output_1 tensor([[0.2689, 0.7311], [0.2689, 0.7311]]) >>> >>> input = torch.tensor([[1, 2], [3, 6]], dtype=torch.float) >>> input tensor([[1., 2.], [3., 6.]]) >>> >>> output_0 = F.softmax(input, dim=0) >>> output_0 tensor([[0.1192, 0.0180], [0.8808, 0.9820]]) >>> >>> output_1 = F.softmax(input, dim=1) >>> output_1 tensor([[0.2689, 0.7311], [0.0474, 0.9526]]) >>> >>> exit() (pt-1.4_py-3.6) yongqiang@yongqiang:~$ |