相关疑难解决方法(0)

如何在Python中实现Softmax函数

Udacity的深度学习类中,y_i的softmax只是指数除以整个Y向量的指数之和:

在此输入图像描述

哪里S(y_i)是的SOFTMAX功能y_i,并e为指数和j是否定的.输入向量Y中的列数.

我尝试过以下方法:

import numpy as np

def softmax(x):
    """Compute softmax values for each sets of scores in x."""
    e_x = np.exp(x - np.max(x))
    return e_x / e_x.sum()

scores = [3.0, 1.0, 0.2]
print(softmax(scores))
Run Code Online (Sandbox Code Playgroud)

返回:

[ 0.8360188   0.11314284  0.05083836]
Run Code Online (Sandbox Code Playgroud)

但建议的解决方案是:

def softmax(x):
    """Compute softmax values for each sets of scores in x."""
    return np.exp(x) / np.sum(np.exp(x), axis=0)
Run Code Online (Sandbox Code Playgroud)

它产生与第一个实现相同的输出,即使第一个实现显式获取每列和最大值的差异,然后除以总和.

有人可以用数学方式显示原因吗?一个是正​​确的而另一个是错的吗?

实现在代码和时间复杂性方面是否相似?哪个更有效率?

python numpy machine-learning logistic-regression softmax

219
推荐指数
10
解决办法
19万
查看次数