Using the checkpoint callback in Keras

In Chapter 2, Using Deep Learning to Solve Regression Problems, we saw the .save() method, that allowed us to save our Keras model after we were done training. Wouldn't it be nice, though, if we could write our weights to disk every now and then so that we could go back in time in the preceding example and save a version of the model before it started to overfit? We could then stop right there and use the lowest variance version of the network.

That's exactly what the ModelCheckpoint callback does for us. Let's take a look:  

checkpoint_callback = ModelCheckpoint(filepath="./model-weights.{epoch:02d}-{val_acc:.6f}.hdf5", monitor='val_acc', verbose=1, save_best_only=True)

What ModelCheckpoint will do for us is save our model at scheduled intervals. Here, we are telling ModelCheckpoint to save a copy of the model every time we hit a new best validation accuracy (val_acc). We could have also monitored validation loss or any other metric we had specified.  

The filename string will include the epoch number and the validation accuracy of the run.  

When we train our model again, we can see these files being created:

model-weights.00-0.971304.hdf5
model-weights.02-0.977391.hdf5
model-weights.05-0.985217.hdf5

So, we can see that after epoch 5, we weren't able to best our val_acc, and no checkpoints were written. We could then go back and load the weights from checkpoint 5 and use our best model.

There are some big assumptions here in calling epoch 5 the best. You may want to run the network several times, especially if your dataset is relatively small, as it is with our early examples in this book. We can be fairly certain that this result won't be stable.

This is, by the way, a really simple way to prevent over fitting. We can just choose to use a checkpoint of the model that occurred before variance got too big. It's one way to do something like early stopping which means we stop training before the specified number of epochs when we see the model isn't improving.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.52.188