C.W.K.
Stream
Lesson 06 of 08 · published

Callbacks

~9 min · training

Level 0Keras Apprentice
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

Callbacks are the training control plane

fit() runs the loop, but callbacks are what make a run smart. They're hooks Keras fires at fixed points — start and end of each epoch, each batch, the whole run — and from inside those hooks a callback can read your metrics and act: stop early, save a checkpoint, drop the learning rate, write a log. Without them, a training run is a fire-and-forget script that wastes GPU hours overfitting and hands you nothing to recover the best model from.

The four that turn a script into a pipeline

The Code section wires the core set. EarlyStopping watches val_loss and halts when it stops improving for patience epochs — and crucially, restore_best_weights=True rolls the model back to its best point instead of leaving you with the slightly-overfit final state. ModelCheckpoint with save_best_only=True keeps the best weights on disk as you go, so a crash never costs you the run. ReduceLROnPlateau cuts the learning rate when progress stalls, squeezing extra accuracy out of a plateau. The fourth slot is a logger — CSVLogger here, or swap in TensorBoard for live curves.

The order in the list doesn't matter for these four; they read state independently. When the built-ins don't cover your case, write a custom callback by subclassing keras.callbacks.Callback and overriding the hook you need — on_epoch_begin, on_batch_end, on_train_end, and the rest all receive a logs dict of the current metrics.

Code

The core callback set wired into fit()·python
callbacks = [
    # Stop when val_loss hasn't improved for 5 epochs
    keras.callbacks.EarlyStopping(
        monitor="val_loss",
        patience=5,
        restore_best_weights=True,  # Go back to best model
    ),

    # Save the best model
    keras.callbacks.ModelCheckpoint(
        "best_model.keras",
        monitor="val_loss",
        save_best_only=True,
    ),

    # Reduce LR when stuck
    keras.callbacks.ReduceLROnPlateau(
        monitor="val_loss",
        factor=0.5,     # Halve the LR
        patience=3,
        min_lr=1e-6,
    ),

    # Log to CSV
    keras.callbacks.CSVLogger("training_log.csv"),
]

model.fit(x_train, y_train, epochs=50, callbacks=callbacks)

External links

Exercise

Train MNIST with the four callbacks above. Confirm: best.keras file appears, training stops before 100 epochs (early stop), lr decreases when val_loss plateaus, TensorBoard logs are written.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.