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

Keras · Class

callbacks.EarlyStopping

EarlyStopping stops model training when a monitored metric stops improving. It supports patience, improvement thresholds, warm-up epochs, baselines, and optional restoration of the best weights.

How it works

EarlyStopping watches a metric during training and marks the model to stop when that metric fails to improve for the configured patience. It can ignore an initial warm-up period, require a baseline, and optionally restore the weights from the best monitored epoch.

Syntax

keras.callbacks.EarlyStopping(monitor="val_loss", min_delta=0, patience=0, verbose=0, mode="auto", baseline=None, restore_best_weights=False, start_from_epoch=0, ...)

Parameters

NameTypeDescription
monitorstrMetric or quantity to monitor; defaults to val_loss.
min_deltafloatMinimum absolute change that counts as improvement; defaults to 0.
patienceintNumber of unimproved epochs tolerated before stopping; defaults to 0.
verboseintVerbosity level, 0 or 1; defaults to 0.
modestrImprovement direction: auto, min, or max; defaults to auto.
baselinefloat or NoneOptional baseline value for the monitored quantity; defaults to None.
restore_best_weightsboolWhether to restore weights from the best monitored epoch; defaults to False.
start_from_epochintNumber of initial epochs to skip before monitoring; defaults to 0.

Returns

An EarlyStopping callback object.

Key points

  • EarlyStopping is a training callback for ending fit when a selected metric no longer improves.
  • min_delta defines the smallest absolute metric change that qualifies as progress.
  • patience sets how many unimproved epochs are tolerated before stopping.
  • mode accepts auto, min, or max to determine whether lower or higher monitored values are better.
  • restore_best_weights controls whether training ends with the best observed weights.
  • start_from_epoch provides a warm-up interval before metric improvement is evaluated.

Common mistakes

  • Setting baseline does not merely initialize the best score: training stops when the monitored metric fails to improve over that value.
  • Use start_from_epoch when early epochs are expected to be unstable; monitoring is delayed during that warm-up.
  • restore_best_weights defaults to false, so stopping alone does not automatically roll back the model to its best epoch.

Example: Stop when validation loss stalls

import keras

model = keras.Sequential([
    keras.layers.Dense(16, activation="relu"),
    keras.layers.Dense(1),
])
model.compile(optimizer="adam", loss="mse")

stop = keras.callbacks.EarlyStopping(
    monitor="val_loss",
    patience=3,
    restore_best_weights=True,
)
model.fit(
    x_train,
    y_train,
    validation_split=0.2,
    epochs=100,
    callbacks=[stop],
)

This trains for at most 100 epochs, stops after three consecutive validation-loss epochs without sufficient improvement, and restores the weights from the best monitored epoch.

When to use it

Limit overtraining

Validation loss or another validation metric stops improving.

Reduce wasted computation

Training runs are expensive or have a high epoch limit.

Keep the best weights

The best validation epoch occurs before the stopping epoch.

Delay monitoring

Initial epochs are a deliberate warm-up period.

Interview questions

QHow does EarlyStopping decide when to stop?

The callback monitors the configured quantity, such as val_loss, and counts epochs without a qualifying improvement. Once the no-improvement count reaches patience, it sets model.stop_training to true so fit terminates.

Follow-up: How can early noisy epochs be excluded? Set start_from_epoch to the number of warm-up epochs; improvement monitoring begins only afterward.

QWhat does the mode argument control?

mode controls the direction of improvement: min expects the monitored quantity to decrease, max expects it to increase, and auto infers the direction from the metric name. The default is auto.

Follow-up: When should mode be specified explicitly? Specify min or max when the metric name does not clearly communicate its optimization direction.

QWhich options most affect the stopping behavior and final weights?

min_delta is the minimum absolute change that counts as improvement, patience is the number of unimproved epochs tolerated, and restore_best_weights can restore the weights from the best monitored epoch. The callback can also require improvement over baseline.

Follow-up: What happens if restore_best_weights is false? The model keeps the weights from the final training step rather than the best monitored epoch.

  • keras.callbacks.Callback
  • keras.callbacks.ModelCheckpoint
  • keras.callbacks.ReduceLROnPlateau
  • Model.fit

Source: EarlyStopping - Keras. Checked 2026-10-15.