Difference between softmax_cross_entropy_with_logits and softmax_cross_entropy_with_logits_v2
nn.softmax 和 softmax_cross_entropy_with_logits 和 softmax_cross_entropy_with_logits_v2 的区别
You have every reason to be confused, because in supervised learning one doesn’t need to backpropagate to labels. They are considered fixed ground truth and only the weights need to be adjusted to match them.
But in some cases, the labels themselves may come from a differentiable source, another network. One example might be adversarial learning. In this case, both networks might benefit from the error signal. That’s the reason why tf.nn.softmax_cross_entropy_with_logits_v2
was introduced. Note that when the labels are the placeholders (which is also typical), there is no difference if the gradient through flows or not, because there are no variables to apply gradient to.
import tensorflow as tf import numpy as np Truth = np.array([0,0,1,0]) Pred_logits = np.array([3.5,2.1,7.89,4.4]) loss = tf.nn.softmax_cross_entropy_with_logits(labels=Truth,logits=Pred_logits) loss2 = tf.nn.softmax_cross_entropy_with_logits_v2(labels=Truth,logits=Pred_logits) loss3 = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=tf.argmax(Truth),logits=Pred_logits) with tf.Session() as sess: print(sess.run(loss)) print(sess.run(loss2)) print(sess.run(loss3))