How to implement early stopping in a Keras model?
Published on Aug. 22, 2023, 12:19 p.m.
To implement early stopping in a Keras model, you can use the EarlyStopping
callback from Keras. This callback allows you to specify a metric to monitor during training, and will stop training if the monitored metric stops improving.
Here is an example of how to use the EarlyStopping
callback:
from tensorflow import keras
model = keras.Sequential([keras.layers.Dense(10, input_shape=(784,), activation='softmax')])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=3)
history = model.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels), callbacks=[early_stop])
In this example, monitor='val_loss'
means that we will monitor the validation loss during training, and patience=3
means that we will stop training after 3 epochs of no improvement in the validation loss.
You can also specify additional arguments to the EarlyStopping
callback, such as mode='min'
to specify that the metric should be minimized, or restore_best_weights=True
to automatically restore the weights of the best-performing model during training.
Once training is complete, you can access the training history and metrics using the history
object returned by fit()
.