def softmax(x):
if x.ndim == 2:
x = x.T
x = x - np.max(x, axis=0) #找出每一列最大的数
y = np.exp(x) / np.sum(np.exp(x), axis=0)
return y.T
x = x - np.max(x) # 溢出对策
return np.exp(x) / np.sum(np.exp(x))
在二维的情况下,np.max(x,axis = 0)是取出每列的最大值
np.sum(np.exp(x), axis = 0)是将每一列所有元素相加