0. 引言
假设最后的 logit 为:[15664, 15432, 14965]
,对于这样的数据应该如何处理呢?
1. 直接进行 Softmax
>>> import torch
>>> a = torch.FloatTensor(3, )
>>> a[0], a[1], a[2] = 15664, 15432, 14965
>>> torch.nn.functional.softmax(a)
tensor([1., 0., 0.])
很明显,这样的结果并不是我们想要的。
2. 方法 1:先进行最大最小归一化再进行 Softmax
>>> import torch
>>> a = torch.FloatTensor(3, )
>>> a[0], a[1], a[2] = 15664, 15432, 14965
>>> logit = (a - torch.min(a)) / (torch.max(a) - torch.min(a))
>>> torch.nn.functional.softmax(logit)
tensor([0.4795, 0.3441, 0.1764])
3. 🥳方法 2:先进行 z-score 再进行 Softmax
>>> import torch
>>> a = torch.FloatTensor(3, )
>>> a[0], a[1], a[2] = 15664, 15432, 14965
>>> z_score = (a - a.mean()) / a.std()
>>> z_score
tensor([ 0.8717, 0.2200, -1.0917])
>>> torch.nn.functional.softmax(z_score)
tensor([0.6018, 0.3137, 0.0845])
结果很完美!