A callback runs user-defined actions at specified points during model training or evaluation. In practice, callbacks control training without changing the model code, especially when validation performance stops improving. section 9.4 section 9.6
Callbacks support several practical training decisions:
- Stop training after a chosen number of epochs or iterations without improvement. section 5.5.3 section 9.4
- Retain the model state associated with the best validation performance rather than the final training state. section 5.5.3 section 9.6
- Tune stopping behavior with monitoring choices, minimum improvement thresholds, and verbosity settings. section 5.5.3
Conditional Keras training
A configuration flag can choose between uninterrupted training and callback-controlled training: create an early-stopping callback when early_stop is enabled, then pass callback_list to model.fit() with validation_data. Otherwise, omit callbacks and train for all specified epochs. section 9.4
Listing 9.17 Code to compile and train the model
model.compile(optimizer=config['hyperparameters']['optimizer'],
loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
metrics=config['metrics']) #1
if config['general']['early_stop']:
callback_list, save_model_path = set_early_stop(es_monitor, es_mode)
model.fit(train_ds,
epochs=config['hyperparameters']['epochs'],
validation_data=val_ds,
callbacks=callback_list) #2
else:
model.fit(train_ds,
epochs=config['hyperparameters']['epochs'],
validation_data=val_ds) #3
The listing shows how configuration values select early stopping and how callback_list is supplied to model.fit(). section 9.4


















