使用TensorFlow进行成对距离计算

14

我试图实现这篇文章: http://ronan.collobert.com/pub/matos/2008_deep_icml.pdf,具体来说是第2节中的方程式(3)。

简而言之,我想要对每个小批次的特征进行成对距离计算,并将此损失插入到总网络损失中。我只有小批次(16个样本)的张量、标签张量和小批次特征张量。

经过一段时间的寻找,我仍然无法理解以下问题:

1)如何将批次划分为正面(即相同标签)和负面对。由于Tensor不可迭代,我无法弄清楚如何获得哪个样本具有哪个标签,然后划分我的向量,或者获取张量的哪些索引属于每个类。

2)如何对批量张量中的某些索引进行两两距离计算?

3)我还需要为负样例定义一个新的距离函数。

总之,我需要找出哪些索引属于哪个类,对所有正面对进行正面对距离计算。同时,还需要对所有负面对进行另一种距离计算。然后将它们加起来,并添加到网络损失中。

如果能够解决以上三个问题中的一个或多个,将不胜感激。


如果您的距离度量是欧几里得距离,您可以使用tf.gather创建一个矩阵,其中只包含感兴趣的向量,然后有一种有效的方法来计算所有成对距离,就像这里中所示。 - Yaroslav Bulatov
2个回答

14

1) 在将数据输入会话之前,应该对成对数据进行配对采样。将每个成对标记为布尔值标签,例如y = 1表示匹配对,否则为0。

2) 只需计算每对的正/负项,并让0-1标签y选择哪个添加到损失中。


首先创建占位符,y_用于布尔标签。

dim = 64
x1_ = tf.placeholder('float32', shape=(None, dim))
x2_ = tf.placeholder('float32', shape=(None, dim))
y_ = tf.placeholder('uint8', shape=[None])   # uint8 for boolean

然后可以通过该函数创建损失张量。

def loss(x1, x2, y):
    # Euclidean distance between x1,x2
    l2diff = tf.sqrt( tf.reduce_sum(tf.square(tf.sub(x1, x2)),
                                    reduction_indices=1))

    # you can try margin parameters
    margin = tf.constant(1.)     

    labels = tf.to_float(y)

    match_loss = tf.square(l2diff, 'match_term')
    mismatch_loss = tf.maximum(0., tf.sub(margin, tf.square(l2diff)), 'mismatch_term')

    # if label is 1, only match_loss will count, otherwise mismatch_loss
    loss = tf.add(tf.mul(labels, match_loss), \
                  tf.mul((1 - labels), mismatch_loss), 'loss_add')

    loss_mean = tf.reduce_mean(loss)
    return loss_mean

loss_ = loss(x1_, x2_, y_)

然后输入你的数据(例如随机生成的):

batchsize = 4
x1 = np.random.rand(batchsize, dim)
x2 = np.random.rand(batchsize, dim)
y = np.array([0,1,1,0])

l = sess.run(loss_, feed_dict={x1_:x1, x2_:x2, y_:y})

它起作用了!非常感谢!顺便说一句,最好使用ReLU而不是max。但我使用的其余代码不变。谢谢! - mangate
margin的值应该是多少? - Mithun
@Mithun 这是一个可以调整的参数。我不确定现在什么是最佳实践,但在Caffe中默认值为1.0(http://caffe.berkeleyvision.org/tutorial/layers/contrastiveloss.html)。 - weitang114

7

简短回答

我认为最简单的方法是在 TensorFlow 图之外离线采样这些数据对。您需要创建一个批次的数据对以及它们的标签(正面或负面,即相同类别或不同类别)的 tf.placeholder,然后可以在 TensorFlow 中计算相应的损失。


代码实现

  1. 您需要在 TensorFlow 图之外离线采样这些数据对。您可以采样 batch_size 个输入对,并输出形状为 [batch_size, input_size] 的一侧对的 batch_size 个元素。您还需要输出形状为 [batch_size,] 的这些数据对的标签(正面或负面)。
pairs_left = np.zeros((batch_size, input_size))
pairs_right = np.zeros((batch_size, input_size))
labels = np.zeros((batch_size, 1))  # ex: [[0.], [1.], [1.], [0.]] for batch_size=4
  1. 然后你需要创建与这些输入相对应的Tensorflow占位符。在你的代码中,你将通过sess.run()函数的feed_dict参数将之前的输入提供给这些占位符。
pairs_left_node = tf.placeholder(tf.float32, [batch_size, input_size])
pairs_right_node = tf.placeholder(tf.float32, [batch_size, input_size])
labels_node = tf.placeholder(tf.float32, [batch_size, 1])

现在我们可以对输入进行前馈(假设您的模型是线性模型)。
W = ...   # shape [input_size, feature_size]
output_left = tf.matmul(pairs_left_node, W)  # shape [batch_size, feature_size]
output_right = tf.matmul(pairs_right_node, W)  # shape [batch_size, feature_size]
  1. 最后,我们可以计算成对损失。 Loss
l2_loss_pairs = tf.reduce_sum(tf.square(output_left - output_right), 1)
positive_loss = l2_loss_pairs
negative_loss = tf.nn.relu(margin - l2_loss_pairs)
final_loss = tf.mul(labels_node, positive_loss) + tf.mul(1. - labels_node, negative_loss)

就是这样!现在你可以通过好的离线采样来优化这个损失。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接