def _build_loss(self, readout, labels):
"""Build the layer including the loss and the accuracy.
Args:
readout (tensor): The readout layer. A probability distribution over the classes.
labels (tensor): Labels as integers.
Returns:
tensor: The loss tensor (cross entropy).
"""
with tf.name_scope('loss'):
self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=readout, labels=labels))
tf.summary.scalar('cross_entropy', self.loss)
correct_prediction = tf.nn.in_top_k(readout, labels, 1)
self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
tf.summary.scalar('accuracy', self.accuracy)
return self.loss
评论列表
文章目录