Day 9 - Using Callbacks in Keras
-
A list of callbacks can be passed to the
fit()
method.
-
Keras calls callbacks at:
- start and end of training
- start and end of an epoch
- before and after processing each batch
ModelCheckpoint
callback
-
Saves checkpoints of model at regular intervals during training.
-
If a validation set is used during training,
save_best_only=True
will save weights only if
performance on validation set is best so far.
-
No need to worry about training too long and overfitting on training
set. Restore last model after training. This will be best model on
training set.
EarlyStopping
callback
-
Training is stopped when there is no progress on validation set for
a given number of epochs (defined by the
patience
argument).
- Optionally, can roll back to the best model.
-
Set number of epochs to large value since training will stop
automatically when there is no progress.
Custom callbacks
- Extend the
keras.callbacks.Callback
class.
-
For callback to be used during training, implement methods:
on_train_begin()
, on_train_end()
,
on_epoch_begin()
, on_epoch_end()
,
on_batch_begin()
and on_batch_end()
.
-
For callbacks to be used during evaluation, implement methods:
on_test_begin()
, on_test_end()
,
on_test_batch_begin()
, and
on_test_batch_end()
.
-
For callbacks to be used during prediction, implement methods:
on_predict_begin()
, on_predict_end()
,
on_predict_batch_begin()
and
on_predict_batch_end()
.
-
Using callbacks during evaluation and prediction can be useful for
debugging.