Limit unnecessary epochs
Validation performance plateaus during supervised training.
TensorFlow · Class
Stops Keras training when a monitored metric no longer improves. It can require a minimum improvement, tolerate a configured number of stalled epochs, use a baseline or warm-up period, and restore the best weights.
During Model.fit, the callback evaluates the selected metric after each epoch. It applies the chosen improvement direction, counts epochs without sufficient progress, and sets model.stop_training when patience is exhausted; optionally, it restores weights from the best observed epoch.
tf.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 | Quantity to monitor; defaults to val_loss. |
min_delta | float | Minimum absolute change required to count as improvement; defaults to 0. |
patience | int | Number of unimproved epochs tolerated; defaults to 0. |
verbose | int | Verbosity mode, 0 or 1; defaults to 0. |
mode | str | Improvement direction: auto, min, or max; defaults to auto. |
baseline | float or None | Optional baseline for the monitored quantity. |
restore_best_weights | bool | Whether to restore weights from the best epoch; defaults to False. |
start_from_epoch | int | Epoch count to skip before monitoring; defaults to 0. |
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Input(shape=(10,)),
tf.keras.layers.Dense(1)
])
model.compile(optimizer="adam", loss="mse")
stop = tf.keras.callbacks.EarlyStopping(
monitor="val_loss",
patience=3,
restore_best_weights=True
)
model.fit(
x_train, y_train,
validation_split=0.2,
epochs=50,
callbacks=[stop]
)Stops fitting when validation loss no longer improves and restores the weights from the best epoch.
Validation performance plateaus during supervised training.
A model may begin with unstable metrics.
The final stopping epoch may overfit relative to an earlier epoch.
It is a Keras callback that stops training when a monitored metric stops improving. At the end of an epoch, Model.fit checks the metric and eventually marks model.stop_training as True.
Follow-up: How would you delay monitoring? Set start_from_epoch to the desired warm-up length so early epochs do not trigger stopping.
monitor selects the quantity, while mode determines whether improvement means decreasing or increasing. auto infers the direction from the monitored quantity's name; min and max make it explicit.
Follow-up: When should mode be explicit? Use it when the metric name could make automatic direction inference ambiguous or when custom metrics have unusual names.
min_delta is the minimum absolute change required to count as improvement, and patience is the number of unimproved epochs tolerated. With patience set to zero, the first qualifying lack of improvement can stop training.
Follow-up: How can the final model retain the best epoch rather than the last epoch? Set restore_best_weights=True; otherwise, training ends with the weights from the stopping epoch.
tf.keras.callbacks.Callbacktf.keras.callbacks.ModelCheckpointtf.keras.callbacks.ReduceLROnPlateautf.keras.Model.fit