def compute_categorical_loss_and_accuracy(logits, targets):
"""return total loss, reg loss (subset of total), and accuracy"""
with tf.variable_scope('loss'):
regularization_losses = sum(
tf.get_collection(
tf.GraphKeys.REGULARIZATION_LOSSES
)
)
loss = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(
logits=logits, labels=targets
),
axis=0,
name='loss'
) + regularization_losses
preds = tf.nn.softmax(logits, name='preds')
correct_preds = tf.equal(
tf.argmax(preds, 1), tf.argmax(targets, 1),
name='correct_preds'
)
accuracy = tf.divide(
tf.reduce_sum(tf.cast(correct_preds, tf.float32)),
tf.cast(tf.shape(targets)[0], tf.float32),
name='accuracy'
)
return loss, regularization_losses, accuracy
评论列表
文章目录