如何在Tens中设置RMSE成本函数

2024-05-15 14:53:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我在tensorflow中有成本函数。

activation = tf.add(tf.mul(X, W), b)
cost = (tf.pow(Y-y_model, 2)) # use sqr error for cost function

我在试this example。如何将其更改为rmse成本函数?


Tags: 函数addformodelusetftensorflowfunction
3条回答
tf.sqrt(tf.reduce_mean(tf.square(tf.subtract(targets, outputs))))

稍微简化(TensorFlow重载了最重要的运算符):

tf.sqrt(tf.reduce_mean((targets - outputs)**2))

现在我们有了tf.losses.mean_squared_error

因此

RMSE = tf.sqrt(tf.losses.mean_squared_error(label, prediction))

root mean square error的公式是:

enter image description here

在TF中实现它的方法是tf.sqrt(tf.reduce_mean(tf.squared_difference(Y1, Y2)))


需要记住的重要一点是,没有必要使用优化器将RMSE损失最小化。使用相同的结果,您可以将tf.reduce_mean(tf.squared_difference(Y1, Y2))甚至tf.reduce_sum(tf.squared_difference(Y1, Y2))最小化,但是因为它们有更小的操作图,所以优化速度会更快。

但如果您只想跟踪RMSE的值,则可以使用此函数。

相关问题 更多 >