在Tensorflow中实现对比损失和三重损失 [英] Implementing contrastive loss and triplet loss in Tensorflow

查看:325
本文介绍了在Tensorflow中实现对比损失和三重损失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两天前我开始使用TensorFlow,我想知道是否存在三元组和实现对比损失。

I started to play with TensorFlow two days ago and I'm wondering if there is the triplet and the contrastive losses implemented.

我一直在观察< a href = https://www.tensorflow.org/versions/r0.9/api_docs/python/nn.html#losses rel = noreferrer>文档,但我没有找到任何文档

I've been looking at the documentation, but I haven't found any example or description about these things.

推荐答案

更新(2018/03/19): 博客文章详细说明了如何在TensorFlow中实现三重态损失。

Update (2018/03/19): I wrote a blog post detailing how to implement triplet loss in TensorFlow.

您需要实现对比损失或三重损失,但是一旦知道了配对或三元组,这将非常容易。

You need to implement yourself the contrastive loss or the triplet loss, but once you know the pairs or triplets this is quite easy.

假设您有输入对数据及其标签(正或负,即同一类别或不同类别)。例如,您输入的图像尺寸为28x28x1:

Suppose you have as input the pairs of data and their label (positive or negative, i.e. same class or different class). For instance you have images as input of size 28x28x1:

left = tf.placeholder(tf.float32, [None, 28, 28, 1])
right = tf.placeholder(tf.float32, [None, 28, 28, 1])
label = tf.placeholder(tf.int32, [None, 1]). # 0 if same, 1 if different
margin = 0.2

left_output = model(left)  # shape [None, 128]
right_output = model(right)  # shape [None, 128]

d = tf.reduce_sum(tf.square(left_output - right_output), 1)
d_sqrt = tf.sqrt(d)

loss = label * tf.square(tf.maximum(0., margin - d_sqrt)) + (1 - label) * d

loss = 0.5 * tf.reduce_mean(loss)






三重损失



与对比损失相同,但三胞胎(锚,正,负)。您在这里不需要标签。


Triplet Loss

Same as with contrastive loss, but with triplets (anchor, positive, negative). You don't need labels here.

anchor_output = ...  # shape [None, 128]
positive_output = ...  # shape [None, 128]
negative_output = ...  # shape [None, 128]

d_pos = tf.reduce_sum(tf.square(anchor_output - positive_output), 1)
d_neg = tf.reduce_sum(tf.square(anchor_output - negative_output), 1)

loss = tf.maximum(0., margin + d_pos - d_neg)
loss = tf.reduce_mean(loss)






真正的麻烦是在TensorFlow中实施三元组损失或对比损失是如何对三元组或三元组进行采样。我将重点介绍生成三元组,因为它比生成对更困难。


The real trouble when implementing triplet loss or contrastive loss in TensorFlow is how to sample the triplets or pairs. I will focus on generating triplets because it is harder than generating pairs.

最简单的方法是在Tensorflow图之外(即在python中)生成它们,然后通过占位符将其馈送到网络。基本上,您一次选择3张图像,前两张来自同一类别,第三张来自另一类别。然后,我们对这些三胞胎执行前馈,并计算三重态损失。

The easiest way is to generate them outside of the Tensorflow graph, i.e. in python and feed them to the network through the placeholders. Basically you select images 3 at a time, with the first two from the same class and the third from another class. We then perform a feedforward on these triplets, and compute the triplet loss.

这里的问题是生成三元组很复杂。我们希望它们成为有效的三元组,三元组具有正的损耗(否则损耗为0,并且网络无法学习)。

要知道三元组是好的还是三元组不需要计算损失,因此您已经通过网络进行了前馈...

The issue here is that generating triplets is complicated. We want them to be valid triplets, triplets with a positive loss (otherwise the loss is 0 and the network doesn't learn).
To know whether a triplet is good or not you need to compute its loss, so you already make one feedforward through the network...

很显然,在Tensorflow中实现三重损失非常困难,并且有多种方法可以使其比使用python采样更为有效,但要说明它们,则需要整个博客文章!

Clearly, implementing triplet loss in Tensorflow is hard, and there are ways to make it more efficient than sampling in python but explaining them would require a whole blog post !

这篇关于在Tensorflow中实现对比损失和三重损失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆