Limit overtraining
Validation loss or another validation metric stops improving.
Keras · Class
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.
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.
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, ...)| Name | Type | Description |
|---|---|---|
monitor | str | Metric or quantity to monitor; defaults to val_loss. |
min_delta | float | Minimum absolute change that counts as improvement; defaults to 0. |
patience | int | Number of unimproved epochs tolerated before stopping; defaults to 0. |
verbose | int | Verbosity level, 0 or 1; defaults to 0. |
mode | str | Improvement direction: auto, min, or max; defaults to auto. |
baseline | float or None | Optional baseline value for the monitored quantity; defaults to None. |
restore_best_weights | bool | Whether to restore weights from the best monitored epoch; defaults to False. |
start_from_epoch | int | Number of initial epochs to skip before monitoring; defaults to 0. |
An EarlyStopping callback object.
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.
Validation loss or another validation metric stops improving.
Training runs are expensive or have a high epoch limit.
The best validation epoch occurs before the stopping epoch.
Initial epochs are a deliberate warm-up period.
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.
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.
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.Callbackkeras.callbacks.ModelCheckpointkeras.callbacks.ReduceLROnPlateauModel.fit