A combination of callbacks
Deep learning models can take a long time to train, especially when you move to deeper architectures and bigger datasets. Saving your model every time it improves as well as stopping it when it no longer does allows you to worry less about choosing the number of epochs to train for. You can also restore a saved model anytime and resume training where you left it.
The model training and validation data are available in your workspace as X_train
, X_test
, y_train
, and y_test
.
Use the EarlyStopping()
and the ModelCheckpoint()
callbacks so that you can go eat a jar of cookies while you leave your computer to work!
This exercise is part of the course
Introduction to Deep Learning with Keras
Exercise instructions
- Import both the
EarlyStopping
andModelCheckpoint
callbacks fromtensorflow.keras
. - Create
monitor_val_acc
as anEarlyStopping
callback that will monitor'val_accuracy'
, with apatience
of 3 epochs. - Create
model_checkpoint
as aModelCheckpoint
callback, save the best model asbest_banknote_model.hdf5
. - Fit your model providing a list with the defined callbacks and
X_test
andy_test
as validation data.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import the EarlyStopping and ModelCheckpoint callbacks
from tensorflow.____.____ import ____, ____
# Early stop on validation accuracy
monitor_val_acc = ____(monitor = ____, patience = ____)
# Save the best model as best_banknote_model.hdf5
model_checkpoint = ____(____, save_best_only = True)
# Fit your model for a stupid amount of epochs
h_callback = model.fit(X_train, y_train,
epochs = 1000000000000,
callbacks = [____, ____],
validation_data = (____, ____))