Triplet Loss 实现

本文探讨了Triplet Loss的实现方法,对比了两种不同的策略:一种是先进行前馈操作再生成三元组,另一种是在单次CNN操作中直接计算损失。作者提到了一种巧妙的实现方式,通过Tensorflow直接计算所有三元组的损失,并利用reduce_max/reduce_min等操作简化了代码,避免了if判断。参考代码来自https://github.com/omoindrot/tensorflow-triplet-loss。
摘要由CSDN通过智能技术生成

按照我原来的想法,Triplet Loss 三元组应该是这样选择的:

(1) 前馈操作: cnn 先执行forward 操作,获取到embedding 的具体值后,再去用非 tf 函数去处理embedding并生成三元组

(2) 训练操作: 用(1) 生成的三元组进行训练

 

今天看了下别人的实现代码,直接用Tensorflow 在一次cnn操作中,直接选取并获取最终的loss值。(而我想象的那样,不是一次前馈+一次训练) 

很多很巧妙的思路(如果让我写,我想不出来的那种):

(1) 获取所有三元组的loss,已知d[i,j] 是第i个和第j embedding 的距离,dx[i,j,1] = d[i,1,k] = d[i,j] 

     通过以下一句就可以实现获取所有三元组的loss值

#triplet_loss[i,j,k] = dx[i,j,1] - dy[i,1,k] +margin = d[i,j] - d[i,k] +margin
triplet_loss = anchor_positive_dist - anchor_negative_dist + margin

(2) 通过reduce_max / reduce_min 等操作,省略选取三元组中的各种if 操作

 

代码来源:https://github.com/omoindrot/tensorflow-triplet-loss/blob/master/model/triplet_loss.py

以下代码加了我的注释:

"""Define functions to create the triplet loss with online triplet mining."""

import tensorflow as tf

# (*) 返回embeddings 两两之间的距离
def _pairwise_distances(embeddings, squared=False):
    """Compute the 2D matrix of distances between all the embeddings.

    Args:
        embeddings: tensor of shape (batch_size, embed_dim)
        squared: Boolean. If true, output is the pairwise squared euclidean distance matrix.
                 If false, output is the pairwise euclidean distance matrix.

    Returns:
        pairwise_distances: tensor of shape (batch_size, batch_size)
    """
    # Get the dot product between all embeddings
    # shape (batch_size, batch_size)
    dot_product = tf.matmul(embeddings, tf.transpose(embeddings))

    # Get squared L2 norm for each embedding. We can just take the diagonal of `dot_product`.
    # This also provides more numerical stability (the diagonal of the result will be exactly 0).
    # shape (batch_size,)
    square_norm = tf.diag_part(dot_product)

    # Compute the pairwise distance matrix as we have:
    # ||a - b||^2 = ||a||^2  - 2 <a, b> + ||b||^2
    # shape (batch_size, batch_size)
    distances = tf.expand_dims
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值