Tensorflow最大保证金损失培训?

时间:2016-07-08 15:17:10

标签: neural-network tensorflow

我想在张量流中训练一个具有最大边际损失函数的神经网络,每个正样本使用一个负样本:

max(0,1 -pos_score +neg_score)

我目前正在做的是: 网络有三个输入: input1 ,然后是一个正例 input2_pos 和一个负示例 input2_neg 。 (这些是单词嵌入层的索引。)网络应该计算一个分数,表示两个例子的相关程度。 这是我的代码的简化版本:

input1 = tf.placeholder(dtype=tf.int32, shape=[batch_size])
input2_pos = tf.placeholder(dtype=tf.int32, shape=[batch_size])
input2_neg = tf.placeholder(dtype=tf.int32, shape=[batch_size])

# f is a neural network outputting a score
pos_score = f(input1,input2_pos)
neg_score = f(input1,input2_neg)

cost = tf.maximum(0., 1. -pos_score +neg_score)
optimizer= tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

当我运行这个时,我看到的是,这样的网络只是知道哪个输入具有正面的例子 - 它总是预测类似的分数:

pos_score = 0.9965983
neg_score = 0.00341663

如何构建变量/培训,以便网络学习任务?

我只想要一个网络,它接受两个输入并计算表示它们之间相关性的分数,并训练它以最大边际损失。

分别计算正面和负面的分数对我来说似乎不是一个选项,因为它不会正确地反向传播。另一种选择似乎是随机输入 - 但是对于损失函数我需要知道哪个例子是正面的 - 输入另一个参数会再次给出解决方案?

有什么想法吗?

1 个答案:

答案 0 :(得分:4)

鉴于你的结果(每个阳性为1,每个阴性为0),你似乎有两种不同的网络学习:

  • 预测第一个1
  • 为第二个预测0

使用最大保证金损失时,您需要使用同一网络来计算ExperimentalWarning: 'pixels2d' is in an experimental state.pos_score。这样做的方法是分享变量。我将使用neg_score

给您一个小例子
tf.get_variable()

使用此函数with tf.variable_scope("network"): w = tf.get_variable("weights", shape=..., initializer=...) def f(x, y): with tf.variable_scope("network", reuse=True): w = tf.get_variable("weights") res = w * (x - y) # some computation return res 作为模型,培训将使用名称" network / weights"来优化共享变量。