我正在使用tensorflow和Im做一个神经网络,使用softmax_cross_entropy来计算损失,我正在做测试,并注意到它永远不会给出零值,即使我比较相同的值,这也是我的代码
labels=[1,0,1,1]
with tf.Session() as sess:
onehot_labels=tf.one_hot(indices=labels,depth=2)
logits=[[0.,1.],[1.,0.],[0.,1.],[0.,1.]]
print(sess.run(onehot_labels))
loss=tf.losses.softmax_cross_entropy(onehot_labels=onehot_labels,logits=logits)
print(sess.run(loss))
我知道了
[[0. 1.]
[1. 0.]
[0. 1.]
[0. 1.]]
0.31326166
为什么不为零?
答案 0 :(得分:0)
Matias的帖子是正确的。以下代码与您的代码具有相同的结果
labels=[1,0,1,1]
with tf.Session() as sess:
onehot_labels=tf.one_hot(indices=labels,depth=2)
logits=[[0.,1.],[1.,0.],[0.,1.],[0.,1.]]
print(sess.run(onehot_labels))
probabilities = tf.nn.softmax(logits=logits)
# cross entropy
loss = -tf.reduce_sum(onehot_labels * tf.log(probabilities)) / len(labels)
print(sess.run(loss))