nn.Softmax

nn.Softmax(dim=None)

  • dim: 计算的维度 A dimension along which Softmax will be computed (so every slice along dim will sum to 1).

用softmax函数将N维输入进行归一化,归一化之后每个输出的Tensor范围在[0, 1],并且归一化的那一维和为1
Applies the Softmax function to an n-dimensional input Tensor rescaling them so that the elements of the n-dimensional output Tensor lie in the range [0,1] and sum to 1.

实例:从下面的例子可以看出,Softmax维度为0时对最后一维即列进行归一化,因此,维度为1时对行进行归一化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
input = torch.Tensor([[1,2,3], [4,5,6], [7,8,9]])
m0 = nn.Softmax(dim=0)
m1 = nn.Softmax(dim=1)
output0 = m0(input)
output1 = m1(input)

print("input: ", input)
print("output0: ", output0)
print("output1: ", output1)
'''
input: tensor([[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]])
output0: tensor([[0.0024, 0.0024, 0.0024],
[0.0473, 0.0473, 0.0473],
[0.9503, 0.9503, 0.9503]])
output1: tensor([[0.0900, 0.2447, 0.6652],
[0.0900, 0.2447, 0.6652],
[0.0900, 0.2447, 0.6652]])'''
Error: API rate limit exceeded for 44.193.128.39. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)