EarlyStopping watches a chosen metric, most commonly val_loss, after every epoch, and halts training once that metric fails to improve for patience consecutive epochs, preventing wasted computation and helping avoid the overfitting that tends to happen if training continues well past the point where validation performance peaks. Passing restore_best_weights=True additionally rolls the model's weights back to whichever epoch achieved the best monitored value, rather than leaving it at whatever weights existed when training actually stopped, several epochs after the true best point.
1Understanding callbacks.EarlyStopping()
EarlyStopping watches a chosen metric, most commonly val_loss, after every epoch, and halts training once that metric fails to improve for patience consecutive epochs, preventing wasted computation and helping avoid the overfitting that tends to happen if training continues well past the point where validation performance peaks. Passing restore_best_weights=True additionally rolls the model's weights back to whichever epoch achieved the best monitored value, rather than leaving it at whatever weights existed when training actually stopped, several epochs after the true best point.
Always pass restore_best_weights=True alongside EarlyStopping — without it, training stops patience epochs after the best result, but the model is left with those later, already-degrading weights rather than the genuinely best ones seen during training.
import tensorflow as tf
callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True)
print(callback.patience)2Practical Example
Here is a real-world application of callbacks.EarlyStopping() showing how it is used in production TensorFlow code.
import tensorflow as tf
from tensorflow.keras import layers, Sequential
import numpy as np
model = Sequential([layers.Dense(1, input_shape=(1,))])
model.compile(optimizer='sgd', loss='mse')
callback = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=2)
x, y = np.array([1, 2, 3, 4]), np.array([2, 4, 6, 8])
history = model.fit(x, y, epochs=100, callbacks=[callback], verbose=0)
print(len(history.history['loss']) <= 100)3Best Practices
Follow these guidelines when working with callbacks.EarlyStopping():
1. Pass restore_best_weights=True so the final model uses the weights from its best epoch, not whatever epoch training happened to stop on
2. Monitor val_loss, or a validation metric, rather than a training metric, since the goal is to detect when the model stops generalizing better, not just fitting the training data better
3. Set patience high enough to tolerate normal epoch-to-epoch noise in validation performance, rather than stopping prematurely on a single bad epoch
Tip: Always pass restore_best_weights=True alongside EarlyStopping — without it, training stops patience epochs after the best result, but the model is left with those later, already-degrading weights rather than the genuinely best ones seen during training.
import tensorflow as tf
callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True)
print(callback.patience)