tensorflow 的 softmax

对于tensorflow中的一些softmax的一些总结

softmax的简单说明:
[x1,x2,x3]softmax[ex1,ex2,ex3]ex1+ex2+ex3 [ x 1 , x 2 , x 3 ] ⟶ s o f t m a x ⟶ [ e x 1 , e x 2 , e x 3 ] e x 1 + e x 2 + e x 3

(一)单纯的softmax

tf.nn.softmax():softmax计算

a  = tf.constant([0.1, 0.2, 0.3],dtype=tf.float32)
b = tf.nn.softmax(a)
with tf.Session() as sess:
    print(sess.run(b))
    print(sess.run(tf.reduce_sum(b)))
"""
[ 0.30060959  0.33222499  0.36716539]
1.0
"""

等价于以下numpy实现

import numpy as np
aa = np.array([0.1,0.2,0.3])
c = np.exp(aa) / np.sum(np.exp(aa), 0)
print(c)
"""
[ 0.30060961  0.33222499  0.3671654 ]
"""

(二)多类别softmax with交叉熵

tf.nn.softmax_cross_entropy_with_logits_v2: softmax + 多类别cross-entropy
注意多类别交叉熵的公式(Multi-class cross-entropy),这里有个很好的解释说明。

Hy(y)=iylog(y) H y ′ ( y ) = ∑ i y ′ l o g ( y )
其中 y y ′ 表示groundtruth, y y <script type="math/tex" id="MathJax-Element-4">y</script>表示预测得到的概率(常见的也就是softmax的输出)

a  = tf.constant([[0.1, 0.2, 0.3]],dtype=tf.float32)
label = tf.constant([[0, 0, 1]],dtype=tf.float32)
b = tf.nn.softmax_cross_entropy_with_logits_v2(logits=a, labels=label)

with tf.Session() as sess:
    print(sess.run(b))
    print(sess.run(tf.reduce_sum(b)))
"""
[ 1.00194287]
1.00194
"""

等价于以下numpy实现: softmax(predictions) + cross_entropy

import numpy as np
def softmax(logits):
    return np.exp(logits)/np.sum(np.exp(logits))

def cross_entropy(label, prediction_softmax):
    result = np.sum(-1*np.log(prediction_softmax)*label)
    return result

prediction = np.array([[0.1, 0.2, 0.3]], dtype=np.float32)
label = np.array([[0, 0, 1]],dtype=np.float32)
c = cross_entropy(label, softmax(prediction))
print(c)
"""
1.00194
"""
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值