A callback is an object that can perform actions at various stages of training (e.g. at the start or end of an epoch, before or after a single batch, etc).
You can use callbacks to:
fit()
loopYou can pass a list of callbacks (as the keyword argument callbacks
) to the .fit()
method of a model:
my_callbacks = [
keras.callbacks.EarlyStopping(patience=2),
keras.callbacks.ModelCheckpoint(filepath='model.{epoch:02d}-{val_loss:.2f}.h5'),
keras.callbacks.TensorBoard(log_dir='./logs'),
]
model.fit(dataset, epochs=10, callbacks=my_callbacks)
The relevant methods of the callbacks will then be called at each stage of the training.
Creating new callbacks is a simple and powerful way to customize a training loop.
Learn more about creating new callbacks in the guide
Writing your own Callbacks, and refer to
the documentation for the base Callback
class.