When training a machine learning model, we would like to have the ability to monitor the model performance and perform certain actions depending on those performance measures. That’s where Keras Callbacks come in.

Callbacks are an important type of object TensorFlow and Keras that are designed to be able to monitor the performance in metrics at certain points in the training run and perform some action that might depend on those performance in metric values.

In this article, we’ll explore the following popular Keras Callbacks APIs with the help of some examples.

  1. EarlyStopping: a callback designed for early stopping.
  2. CSVLogger: a callback streams epoch results to a CSV file.
  3. ModelCheckpoint : a callback to save the Keras model or model weight during training
  4. ReduceLROnPlateau : a callback to reduce the learning rate when a metric has stopped improving.
  5. LearningRateScheduler: a callback for learning rate schedules.
  6. LambdaCallback: a callback for creating custom callbacks on-the-fly.

Please check out my Github repo for source code.

1. EarlyStopping

EarlyStopping is a built-in callback designed for early stopping. First, let’s import it and create an early stopping object:

from tensorflow.keras.callbacks import EarlyStopping

early_stopping = EarlyStopping()

EarlyStopping() has a few options and by default:

  • monitor='val_loss': to use validation loss as performance measure to terminate the training.
  • patience=0: is the number of epochs with no improvement. The value 0 means the training is terminated as soon as the performance measure gets worse from one epoch to the next.

Next, we just need to pass the callback object to model.fit() method.

history = model.fit(
    X_train, 
    y_train, 
    epochs=50, 
    validation_split=0.20, 
    batch_size=64, 
    verbose=2,
    callbacks=[early_stopping]
)

You can see that early_stopping get passed in a list to the callbacks argument. It is a list because in practice we might be passing a number of callbacks for performing different tasks, for example, debugging and learning rate schedules.

#machine-learning #callback #keras #deep-learning #tensorflow

A Practical Introduction to Keras Callbacks in TensorFlow 2
6.00 GEEK