nn.Softmax
pytorch
本文字数:257 字 | 阅读时长 ≈ 1 min

nn.Softmax

pytorch
本文字数:257 字 | 阅读时长 ≈ 1 min

nn.Softmax(dim=None)

用 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]])'''
4月 06, 2025
3月 10, 2025
12月 31, 2024