Callbacks are Keras's built-in mechanism for hooking custom behavior into the training loop at specific points — early stopping, checkpointing, and learning rate scheduling, all handled declaratively without writing manual loop logic.
The Most Common Built-In Callbacks
| Callback | Purpose | Concept Note |
|---|---|---|
EarlyStopping | Stop training automatically when validation performance stops improving | Early Stopping |
ModelCheckpoint | Save the model (or best model) periodically during training | Checkpointing |
ReduceLROnPlateau | Reduce the learning rate when a metric stops improving | Learning Rate Scheduling |
TensorBoard | Log metrics for visualization in TensorBoard | Experiment Tracking |
Code — Using Multiple Callbacks Together
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
callbacks = [
EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True),
ModelCheckpoint('best_model.keras', monitor='val_loss', save_best_only=True),
ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=3)
]
model.fit(x_train, y_train, epochs=100, validation_data=(x_val, y_val), callbacks=callbacks)
Notice this declarative approach handles patterns that required manual, explicit implementation in PyTorch's Early Stopping and Checkpointing notes — Keras's .fit() call internally invokes these callbacks at the right points (end of epoch, end of batch, etc.) automatically.
Writing a Custom Callback
from tensorflow.keras.callbacks import Callback
class PrintLearningRate(Callback):
def on_epoch_end(self, epoch, logs=None):
lr = self.model.optimizer.learning_rate.numpy()
print(f"Epoch {epoch}: learning rate is {lr}")
model.fit(x_train, y_train, epochs=10, callbacks=[PrintLearningRate()])
Custom callbacks can hook into on_epoch_end, on_batch_end, on_train_begin, and many other specific points in the training lifecycle — a clean, structured alternative to scattering custom logic throughout a manual training loop.
Common Mistakes
- Forgetting
restore_best_weights=TrueonEarlyStopping— without it, the model's weights at the moment training stops are whatever the last epoch produced, not necessarily the best-performing epoch, exactly the mistake flagged conceptually in Early Stopping. - Setting
save_best_only=FalseonModelCheckpointwhen disk space is limited — this saves a checkpoint every epoch rather than only when performance improves.
Interview Relevance
Q: "How do Keras callbacks compare to how you'd implement early stopping and checkpointing manually in PyTorch?" Keras callbacks handle these patterns declaratively — you configure a callback object with the desired behavior (patience, what metric to monitor, whether to restore best weights) and pass it to .fit(), which invokes it automatically at the right points. PyTorch requires writing this logic manually inside the training loop, as covered in Early Stopping and Checkpointing — functionally equivalent, but Keras trades manual control for convenience.
Practice Question
Why is restore_best_weights=True important when using EarlyStopping, rather than just letting training stop at whatever epoch triggered it?