tensorflow 的 softmax

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

softmax的简单说明:
[ 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),这里有个很好的解释说明。

H y ( y ) = i y l o g ( y )
其中 y 表示groundtruth, y 表示预测得到的概率(常见的也就是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
"""

猜你喜欢

转载自blog.csdn.net/qq_29007291/article/details/81111322