Tensorflow 损失函数:平均成对平方误差
当我使用Tensorflow 损失函数:平均成对平方误差,tensorflow,Tensorflow,当我使用 tf.losses.mean_pairwise_squared_error(labels, predictions, weights=1.0, scope=None, loss_collection=tf.GraphKeys.LOSSES) 函数,我确信数据是正确的。然而,张力板上的损耗始终为零。我努力想弄清楚,但不知道为什么?下面是我代码的一部分。我用错形状了吗 score_a=tf.reshape(score,[-1])#shape: [1,39] ys_a=tf.reshape
tf.losses.mean_pairwise_squared_error(labels, predictions, weights=1.0, scope=None, loss_collection=tf.GraphKeys.LOSSES)
函数,我确信数据是正确的。然而,张力板上的损耗始终为零。我努力想弄清楚,但不知道为什么?下面是我代码的一部分。我用错形状了吗
score_a=tf.reshape(score,[-1])#shape: [1,39]
ys_a=tf.reshape(ys,[-1])#shape: [1,39]
with tf.name_scope('loss'):
loss=tf.losses.mean_pairwise_squared_error(score_a,ys_a)
使用
tf.loss.mean_pairwise_squared_error()
,标签
和预测
应至少为2级,因为第一个维度将用作批量大小
。这意味着您不需要重塑score\u a
和ys\u a
。(我假设score_a
和ys_a
有39个条目和一批。)
如果标签
和预测
为秩1,则意味着所有数据项均为0张量(标量),因此tf.loss.mean_pairwise_squared_error()
的结果始终为零
还有一件事。在我看来,tf.loss.mean\u pairwise\u squared\u error()
的当前实施(2018-01-03)看起来不完美。例如,如中所示,将以下数据作为标签
和预测
:
labels=tf.常数([[0,0.5,1.]]
预测=tf.常数([[1,1,1.]]
tf.损失。平均成对误差(标签、预测)
在这种情况下,结果应该是[(0-0.5)^2+(0-1)^2+(0.5-1)^2]/3=0.5
,这与tensorflow得出的结果0.3333333333 134651184
不同
[Update]tf.Loss.mean_pairwise_squared_error()中的错误(如上所述)已修复,并从tensorflow 1.6.0中应用。如果您正在进行分类,则您可能会在一维中使用分类值标记
张量。如果是这种情况,则在调用tf.loss.mean\u pairwise\u squared\u error()
之前,需要执行一次热转换以匹配预测的形状。您不需要改变预测的形状
labels_a = tf.one_hot(labels, 2)