🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Deep Learning Notes
Topic #371

Keras Callbacks

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

CallbackPurposeConcept Note
EarlyStoppingStop training automatically when validation performance stops improvingEarly Stopping
ModelCheckpointSave the model (or best model) periodically during trainingCheckpointing
ReduceLROnPlateauReduce the learning rate when a metric stops improvingLearning Rate Scheduling
TensorBoardLog metrics for visualization in TensorBoardExperiment 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=True on EarlyStopping — 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=False on ModelCheckpoint when 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?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Keras Callbacks – FAQs

Quick answers about learning Keras Callbacks in Deep Learning.

This free note from CodingNow 2.0 explains Keras Callbacks in Deep Learning — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Keras Callbacks, is 100% free with no signup required.
With focused practice, most students grasp Keras Callbacks in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now