liveBook · Manning

Callback

As explained in Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI

A Keras callback is an object that runs user-defined actions at specified points during model training or evaluation.

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

How Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI covers it

How deep it goes
Defines callbacks and demonstrates fit() integration, conditional configuration, and early-stopping controls without detailing callback internals.
Examples
Uses Keras model.fit(), configuration-driven early stopping, and LightGBM logging and early-stopping callbacks.
What it assumes
Assumes a compiled Keras model, training and validation datasets, configured epochs, and familiarity with model.fit().
What you can do after
Operate/configure training with callbacks that stop stalled runs and select the strongest observed model state.
How it connects
Connects callbacks with model fitting, validation data, early stopping, best iteration, and overfitting control.

Also explained in

Compare how 19 books explain callback →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI. The section links point to the parts of the book it draws on.