I have cost function in tensorflow.
activation = tf.add(tf.mul(X, W), b)
cost = (tf.pow(Y-y_model, 2)) # use sqr error for cost function
I am trying out this example. How can I change it to rmse cost function?
To compute RMSE, calculate the residual (difference between prediction and truth) for each data point, compute the norm of residual for each data point, compute the mean of residuals and take the square root of that mean.
tf.sqrt(tf.reduce_mean(tf.square(tf.subtract(targets, outputs))))
And slightly simplified (TensorFlow overloads the most important operators):
tf.sqrt(tf.reduce_mean((targets - outputs)**2))
The formula for root mean square error is:
The way to implement it in TF is tf.sqrt(tf.reduce_mean(tf.squared_difference(Y1, Y2)))
.
The important thing to remember is that there is no need to minimize RMSE loss with the optimizer. With the same result you can minimize just tf.reduce_mean(tf.squared_difference(Y1, Y2))
or even tf.reduce_sum(tf.squared_difference(Y1, Y2))
but because they have a smaller graph of operations, they will be optimized faster.
But you can use this function if you just want to tract the value of RMSE.
Now we have tf.losses.mean_squared_error
Therefore,
RMSE = tf.sqrt(tf.losses.mean_squared_error(label, prediction))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With