keras: how to save the training history attribute of the history object

What I use is the following:

with open('/trainHistoryDict', 'wb') as file_pi:
    pickle.dump(history.history, file_pi)

In this way I save the history as a dictionary in case I want to plot the loss or accuracy later on. Later, when you want to load the history again, you can use:

with open('/trainHistoryDict', "rb") as file_pi:
    history = pickle.load(file_pi)

Why choose pickle over json?

The comment under this answer accurately states:

[Storing the history as json] does not work anymore in tensorflow keras. I had issues with: TypeError: Object of type ‘float32’ is not JSON serializable.

There are ways to tell json how to encode numpy objects, which you can learn about from this other question, so there’s nothing wrong with using json in this case, it’s just more complicated than simply dumping to a pickle file.

Leave a Comment