def loss(logits, labels,n_class, scope='loss'):
with tf.variable_scope(scope):
# entropy loss
targets = one_hot_embedding(labels, n_class)
entropy_loss = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(logits, targets),
name='entropy_loss')
tf.add_to_collection('losses', entropy_loss)
# weight l2 decay loss
weight_l2_losses = [tf.nn.l2_loss(o) for o in tf.get_collection('weights')]
weight_decay_loss = tf.mul(FLAGS.weight_decay, tf.add_n(weight_l2_losses),
name='weight_decay_loss')
tf.add_to_collection('losses', weight_decay_loss)
for var in tf.get_collection('losses'):
tf.scalar_summary('losses/' + var.op.name, var)
# total loss
return tf.add_n(tf.get_collection('losses'), name='total_loss')
model_cifar.py 文件源码
python
阅读 25
收藏 0
点赞 0
评论 0
评论列表
文章目录