Softmax后的数据分布

在读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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值