Get startedGet started for free

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

View Course

Exercise instructions

  • Import both the EarlyStopping and ModelCheckpoint callbacks from tensorflow.keras.
  • Create monitor_val_acc as an EarlyStopping callback that will monitor 'val_accuracy', with a patience of 3 epochs.
  • Create model_checkpoint as a ModelCheckpointcallback, save the best model as best_banknote_model.hdf5.
  • Fit your model providing a list with the defined callbacks and X_test and y_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 = (____, ____))
Edit and Run Code