我有一个卷积神经网络,其中三个图像作为输入:
x_anchor = tf.placeholder('float', [None, 4900], name='x_anchor')
x_positive = tf.placeholder('float', [None, 4900], name='x_positive')
x_negative = tf.placeholder('float', [None, 4900], name='x_negative')
在一个train
函数中,我为占位符提供实际图像:
input1, input2, input3 = training.next_batch(start,end)
....some other operations...
loss_value = sess.run([cost], feed_dict={x_anchor:input1, x_positive:input2, x_negative:input3})
我在这三个输入上使用了三元组损失函数(实际上是上面的成本变量):
def triplet_loss(d_pos, d_neg):
margin = 0.2
loss = tf.reduce_mean(tf.maximum(0., margin + d_pos - d_neg))
return loss
如何过滤损失,因此只有 loss_value > 0 的图像将用于训练网络?
我怎样才能实现类似的东西:
if(loss_value for input1, input2, input3 > 0)
use inputs to train network
else
do nothing/try another input
到目前为止我已经尝试过:
我一张一张地拍摄图像(input1[0]、input2[0]、input3[0]),计算损失,如果损失为正,我将计算(并应用)梯度。但问题是我在我的模型中使用了dropout,我必须在我的输入上应用两次模型:
首先计算loss并验证是否大于0
其次运行优化器:这是出现问题的时候。前面提到过,我使用了dropout,所以模型在我的输入上的结果是不一样的,所以即使第1步确定的loss大于0,新的loss有时候也会为0。
我也尝试使用tf.py_func
但卡住了。