【问题标题】:Caffe EuclideanLoss reproduce in TensorflowCaffe EuclideanLoss 在 Tensorflow 中重现
【发布时间】:2017-07-11 07:36:12
【问题描述】:

我试图在Tensorflow 中从Caffe 复制EuclideanLoss。我找到了一个名为:tf.nn.l2_loss 的函数,根据文档计算如下:

output = sum(t ** 2) / 2

当查看 Python 版本的 caffe 中的 EuclideanLoss 时,它会说:

def forward(self, bottom, top):
        self.diff[...] = bottom[0].data - bottom[1].data
        top[0].data[...] = np.sum(self.diff**2) / bottom[0].num / 2.

在原始文档中它说:

对我来说,这是完全相同的计算。但是,我在 Tensorflow 中对同一网络的损失值约为 3000,而在 Caffe 中约为 300。那么差异在哪里?

【问题讨论】:

  • 我会说你还需要除以批量大小(10?),或者更好的是,使用tf.mean() 来计算批量的平均损失。
  • 嗯更好的不是我要求的。我要求完全相同的损失?! @ManoloSantos
  • 同样的损失。 tf.reduce_mean(x) / 2. == tf.sum(x) / x.shape[0] / 2.
  • 好的,你能回答这个问题并写下要使用的确切张量流损失吗?我有点困惑。 @ManoloSantos

标签: python tensorflow caffe euclidean-distance


【解决方案1】:

tf.nn.l2_loss 不考虑批量大小来计算损失。为了获得与 caffe 相同的值,您应该除以批量大小。为此,最简单的方法是使用均值 (sum / n):

import tensorflow as tf

y_pred = tf.constant([1, 2, 3, 4], tf.float32)
y_real = tf.constant([1, 2, 4, 5], tf.float32)
mse_loss = tf.reduce_mean(tf.square(y_pred - y_real)) / 2.

sess = tf.InteractiveSession()
mse_loss.eval()

【讨论】:

  • 我 100% 确定这不等于 caffe 的 EuclideanLoss,因为我已经在使用这个公式了!它返回 [0, 1] 范围内的值,因为我的地面真值范围为 [0, 1],这是由于均值函数。平均值返回“平均值”而不是总和。这是不正确的。你能看看这个链接caffe euclideanloss
  • @thigi。嗯,我认为您缺少均值的定义。 mean(x) = (1 / N) * sum(x)
  • 您的目标 (y) 是否可能是向量而不是标量?在那种情况下,情况就不同了。您只需对最后一个轴 (-1) 求和并除以批量大小
  • 是的,它们是这样的:tf.placeholder(tf.float32, shape=[1, 64, 64, 54])。你能更新你的答案吗?
猜你喜欢
  • 2017-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-25
  • 1970-01-01
  • 2020-10-18
  • 2017-05-08
  • 1970-01-01
相关资源
最近更新 更多