很多代码里面求交叉熵的时候就直接一行代码展示,其中的细节没有详细展示出来。tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=y_, labels=y))
其实这句代码应该是拆分成几个部分的:
tf.nn.softmax_cross_entropy_with_logits_v2
完成。tf.reduce_mean
,对上一步中得到的数组求均值,并降维,即(batch_size, ) − − > --> −−> (1, )代码如下:
import tensorflow as tf
# calculate cross_entropy
y = tf.constant([[1.0, 2.0, 3.0, 4.0], [1.0, 2.0, 3.0, 4.0], [1.0, 2.0, 3.0, 4.0]])
y_ = tf.constant([[0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 0.0, 1.0]])
ysoft = tf.nn.softmax(y)
a = y_ * tf.log(ysoft)
cross_entropy1 = -tf.reduce_sum(y_ * tf.log(ysoft))
# do cross_entropy just one step
cross_entropy2 = tf.nn.softmax_cross_entropy_with_logits_v2(logits=y, labels=y_)
cross_entropy3 = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits_v2(logits=y, labels=y_))
cross_entropy_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=y, labels=y_))
with tf.Session() as sess:
print("step1:softmax result=")
print(sess.run(ysoft))
print("\na=")
print(sess.run(a))
print('a.shape is :', a.shape)
print('a.rank is :', sess.run(tf.rank(a)))
print("\nstep2:cross_entropy1 result=")
print(sess.run(cross_entropy1))
print("\nFunction(softmax_cross_entropy_with_logits) result=")
print(sess.run(cross_entropy2))
print('cs.shape is :', cross_entropy2.shape)
print('cs.rank is :', sess.run(tf.rank(cross_entropy2)))
print("\nFunction(reduce_sum) result=")
print(sess.run(cross_entropy3))
print("\nFunction(reduce_mean) result=")
print(sess.run(cross_entropy_loss))
运行的结果如下:
step1:softmax result=
[[0.0320586 0.08714432 0.23688284 0.6439143 ]
[0.0320586 0.08714432 0.23688284 0.6439143 ]
[0.0320586 0.08714432 0.23688284 0.6439143 ]]
a=
[[-0. -0. -0. -0.44018966]
[-0. -0. -0. -0.44018966]
[-0. -0. -0. -0.44018966]]
a.shape is : (3, 4)
a.rank is : 2
step2:cross_entropy1 result=
1.320569
Function(softmax_cross_entropy_with_logits) result=
[0.4401897 0.4401897 0.4401897]
cs.shape is : (3,)
cs.rank is : 1
Function(reduce_sum) result=
1.320569
Function(reduce_mean) result=
0.4401897