In frameworks like Keras, what is a callback in the context of model training?
Select an answer to reveal the explanation.
Short Explanation and Infographic
A callback is basically a hook Keras lets you plug custom behavior into at key moments — end of a batch, end of an epoch, start of training, and so on. You don't write the whole training loop yourself to react to those moments; you pass callback objects into model.fit(), and Keras calls them automatically at the right time. Classic examples are ModelCheckpoint, which saves the best weights so far, and EarlyStopping, which halts training if validation loss stops improving. That's exactly 'invoked automatically at specific points to perform an action' — the correct answer. It's not a required data-loading step; loading data happens separately via your Dataset/DataLoader before fit() starts. It's not about specifying a GPU device either — that's handled through device placement. And it's not a loss function; callbacks and loss functions serve completely different roles in training.
Full explanation below image
Full Explanation
A callback in Keras (and analogous concepts exist in other frameworks) is an object designed to be invoked automatically by the training loop at specific, predefined points — such as at the start or end of training, at the start or end of each epoch, or at the start or end of each batch — in order to perform some action without requiring the developer to manually modify the core training loop itself. Callbacks are passed as a list to the callbacks argument of model.fit(), and the framework calls the appropriate callback methods at the corresponding points automatically. Common built-in callbacks include ModelCheckpoint, which saves the model's weights (often only when validation performance improves, preserving the 'best' version seen so far); EarlyStopping, which monitors a specified metric (like validation loss) and halts training automatically if it fails to improve for a specified number of epochs, helping to prevent wasted computation and overfitting; ReduceLROnPlateau, which lowers the learning rate when a metric plateaus; and TensorBoard, which logs metrics for visualization. Developers can also write custom callbacks by subclassing the base Callback class and overriding methods like on_epoch_end() to implement bespoke logging, custom learning rate schedules, or other training-time behaviors, all without touching the actual gradient computation or weight update logic.
Loading the dataset before training starts is handled separately, typically via a Dataset and DataLoader (in PyTorch) or by preparing NumPy/TensorFlow arrays or a tf.data pipeline (in TensorFlow/Keras) before model.fit() is called. This data-preparation step is conceptually and functionally distinct from callbacks, which react to events during the training process itself rather than performing the initial data ingestion.
Specifying which GPU device to use for training is a separate configuration concern, typically handled through device placement commands (like .to('cuda') in PyTorch) or framework-level device visibility settings (like environment variables or tf.device() context managers in TensorFlow), not through callbacks. Callbacks have no inherent role in hardware device selection.
A loss function (such as categorical cross-entropy or mean squared error) is the quantity being minimized during training, computed by comparing model predictions to true labels, and is configured via model.compile(loss=...). It is a fundamentally different concept from a callback: the loss function drives the actual gradient computation and parameter updates, while a callback merely observes and reacts to the training process at specified checkpoints, without itself influencing how gradients are computed.