Early stopping is a simple and effective regularization technique that monitors a model's performance on a validation dataset during training. Instead of training for a fixed number of epochs, it automatically stops training when the validation metric no longer improves. This helps prevent overfitting and saves computational resources.
- Stops training before the model begins memorizing noise in the training data, improving generalization.
- Selects the model from the epoch with the best validation metric instead of the final epoch.
- Reduces training time by avoiding unnecessary epochs once performance has stabilized.

Working
- Step 1: Split the Dataset Divide the available data into training and validation sets. The training set is used to update the model weights, while the validation set is used only to monitor performance during training.
- Step 2: Train the Model Train the model for multiple epochs using the training dataset.
- Step 3: Evaluate on the Validation Set At the end of every epoch, calculate the selected validation metric, such as validation loss or validation accuracy.
- Step 4: Compare with the Best Performance If the validation metric improves, save the current model weights as the best-performing model and reset the patience counter.
- Step 5: Check the Patience Value If the validation metric does not improve for the specified number of consecutive epochs, the patience counter reaches its limit and training is stopped automatically.
- Step 6: Restore the Best Model If enabled, the model restores the weights from the epoch that achieved the best validation performance before completing training.
Key Parameters
These parameters can be configured using the built-in EarlyStopping callback available in deep learning frameworks such as TensorFlow and Keras.
| Parameter | Description |
|---|---|
| monitor | Specifies the metric to track during training, such as val_loss or val_accuracy. Training decisions are based on changes in this metric. |
| patience | Defines the number of consecutive epochs to wait for an improvement in the monitored metric before stopping training. |
| min_delta | Sets the minimum change in the monitored metric that qualifies as an improvement. Smaller changes than this value are ignored. |
| mode | Determines whether the monitored metric should be minimized ("min"), maximized ("max") or inferred automatically ("auto"). |
| restore_best_weights | If enabled (True), restores the model weights from the epoch that achieved the best validation performance after training stops. |
Implementation using TensorFlow/Keras
In this implementation, we will train a simple neural network on the MNIST handwritten digit dataset and apply Early Stopping to automatically stop training when the validation loss stops improving.
Step 1: Import Required Libraries
First, import the required libraries for building the neural network, loading the dataset and visualizing the training process.
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Flatten, Dense
from tensorflow.keras.callbacks import EarlyStopping
import matplotlib.pyplot as plt
Step 2: Load and Preprocess the Dataset
Load the MNIST dataset and normalize the pixel values to the range [0, 1] for faster and more stable training.
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train = x_train / 255.0
x_test = x_test / 255.0
Step 3: Build the Neural Network
Create a simple fully connected neural network for handwritten digit classification.
model = Sequential([
Flatten(input_shape=(28, 28)),
Dense(128, activation="relu"),
Dense(10, activation="softmax")
])
model.compile(
optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"]
)
Step 4: Create the Early Stopping Callback
Configure the Early Stopping callback to monitor the validation loss. Training will stop if the validation loss does not improve for 3 consecutive epochs and the best model weights will be restored.
- monitor="val_loss" monitors the validation loss after each epoch.
- patience=3 allows training to continue for three additional epochs without improvement.
- restore_best_weights=True restores the model weights from the epoch with the lowest validation loss.
early_stopping = EarlyStopping(
monitor="val_loss",
patience=3,
restore_best_weights=True
)
Step 5: Train the Model with Early Stopping
Train the model while reserving 20% of the training data for validation.
history = model.fit(
x_train,
y_train,
epochs=20,
batch_size=32,
validation_split=0.2,
callbacks=[early_stopping],
verbose=1
)
Step 6: Evaluate the Model
Evaluate the trained model on the test dataset.
test_loss, test_accuracy = model.evaluate(x_test, y_test)
print(f"Test Loss: {test_loss:.4f}")
print(f"Test Accuracy: {test_accuracy:.4f}")
Output:
313/313 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.9766 - loss: 0.0775
Test Loss: 0.0775
Test Accuracy: 0.9766
Step 7: Visualize the Training Process
Plot the training loss and validation loss to observe where Early Stopping terminated the training.
plt.figure(figsize=(8, 5))
plt.plot(history.history["loss"], label="Training Loss")
plt.plot(history.history["val_loss"], label="Validation Loss")
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.title("Training and Validation Loss")
plt.legend()
plt.show()
Output:
The graph shows that:
- The training loss continuously decreases with each epoch.
- The validation loss decreases initially but eventually stops improving.
- After the validation loss fails to improve for three consecutive epochs, Early Stopping terminates the training automatically.
You can download the code from here.
Applications
- Medical Image Diagnosis: Stops training before medical imaging models memorize limited patient scans, improving diagnosis on unseen cases.
- Image Classification: Prevents CNNs from overfitting large image datasets while maintaining high recognition accuracy.
- Natural Language Processing: Helps language models generalize better when trained on limited text corpora.
- Fraud Detection: Improves detection of unseen fraudulent transactions by preventing memorization of historical patterns.
- Time Series Forecasting: Produces forecasting models that generalize better to future observations instead of historical noise.
Advantages
- Prevents overfitting by selecting the model before validation performance degrades.
- Reduces training time by avoiding unnecessary epochs.
- Saves computational resources during deep learning training.
- Works with almost any neural network architecture.
- Easy to implement using built-in callbacks in TensorFlow and Keras.
Limitations
- Choosing an inappropriate patience value can lead to underfitting or overfitting.
- Requires a separate validation dataset, reducing available training data.
- Validation metrics may fluctuate, causing inconsistent stopping decisions.