losses.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:tefla 作者: openAGI 项目源码 文件源码
def triplet_loss(anchor, positive, negative, alpha=0.2, name='triplet_loss'):
    """Calculate the triplet loss according to the FaceNet paper

    Args:
      anchor: 2-D `tensor` [batch_size, embedding_size], the embeddings for the anchor images.
      positive: 2-D `tensor` [batch_size, embedding_size], the embeddings for the positive images.
      negative: 2-D `tensor` [batch_size, embedding_size], the embeddings for the negative images.
      alpha: positive to negative triplet distance margin

    Returns:
      the triplet loss.
    """
    with tf.name_scope(name):
        pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), 1)
        neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)), 1)
        basic_loss = tf.add(tf.subtract(pos_dist, neg_dist), alpha)
        loss = tf.reduce_mean(tf.maximum(basic_loss, 0.0), 0)
    return loss
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号