对于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),这里有个很好的解释说明。
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
"""