我是tensorflow的新手
在 tensorflow session 的部分代码中,有:
loss = tf.nn.softmax_cross_entropy_with_logits_v2(
logits=net, labels=self.out_placeholder, name='cross_entropy')
self.loss = tf.reduce_mean(loss, name='mean_squared_error')
为此,我想使用 mean_squared_error 损失函数。我在 tensorflow 网站上找到了这个损失函数:
tf.losses.mean_squared_error(
labels,
predictions,
weights=1.0,
scope=None,
loss_collection=tf.GraphKeys.LOSSES,
reduction=Reduction.SUM_BY_NONZERO_WEIGHTS
)
我需要这个损失函数来解决回归问题。
我试过:
loss = tf.losses.mean_squared_error(predictions=net, labels=self.out_placeholder)
self.loss = tf.reduce_mean(loss, name='mean_squared_error')
其中 net = tf.matmul(input_tensor, weights) + biases
但是,我不确定这是不是正确的方法。
请您参考如下方法:
首先请记住,交叉熵主要用于分类,而 MSE 用于回归。
在您的情况下,交叉熵测量两个分布之间的差异(真实发生,称为标签 - 和您的预测)
因此,虽然第一个损失函数作用于 softmax 层的结果(可以看作是概率分布),但第二个损失函数直接作用于网络的浮点输出(没有概率分布)——因此它们不能简单地交换。
