以下はソフトコードを計算しようとしている小さなコードです。 1つのアレイでうまく動作します。しかし、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を取得吹く:オーバーフローがあなたのコード softmax1 = np.exp(x)の/np.sum(NPを実行EXP に遭遇しました。 EXP(X))ソフトマックスの
http://stackoverflow.com/questions/34968722/softmax-function-pythonとhttp://stackoverflow.com/questions/42599498/numerce-stable-softmax –