对于二维数组的dim=0时,将每列的每个元素和这一列的自然指数之和进行比,
import torch.nn as nn
import numpy as np
import math
arr = np.linspace(1,16,16).reshape(4,4)
arr = torch.tensor(arr)
sofmax = nn.Softmax(dim=0)
out = sofmax(arr)
print(arr)
print(out)
son = math.exp(1)
pat = 0
for i in range(1,14,4):
pat+=math.exp(i)
print(son/pat)
'''
tensor([[ 1., 2., 3., 4.],
[ 5., 6., 7., 8.],
[ 9., 10., 11., 12.],
[13., 14., 15., 16.]], dtype=torch.float64)
tensor([[6.0317e-06, 6.0317e-06, 6.0317e-06, 6.0317e-06],
[3.2932e-04, 3.2932e-04, 3.2932e-04, 3.2932e-04],
[1.7980e-02, 1.7980e-02, 1.7980e-02, 1.7980e-02],
[9.8168e-01, 9.8168e-01, 9.8168e-01, 9.8168e-01]], dtype=torch.float64)
6.031677857384872e-06
'''
只是第一列发生变化,
arr = np.linspace(1,16,16).reshape(4,4)
arr = np.array([[ 20., 2., 3., 4.],
[ 5., 6., 7., 8.],
[ 9., 10., 11., 12.],
[13., 14., 15., 16.]])
arr = torch.tensor(arr)
import torch.nn as nn
import numpy as np
import math
arr = np.linspace(1,16,16).reshape(4,4)
arr = np.array([[ 1., 2., 3., 4.],
[ 5., 6., 7., 8.],
[ 9., 10., 11., 12.],
[13., 14., 15., 16.]])
arr = torch.tensor(arr)
sofmax = nn.Softmax(dim=1)
out = sofmax(arr)
print(arr)
print(out)
son = math.exp(1)
pat = 0
for i in range(1,5):
pat+=math.exp(i)
print(son/pat)