Keras Callbacks: EarlyStopping, ModelCheckpoint & TensorBoard

Deep Learning with Keras and TensorFlow 10 min min read Updated: Nov 16, 2026 Intermediate
Keras Callbacks: EarlyStopping, ModelCheckpoint & TensorBoard
Intermediate Topic 6 of 8

What is a Callback?

A callback is a helper that Keras runs automatically at certain moments of training, for example at the end of every epoch. It lets you control and monitor training without writing a manual loop.

Important Callbacks

CallbackWhat it does
EarlyStoppingStops training when a metric stops improving
ModelCheckpointSaves the model (or the best one) during training
ReduceLROnPlateauLowers the learning rate when progress stalls
TensorBoardWrites logs to visualize training in a browser
CSVLoggerSaves the metrics of each epoch to a CSV file
LearningRateSchedulerChanges the learning rate with your own function

Putting Them Together

python import keras callbacks = [ keras.callbacks.EarlyStopping( monitor="val_loss", patience=5, restore_best_weights=True), keras.callbacks.ModelCheckpoint( filepath="best_model.keras", monitor="val_loss", save_best_only=True), keras.callbacks.ReduceLROnPlateau( monitor="val_loss", factor=0.5, patience=2, min_lr=1e-6), keras.callbacks.TensorBoard(log_dir="logs"), ] history = model.fit( x_train, y_train, epochs=100, validation_split=0.2, callbacks=callbacks )
  • Set a big number of epochs, and let EarlyStopping decide when to stop
  • patience=5 waits 5 epochs without improvement
  • save_best_only=True keeps only the best version on disk
  • factor=0.5 halves the learning rate when stuck

Viewing TensorBoard

bash tensorboard --logdir logs

Open the address shown in the terminal (usually http://localhost:6006) to see live graphs of loss and accuracy. In a notebook you can use %load_ext tensorboard and %tensorboard --logdir logs.

Load the Best Model Later

python best = keras.models.load_model("best_model.keras") print(best.evaluate(x_test, y_test))

Writing Your Own Callback

python class StopAtAccuracy(keras.callbacks.Callback): def on_epoch_end(self, epoch, logs=None): if logs.get("accuracy", 0) > 0.99: print("\nReached 99% accuracy, stopping training.") self.model.stop_training = True model.fit(x_train, y_train, epochs=50, callbacks=[StopAtAccuracy()])
Good habit: Use EarlyStopping together with ModelCheckpoint. You never have to guess how many epochs to train, and you always keep your best model.

Conclusion

Callbacks make training smarter and safer. Almost every real project uses at least EarlyStopping and ModelCheckpoint.

Quick Summary: Pass callbacks=[...] to fit() to stop early, save the best model, reduce the learning rate and log to TensorBoard.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators