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

Keras · Class

losses.MeanSquaredError

MeanSquaredError is a Keras regression loss that averages squared differences between targets and predictions. Its instance-level reduction controls whether those per-sample values are averaged, summed, weighted, or left unaggregated.

How it works

When called with target values and predictions, the loss squares their element-wise differences and averages across the last dimension to produce per-sample losses. The loss object then aggregates those values according to reduction; by default, it averages them over the batch.

Syntax

keras.losses.MeanSquaredError(reduction="sum_over_batch_size", name="mean_squared_error", dtype=None)

Parameters

NameTypeDescription
reductionstring or NoneReduction mode for combining per-sample losses; supported values include "sum", "sum_over_batch_size", "mean", "mean_with_sample_weight", and None.
namestringOptional identifier for the loss instance.
dtypestring or NoneNumeric dtype used for loss computations; omitted means Keras uses its configured float type.

Returns

A callable loss object that produces per-sample mean-squared-error values and aggregates them according to reduction.

Key points

  • MeanSquaredError measures regression error by squaring prediction residuals and averaging them.
  • Its mathematical form is the mean of squared differences between y_true and y_pred.
  • Instantiate it through keras.losses.MeanSquaredError and pass the resulting object where a loss is expected.
  • Unless configured otherwise, the instance averages per-sample losses across the batch.
  • Use name to label the loss object when a custom identifier is useful.
  • The dtype option controls the numeric type used during loss computation.

Common mistakes

  • The default reduction is generally appropriate, but changing it alters how per-sample losses are combined.
  • Do not assume the standalone mean-squared-error function behaves like this class: the class performs reduction by default.

Example: Compile a regression model with MSE

import keras

model = keras.Sequential([
    keras.layers.Input(shape=(1,)),
    keras.layers.Dense(1),
])
model.compile(
    optimizer="adam",
    loss=keras.losses.MeanSquaredError(),
)

Creates a regression model and uses MeanSquaredError as its training loss.

When to use it

Regression training

Train a model to predict continuous numeric targets.

Model compilation

Use a loss object with Keras's model compilation API.

Per-sample analysis

Inspect individual example losses for custom aggregation or diagnostics.

Interview questions

QWhat does MeanSquaredError calculate?

It computes the mean of the squared differences between labels and predictions. The per-sample calculation averages over the last dimension, and the class then applies its configured reduction.

Follow-up: How would you preserve every per-sample value? Set reduction to None, which performs no aggregation.

QWhat is the default reduction?

The default is "sum_over_batch_size". It sums per-sample losses and divides by the sample size, yielding an average over the batch.

Follow-up: What does "sum" do instead? It returns the sum of the per-sample losses without dividing by batch size.

QWhich constructor options can be configured?

The constructor accepts reduction, name, and dtype. dtype controls the computation type and defaults to Keras's configured floating-point type when omitted.

Follow-up: How can sample weights affect reduction? With "mean_with_sample_weight", the loss is divided by the sum of sample weights.

  • keras.losses.mean_squared_error
  • keras.losses.MeanAbsoluteError
  • keras.losses.MeanSquaredLogarithmicError
  • keras.losses.Huber

Source: Regression losses - Keras. Checked 2026-10-15.