Softmax 不会在 Python 实现中产生概率分布

Softmax not resulting in a probability distribution in Python Implementation

我有一个简单的 softmax 实现:

softmax = np.exp(x) / np.sum(np.exp(x), axis=0)

此处 x 设置为数组:https://justpaste.it/6wis7

您可以将其加载为:

 import numpy as np

 x = np.as (just copy and paste the content (starting from array))

我得到:

softmax.mean(axis=0).shape 
(100,) # now all elements must be 1.0 here, since its a probability

softmax.mean(axis=0) # all elements are not 1

array([0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158,
       0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158,
       0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158,
       0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158,
       0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158,
       0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158,
       0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158,
       0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158,
       0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158,
       0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158,
       0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158,
       0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158,
       0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158,
       0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158,
       0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158,
       0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158,
       0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158,
       0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158,
       0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158,
       0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158])

为什么这个实现是错误的?如何解决?

我觉得不错:

import numpy as np

def softmax(x):
    return np.exp(x) / np.sum(np.exp(x), axis=0)

logits = softmax(np.random.rand(4))

print(logits)

softmax 动作的所有元素的总和应该等于 1。

对于class化任务,通常采用具有最高值(np.argmax())或最高n个索引的索引,并选择那些最有可能class(es ):

class_index = np.argmax(logits)  # Assuming logits is the output of a trained model

print('Most likely class: %d' % class_index)

正如JosepJoestar在评论中所指出的,可以找到softmax函数的定义here

概率总和必须是1,不是平均数。让我们用这个简单的例子让它更清楚。想象一下 3 softmax 个输出值 s = [0.5, 0.25, 0.25]。显然他们要总结1(概率)。但是他们的平均值是 0.333.

>>> softmax.sum(axis=0)
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
       1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
       1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
       1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
       1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
       1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])

我希望这个例子能说明这一点!