第一个参数不用管。输入labels时,必须为one-hot形式。
若不是one-hot形式,只用每一次对应的真值,则会自动广播补全,实验如下:
import tensorflow as tf
import numpy as np
#our NN's output
logits=tf.constant([[1.0,2.0,3.0],[1.0,2.0,3.0],[1.0,2.0,3.0]])
#step1:do softmax
y=tf.nn.softmax(logits)
#true label
y_=tf.constant([[0.0,0.0,1.0],[0.0,0.0,1.0],[0.0,0.0,1.0]])
y_2 = np.zeros(3)
for i in range(3):
y_2[i] = 1
y_3 = np.zeros(3)
y_3[2] = 1
#step2:do cross_entropy
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
#do cross_entropy just one step
cross_entropy2=tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(labels=y_2, logits=logits))#dont forget tf.reduce_sum()!!
cross_entropy3=tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(labels=y_3, logits=logits))#dont forget tf.reduce_sum()!!
with tf.Session() as sess:
c_e = sess.run(cross_entropy)
c_e2 = sess.run(cross_entropy2)
c_e3 = sess.run(cross_entropy3)
print("step1 : cross_entropy result=")
print(c_e)
print("step2 : result=")
print(c_e2)
print("step3 : result=")
print(c_e3)
结果如下:
step1 : cross_entropy result=
1.222818
step2 : result=
12.668453
step3 : result=
1.2228179