Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TensorFlow · Class

losses.MeanSquaredError

Computes the average squared difference between labels and predictions. It is a configurable Keras Loss object with selectable reduction behavior.

How it works

The loss compares corresponding values in the labels and predictions, squares their differences, and averages the resulting errors. Calling the object invokes the inherited loss interface; the configured reduction controls how per-example losses are aggregated.

Syntax

tf.keras.losses.MeanSquaredError(
    reduction='sum_over_batch_size',
    name='mean_squared_error'
)

Parameters

NameTypeDescription
reductionstr or NoneType of reduction to apply to the loss. Supported values are "sum", "sum_over_batch_size", or None.
namestrOptional name for the loss instance.

Returns

A loss value computed from the labels and predictions; its aggregation is controlled by reduction.

Key points

  • It measures squared prediction error, so larger mistakes contribute disproportionately more than smaller mistakes.
  • The calculation subtracts predictions from labels, squares elementwise differences, then takes their mean.
  • Because it derives from Loss, the class fits Keras loss APIs and can be configured as a model objective.
  • The reduction setting supports summing, averaging over the batch, or disabling aggregation.
  • The name parameter labels the loss object and defaults to mean_squared_error.
  • Its core computation receives true target values and predicted values through the call method.

Common mistakes

  • Changing the default reduction can alter how the batch loss is aggregated; use the documented default unless a different aggregation is intentional.

Example: Compute mean squared error

import tensorflow as tf

loss_fn = tf.keras.losses.MeanSquaredError()
y_true = tf.constant([[1.0, 2.0], [3.0, 4.0]])
y_pred = tf.constant([[1.5, 1.0], [2.0, 5.0]])
loss = loss_fn(y_true, y_pred)
print(float(loss))

Creates the loss object and computes one scalar loss for two samples.

When to use it

Optimize continuous predictions with a squared-error objective.

Regression training

Reuse one configured loss object when calculating batch loss.

Custom training loops

Store and restore the configured loss through its Keras configuration.

Loss serialization

Interview questions

QWhat does MeanSquaredError calculate?

It computes the mean of the squared differences between y_true and y_pred: mean(square(y_true - y_pred)). The class is a Loss, so it can be passed directly as a Keras model loss.

Follow-up: How would you change aggregation? Instantiate it with reduction='sum' or reduction=None, choosing deliberately because the reduction changes the returned loss structure and scale.

QWhat are the constructor parameters?

The constructor accepts reduction, which specifies the type of loss aggregation, and name, which identifies the loss instance. The documented default reduction is 'sum_over_batch_size'.

Follow-up: When might reduction=None be useful? It can preserve individual loss values instead of aggregating them, which is useful when later code needs per-example weighting or custom aggregation.

QHow is the loss used in a Keras model?

The callable interface accepts y_true, y_pred, and optional sample_weight. Calling the object computes the loss using the configured reduction, while get_config() exposes its serializable configuration.

Follow-up: Why use the callable object rather than only a standalone function? The object packages configuration such as reduction and name and integrates with Keras loss serialization and training APIs.

  • tf.keras.losses.Loss
  • tf.keras.losses.MeanAbsoluteError
  • tf.keras.losses.MeanSquaredLogarithmicError
  • tf.keras.losses.MSE

Source: tf.keras.losses.MeanSquaredError. Checked 2026-10-15.