Project Creation: Part Three

In the previous lesson, our model architecture was read. Now, before moving to actual training, I want to first introduce you to something new.

Callbacks in Keras

Many times when we are training our model, we see that the training accuracy starts decreasing after a certain number of epochs and our model ends up with low accuracy (although we achieved high accuracy in previous epochs). So, how can we get the best model out of all the epochs? The answer is callbacks.

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.

We will discuss two types of callbacks and use them in our project.

1. ModelCheckpoint

The ModelCheckpoint callback is used in conjunction with training to save a model or weights (in a checkpoint file) at some interval, so the model or weights can be loaded later to continue the training from the state saved. We will use some parameters to save the model only if we get a higher training accuracy than in the previous epoch.

2. EarlyStopping

The EarlyStopping callback is used to stop training when a monitored metric has stopped improving.

The major difference among these two is that the ModelCheckpoint runs for all the epochs, whereas the EarlyStopping runs until the monitored metric is improving in the next epoch and stops if that doesn’t happen.

Now that you have an understanding of these callbacks, let’s use them in our project.

Get hands-on with 1200+ tech skills courses.