用TensorF进行成对距离计算

2024-04-29 14:17:17 发布

您现在位置:Python中文网/ 问答频道 /正文

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

很快,我想为每个小批量的特性做一个成对距离计算,并将这个损失插入到一般的网络损失中。 我只有批次的Tesnor(16个样本),批次的标签张量和批次特征张量。

找了好一阵子,我还是搞不懂:

1)如何划分正片(即相同标签)和负片对的批次。因为张量不是迭代的,所以我不知道如何得到哪个样本有哪个标签,然后划分向量,或者得到张量的哪些索引属于每个类。

2)如何对批次张量中的某些指标进行成对距离计算?

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

总的来说,我需要得到哪些索引属于哪个类,对所有的正对进行正对距离计算。对所有的负对做另一个计算。然后把这些加起来,加上网络损耗。

任何帮助(三个问题中的一个)都将非常感谢。


Tags: 网络comhttp距离pdf标签样本损失
2条回答

(一) 您应该在将数据输入会话之前进行配对采样。为每对标记一个布尔值标签,对于匹配的对来说y=1,否则为0。

2)3)只需计算每对的正/负项,并让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})

简短回答

我认为最简单的方法是离线采样(即在TensorFlow图之外) 为一批对及其标签(正或负,即同一类或不同类)创建tf.placeholder,然后在TensorFlow中计算相应的损失。


代码

  1. 你可以离线取样。您采样batch_size对输入,并输出batch_size对形状[batch_size, input_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])
  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)

就这样!现在可以通过良好的脱机采样来优化此丢失。

相关问题 更多 >