numpy 实现 softmax

def col_softmax(x):
    x = np.asarray(x)
    x_col_max = x.max(axis=0)
    x_col_max = x_col_max.reshape([1,x.shape[1]])
    x = x - x_col_max  # 防止数值上溢
    x_exp = np.exp(x)
    x_exp_col_sum = x_exp.sum(axis=0).reshape([1,x.shape[1]])
    softmax = x_exp / x_exp_col_sum
    return softmax

测试

a = [[i] for i in range(20)]
col_softmax(a)
'''
array([[3.54164282e-09],
       [9.62718333e-09],
       [2.61693975e-08],
       [7.11357977e-08],
       [1.93367146e-07],
       [5.25626400e-07],
       [1.42880069e-06],
       [3.88388295e-06],
       [1.05574885e-05],
       [2.86982290e-05],
       [7.80098745e-05],
       [2.12052824e-04],
       [5.76419339e-04],
       [1.56687021e-03],
       [4.25919483e-03],
       [1.15776919e-02],
       [3.14714295e-02],
       [8.55482150e-02],
       [2.32544158e-01],
       [6.32120560e-01]])
'''
原创文章 338 获赞 621 访问量 50万+

猜你喜欢

转载自blog.csdn.net/itnerd/article/details/105014868
今日推荐