Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set rmse cost function in tensorflow

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?

like image 698
Vikash Singh Avatar asked Nov 21 '15 16:11

Vikash Singh


People also ask

How do you calculate RMSE in ML?

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.


3 Answers

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))
like image 184
Rajarshee Mitra Avatar answered Oct 16 '22 10:10

Rajarshee Mitra


The formula for root mean square error is:

enter image description here

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.

like image 24
Salvador Dali Avatar answered Oct 16 '22 09:10

Salvador Dali


Now we have tf.losses.mean_squared_error

Therefore,

RMSE = tf.sqrt(tf.losses.mean_squared_error(label, prediction))
like image 8
pjh Avatar answered Oct 16 '22 09:10

pjh