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(x_{i}) = \frac{e{x_{i}}}{\sum_{j}e{x_{j}}}
$$
实例:从下面的例子可以看出,Softmax
维度为0时对最后一维即列进行归一化,因此x00+x10+x20=1,维度为 1 时对行进行归一化
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]])'''
本文由 Yonghui Wang 创作,采用
知识共享署名4.0
国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为:
Dec 19, 2024 12:13 pm