1.softmax 公式:
python:
def softmax(X):
exps = np.exp(X)
return exps / np.sum(exps)
对于softmax理解,就是一种平滑的归一化。假如我们使用线性的归一化,就很均匀,softmax相当于给比较大的数值大的权重,比如python里面:
plt.plot(range(1, 20), np.exp(range(1, 20)))
np.exp(20) = 485165195.40979028
越大的数占的得权重就越大。
这样造成一个问题,如果某个数比较大,可以参考https://blog.csdn.net/qq_39575835/article/details/88239982
最大也就 1.7x10^308这么大,所以为了避免溢出,stable softmax 就出来了
2.stable softmax
分子分母同乘一个数,比值不变。一般而言C= max(sample)
python 代码就是
def stable_softmax(X):
exps = np.exp(X - np.max(X))
return exps / np.sum(exps)
化简步骤如下:
我在图中标注的M 是一个常数,大家一般都用 - max(x)来约束。所以stable softmax两行代码就撸完了。
3.cross entropy loss
reference:
https://deepnotes.io/softmax-crossentropy
https://blog.csdn.net/u014380165/article/details/77284921