【发布时间】:2018-11-16 08:58:51
【问题描述】:
我正在尝试实现连体网络,如paper
在本文中,他们使用交叉熵作为损失函数
我使用 STL-10 数据集进行训练,而不是论文中使用的 3 层网络,我将其替换为 VGG-13 CNN 网络,除了最后一个 logit 层。
这是我的损失函数代码
def loss(pred,true_pred):
cross_entropy_loss = tf.multiply(-1.0,tf.reduce_mean(tf.add(tf.multiply(true_pred,tf.log(pred)),tf.multiply((1-true_pred),tf.log(tf.subtract(1.0,pred))))))
total_loss = tf.add(tf.reduce_sum(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)),cross_entropy_loss,name='total_loss')
return cross_entropy_loss,total_loss
with tf.device('/gpu:0'):
h1 = siamese(feed_image1)
h2 = siamese(feed_image2)
l1_dist = tf.abs(tf.subtract(h1,h2))
with tf.variable_scope('pred') as scope:
predictions = tf.contrib.layers.fully_connected(l1_dist,1,activation_fn = tf.sigmoid,weights_initializer = tf.contrib.layers.xavier_initializer(uniform=False),weights_regularizer = tf.contrib.layers.l2_regularizer(tf.constant(0.001, dtype=tf.float32)))
celoss,cost = loss(predictions,feed_labels)
with tf.variable_scope('adam_optimizer') as scope:
optimizer = tf.train.AdamOptimizer(learning_rate=0.001)
opt = optimizer.minimize(cost)
但是,当我进行训练时,成本几乎保持在 0.6932
我在这里使用过 Adam Optimizer。
但之前我使用的是 Momentum Optimizer。 我已尝试更改学习率,但成本仍然相同。
并且经过几次迭代,所有预测值都收敛到 0.5。
在获取两批图像(输入 1 和输入 2)的输出后,我获取它们的 L1 距离,并且我已经连接了一个具有单个输出和 sigmoid 激活函数的全连接层。
[h1和h2包含VGG-13网络的最后一个全连接层(不是logit层)的输出]
由于输出激活函数是sigmoid,并且由于预测值在0.5左右,我们可以计算出两个网络输出的加权L1距离之和接近于零。
我不明白我哪里出错了。 非常感谢您的帮助。
【问题讨论】:
-
我也有同样的问题,我的错误停留在 0.6931。在检查时,我观察到 L1 距离的最后一个全连接层的权重变得非常非常小,这一定导致总和为零,正如你所提到的。不知道该怎么办
标签: python tensorflow conv-neural-network