RMSE
cost function
TensorFlow
machine learning
tutorial

how to set rmse cost function in tensorflow

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

If you want RMSE in TensorFlow, first decide whether you mean a training loss or an evaluation metric. RMSE is a perfectly reasonable metric for regression, but for optimization many practitioners still train with MSE and report RMSE, because RMSE is just the square root of MSE and the square root adds extra gradient behavior without changing the ordering of predictions.

TensorFlow supports both approaches. You can register RMSE as a metric with RootMeanSquaredError, or write a custom loss if you truly want to optimize RMSE directly.

Use RMSE As A Metric

For most Keras regression models, this is the simplest setup:

python
1import tensorflow as tf
2from tensorflow import keras
3from tensorflow.keras import layers
4
5model = keras.Sequential([
6    layers.Input(shape=(4,)),
7    layers.Dense(16, activation="relu"),
8    layers.Dense(1),
9])
10
11model.compile(
12    optimizer="adam",
13    loss="mse",
14    metrics=[tf.keras.metrics.RootMeanSquaredError()],
15)

This trains on mean squared error and reports RMSE during evaluation and training logs.

If You Really Want RMSE As The Loss

You can define it directly:

python
1import tensorflow as tf
2
3def rmse_loss(y_true, y_pred):
4    return tf.sqrt(tf.reduce_mean(tf.square(y_pred - y_true)))
5
6model.compile(
7    optimizer="adam",
8    loss=rmse_loss,
9    metrics=[tf.keras.metrics.RootMeanSquaredError()],
10)

That is valid TensorFlow code. The model will now optimize RMSE itself.

Why Many Models Still Train With MSE

RMSE and MSE rank predictions the same way because square root is monotonic for nonnegative values. That means minimizing MSE also minimizes RMSE. In many cases, MSE is preferred as the loss because it is simpler and avoids the extra square-root operation in the training objective.

So the practical recommendation is often:

  • use RMSE as a metric for reporting
  • use MSE as the training loss unless you have a specific reason not to

That gives you readable model quality numbers without complicating the optimization unnecessarily.

Tensor Shapes Still Matter

Regardless of which version you use, y_true and y_pred must have compatible shapes. If one is shaped like (batch, 1) and the other is (batch,), Keras may still broadcast in ways that hide mistakes. It is worth checking shapes explicitly when the loss values look strange.

Metrics And Losses Serve Different Jobs

Teams often talk about a single “cost function” when they really mean two different things: the objective the optimizer minimizes and the number humans want to read in reports. Keeping MSE as the loss and RMSE as the metric is popular precisely because it separates those two jobs cleanly.

Units Matter In Interpretation

RMSE is expressed in the same units as the target variable. That makes it intuitive for humans, but it also means that comparing RMSE across unrelated datasets or differently scaled targets is often meaningless unless the target units are comparable.

A small RMSE is meaningful only relative to the target scale.

Common Pitfalls

  • Calling RMSE a cost function when what you really need is a metric.
  • Using a custom RMSE loss without understanding that MSE is often the more standard optimization choice.
  • Forgetting to import TensorFlow math functions inside the custom loss.
  • Debugging poor training when the real problem is target scaling or shape mismatch.
  • Comparing RMSE values across datasets with different target units as if they were directly comparable.

Summary

  • TensorFlow can use RMSE either as a metric or as a custom loss.
  • 'tf.keras.metrics.RootMeanSquaredError() is the easiest way to report RMSE.'
  • A custom loss can compute sqrt(mean(square(error))) if needed.
  • In many cases, training with MSE and reporting RMSE is the cleaner default.
  • Always check target shapes and units when interpreting RMSE.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design