下面是我尝试计算 softmax 的一小段代码。它适用于单个阵列。但是对于更大的数字,比如 1000 等,它会爆炸
import numpy as np
def softmax(x):
print (x.shape)
softmax1 = np.exp(x)/np.sum(np.exp(x))
return softmax1
def test_softmax():
print "Running your code"
#print softmax(np.array([1,2]))
test1 = softmax(np.array([1,2]))
ans1 = np.array([0.26894142, 0.73105858])
assert np.allclose(test1, ans1, rtol=1e-05, atol=1e-06)
print ("Softmax values %s" % test1)
test2 = softmax(np.array([[1001,1002],[3,4]]))
print test2
ans2 = np.array([
[0.26894142, 0.73105858],
[0.26894142, 0.73105858]])
assert np.allclose(test2, ans2, rtol=1e-05, atol=1e-06)
if __name__ == "__main__":
test_softmax()
我得到一个错误 RuntimeWarning: exp 遇到溢出 运行你的代码 softmax1 = np.exp(x)/np.sum(np.exp(x))
最佳答案
softmax的典型实现是先取最大值来解决这个问题:
def softmax(x, axis=-1):
# save typing...
kw = dict(axis=axis, keepdims=True)
# make every value 0 or below, as exp(0) won't overflow
xrel = x - x.max(**kw)
# if you wanted better handling of small exponents, you could do something like this
# to try and make the values as large as possible without overflowing, The 0.9
# is a fudge factor to try and ignore rounding errors
#
# xrel += np.log(np.finfo(float).max / x.shape[axis]) * 0.9
exp_xrel = np.exp(xrel)
return exp_xrel / exp_xrel.sum(**kw)
在代数上,这完全相同,但这确保了传递给 exp
的最大值是 1
。
关于python - 大量错误的 Softmax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43401593/