使用 TensorFlow 进行成对距离计算
Doing pairwise distance computation with TensorFlow
我正在尝试实现这篇文章:
http://ronan.collobert.com/pub/matos/2008_deep_icml.pdf
特别是第 2 节中的等式 (3)。
不久我想对每个小批量的特征进行成对距离计算,并将此损失插入到一般网络损失中。
我只有批次的 Tesnor(16 个样本)、批次的标签张量和批次特征张量。
找了半天还是没搞清楚:
1) 如何将批次划分为正(即相同标签)和负对。由于张量不可迭代,我无法弄清楚如何获取哪个样本具有哪个标签然后划分我的向量,或者获取张量的哪些索引属于每个 class.
2) 如何对批量张量中的某些指标进行成对距离计算?
3) 我还需要为反例定义一个新的距离函数
总的来说,我需要得到哪些索引属于哪个 class,对所有正对进行正对距离计算。并对所有负对进行另一次计算。然后将其全部加起来并添加到网络损失中。
任何帮助(对 3 个问题中的一个或多个)将不胜感激。
1)
您应该在将数据输入会话之前 进行配对采样 。为每对标记一个布尔标签,假设 y = 1 表示匹配对,否则为 0。
2) 3) 只需计算每对的两个 pos/neg 项,让 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 图表之外)。
您为一批对及其标签(正或负,即相同 class 或不同 class)创建 tf.placeholder
,然后您可以在 TensorFlow 中计算相应的损失。
用代码
- 您离线对这些对进行了采样。您对
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
- 然后创建与这些输入对应的 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]
- 最后我们可以计算成对损失。
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)
就是这样!您现在可以通过良好的离线采样来优化此损失。
我正在尝试实现这篇文章: http://ronan.collobert.com/pub/matos/2008_deep_icml.pdf 特别是第 2 节中的等式 (3)。
不久我想对每个小批量的特征进行成对距离计算,并将此损失插入到一般网络损失中。 我只有批次的 Tesnor(16 个样本)、批次的标签张量和批次特征张量。
找了半天还是没搞清楚:
1) 如何将批次划分为正(即相同标签)和负对。由于张量不可迭代,我无法弄清楚如何获取哪个样本具有哪个标签然后划分我的向量,或者获取张量的哪些索引属于每个 class.
2) 如何对批量张量中的某些指标进行成对距离计算?
3) 我还需要为反例定义一个新的距离函数
总的来说,我需要得到哪些索引属于哪个 class,对所有正对进行正对距离计算。并对所有负对进行另一次计算。然后将其全部加起来并添加到网络损失中。
任何帮助(对 3 个问题中的一个或多个)将不胜感激。
1) 您应该在将数据输入会话之前 进行配对采样 。为每对标记一个布尔标签,假设 y = 1 表示匹配对,否则为 0。
2) 3) 只需计算每对的两个 pos/neg 项,让 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 图表之外)。
您为一批对及其标签(正或负,即相同 class 或不同 class)创建 tf.placeholder
,然后您可以在 TensorFlow 中计算相应的损失。
用代码
- 您离线对这些对进行了采样。您对
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
- 然后创建与这些输入对应的 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]
- 最后我们可以计算成对损失。
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)
就是这样!您现在可以通过良好的离线采样来优化此损失。