Saving the models weights after each epoch

Let's see how we can save the model weights after every epoch. Let's first import some libraries

import keras
import numpy as np

In this example, we will be using the fashion MNIST dataset to do some basic computer vision, where we will train a Keras neural network to classify items of clothing.

In order to import the data we will be using the built in function in Keras :

keras.datasets.fashion_mnist.load_data()

The model is a very simple neural network consisting in 2 fully connected layers. The model loss function is chosen in order to have a multiclass classifier : "sparse_categorical_crossentropy"

Let's define a simple feedforward network.

##get and preprocess the data
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
train_images = train_images / 255.0
test_images = test_images / 255.0

## define the model 

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28,28)),
    keras.layers.Dense(128, activation="relu"),
    keras.layers.Dense(10, activation="softmax")
])

model.compile(optimizer="adam",
             loss = "sparse_categorical_crossentropy",
             metrics = ["acc"])

In order to automatically save the model weights to a hdf5 format after every epoch we need to import the ModelCheckpoint callback located in

keras.callbacks
from keras.callbacks import ModelCheckpoint

We now need to define the ModelCheckPoint callback, it takes 7 arguments :

  • filepath: string, path to save the model file. monitor: quantity to monitor
  • verbose: verbosity mode, 0 or 1
  • save_best_only: if save_best_only=True, the latest best model according to the quantity monitored will not be overwritten.
  • mode: one of {auto, min, max}
  • If save_best_only=True, the decision to overwrite the current save file is made based on either the maximization or the minimization of the monitored quantity.
  • save_weights_only: if True, then only the model's weights will be saved (model.save_weights(filepath)), else the full model is saved (model.save(filepath)).
  • period: Interval (number of epochs) between checkpoints.

The callback has to be added to the callbacks list in the fit method.

save_to_hdf5 = ModelCheckpoint(filepath="my_model.h5",
                               monitor='acc',
                               verbose=0,
                               save_best_only=True,
                               save_weights_only=False,
                               mode='auto',
                               period=1
                              )
model.fit(train_images, train_labels, epochs=5, callbacks=[save_to_hdf5])
Epoch 1/5
60000/60000 [==============================] - 29s 478us/step - loss: 0.3975 - acc: 0.8575
Epoch 2/5
60000/60000 [==============================] - 98s 2ms/step - loss: 0.3498 - acc: 0.8721
Epoch 3/5
60000/60000 [==============================] - 95s 2ms/step - loss: 0.3213 - acc: 0.8825
Epoch 4/5
60000/60000 [==============================] - 64s 1ms/step - loss: 0.3021 - acc: 0.8887: 6s - loss:
Epoch 5/5
60000/60000 [==============================] - 61s 1ms/step - loss: 0.2855 - acc: 0.8953: 4s - loss: 0.284 - ETA: 3s - loss:  - ETA:





<keras.callbacks.History at 0x1d70aa62f28>
import os 
if "my_model.h5" in os.listdir() :
    print("The model is saved to my_model.h5")
else:
    print('no model saved to disk')
The model is saved to my_model.h5