def contrastive_loss(labels, logits, margin_gen=0, margin_imp=1, scope=None):
"""With this definition the loss will be calculated.
Args:
y: The labels.
distance: The distance vector between the output features..
batch_size: the batch size is necessary because the loss calculation would be over each batch.
Returns:
The total loss.
"""
with ops.name_scope(scope, "contrastive_loss", [labels, logits]) as scope:
# logits.get_shape().assert_is_compatible_with(onehot_labels.get_shape())
labels = math_ops.cast(labels, logits.dtype)
# term_1 = tf.multiply(labels, tf.square(logits))
term_1 = tf.multiply(labels, tf.square(tf.maximum((logits - margin_gen), 0)))
term_2 = tf.multiply(1 - labels, tf.square(tf.maximum((margin_imp - logits), 0)))
# Contrastive
Contrastive_Loss = tf.add(term_1, term_2) / 2
loss = tf.losses.compute_weighted_loss(Contrastive_Loss, scope=scope)
return loss
losses.py 文件源码
python
阅读 22
收藏 0
点赞 0
评论 0
评论列表
文章目录