Caffe EuclideanLoss 在 Tensorflow 中重现
Caffe EuclideanLoss reproduce in Tensorflow
我正在尝试在 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。那么差异在哪里?
tf.nn.l2_loss
为了计算损失没有考虑batch size。为了获得与 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()
我正在尝试在 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。那么差异在哪里?
tf.nn.l2_loss
为了计算损失没有考虑batch size。为了获得与 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()