基于numy实现softmax功能

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hzhj2007/article/details/89205562

本文利用numpy模块实现softmax的功能。

    import numpy as np

    def softmax_np(logits):
        assert (isinstance(logits, np.ndarray)), 'only numpy is available'
        exp_value = np.exp(logits)  # 计算指数值
        dim_ext = np.sum(exp_value, 1).reshape(-1, 1)
        return exp_value / dim_ext

    x_val = [[1, 2, 3], [3, 2, 2]]
    logits = np.array(x_val)
    s_v = softmax_np(logits)
    print s_v, np.sum(s_v, 1)

猜你喜欢

转载自blog.csdn.net/hzhj2007/article/details/89205562