x1def softmax1(x):
2 '''
3 原始softmax
4 '''
5 f_x = np.exp(x) / np.sum(np.exp(x))
6 return f_x
7
8
9def softmax2(x):
10 '''
11 优化版softmax
12 '''
13 y = np.exp(x - np.max(x))
14 f_x = y / np.sum(y)
15 return f_x
16
17softmax2([1, 20, 300000])