在读SampleNet网络的时候,论文中提到当数据都很小(非常大的负数)时,经过Softmax后,会倾向于出现[0, 1]的值,比较疑惑,就进行了实验,发现的确如此。
import numpy as np
import torch
import torch.nn as nn
def pprint(x, y, desc):
print('='*10, desc, '='*10)
print(f'input: {np.round(x.numpy(), 2)}')
print(f'softmax: {y}')
softmax = nn.Softmax(dim=0)
x = torch.rand(5, ) * (-10e10)
y = softmax(x).numpy()
pprint(x, y, '很大的负数')
softmax = nn.Softmax(dim=0)
x = torch.rand(5, ) * (10e10)
y = softmax(x).numpy()
pprint(x, y, '很大的正数')
softmax = nn.Softmax(dim=0)
x = torch.rand(5, )
y = softmax(x).numpy()
pprint(x, y, '0 - 1之间的数')
softmax = nn.Softmax(dim=0)
x = torch.randn(5, )
y = softmax(x).numpy()
pprint(x, y, '-1 - 1之间的数')
softmax = nn.Softmax(dim=0)
x = torch.rand(5, )
x[:3] *= 10e10
y = softmax(x).numpy()
pprint(x, y, '存在大数')
softmax = nn.Softmax(dim=0)
x = torch.rand(5, )
x[:3] *= -10e10
y = softmax(x).numpy()
pprint(x, y, '存在小数')
输出结果如下:
========== 很大的负数 ==========
input: [-8.4782838e+09 -7.4396221e+10 -6.7901854e+10 -1.7147696e+10
-7.8934852e+10]
softmax: [1. 0. 0. 0. 0.]
========== 很大的正数 ==========
input: [4.7449051e+10 7.1184703e+10 7.3997058e+10 2.9726134e+10 5.7239773e+10]
softmax: [0. 0. 1. 0. 0.]
========== 0 - 1之间的数 ==========
input: [0.55 0.52 0.76 0.2 0.57]
softmax: [0.20201461 0.19699681 0.24963409 0.14366919 0.20768534]
========== -1 - 1之间的数 ==========
input: [-0.08 0.69 0.87 0.37 0.52]
softmax: [0.10958216 0.23727055 0.2818025 0.17234579 0.19899899]
========== 存在大数 ==========
input: [7.7410066e+10 6.6511491e+10 4.9623142e+10 8.9999998e-01 1.7000000e-01]
softmax: [1. 0. 0. 0. 0.]
========== 存在小数 ==========
input: [-2.4538337e+10 -4.8202502e+10 -5.1050471e+10 3.8000000e-01
1.1000000e-01]
softmax: [0. 0. 0. 0.5664337 0.4335663]
5230

被折叠的 条评论
为什么被折叠?



