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.
EarlyStopping
: a callback designed for early stopping.CSVLogger
: a callback streams epoch results to a CSV file.ModelCheckpoint
: a callback to save the Keras model or model weight during trainingReduceLROnPlateau
: a callback to reduce the learning rate when a metric has stopped improving.LearningRateScheduler
: a callback for learning rate schedules.LambdaCallback
: a callback for creating custom callbacks on-the-fly.Please check out my Github repo for source code.
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