You can learn a lot about Keras models by observing their History objects after training. In this post, you will discover how you can save the history object into a CSV file of deep learning models training metrics over time during training. 

History callback is the default callback that is registered when training models. It records training metrics for each epoch. This includes the loss and the accuracy for the training dataset as well as the loss and accuracy for the validation dataset. Metrics are stored in a dictionary in the history member of the object returned.

The code below loads the CIFAR-10 dataset using the Keras API.

(x_train, y_train), (x_val, y_val) = tf.keras.datasets.cifar10.load_data()


x_train = x_train.astype('float32')
x_val = x_val.astype('float32')
x_train /= 255
x_val /= 255


BATCH_SIZE=32

The following code defines a simple convolutional neural network for image classification using a common pattern a stack of Conv2D and MaxPooling2D layers.

model = tf.keras.models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation=tf.keras.layers.LeakyReLU(alpha=0.2), input_shape=(32, 32, 3)))

model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation=tf.keras.layers.LeakyReLU(alpha=0.2)))

model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation=tf.keras.layers.LeakyReLU(alpha=0.2)))

model.add(layers.Flatten())
model.add(layers.Dense(64, activation=tf.keras.layers.LeakyReLU(alpha=0.2)))

model.add(layers.Dense(10, activation='softmax'))

Create CSVLOGGER Callback

Keras provides the capability to register callbacks when training a model. It is an object that can perform actions at various stages of training like at the start or end of an epoch, before or after a single batch, etc.

filename='log.csv'
history_logger=tf.keras.callbacks.CSVLogger(filename, separator=",", append=True)

This callback streams epoch results to a CSV file. If append is “True” then it appends if the file exists. It is useful for continuing training. otherwise, overwrite the existing file. This callback supports all values that can be represented as a string, including 1D iterables such as np.ndarray.

You can pass this callback as the keyword argument callbacks to the .fit() method of a model:

history_const=model.fit(x_train, y_train,
              batch_size=BATCH_SIZE,
              epochs=epochs,
              callbacks=[history_logger],
              validation_data=(x_val, y_val),
              shuffle=True)

The above code saves model history training in form of a datasheet file log.csv.

Save and load History object With Numpy

A history object has a history field, it is a dictionary that holds different training metrics spanned across every training epoch. So e.g. history.history[‘loss’][10] will return a loss of your model in the 10th epoch of training. In order to save that you could pickle this dictionary.

np.save('history1.npy',history_const.history)

history1=np.load('history1.npy',allow_pickle='TRUE').item()

The above code is useful when saving history at the end of the training process. If you want to save the history during the training then use the CSVLogger callback.