Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a Keras models' history after loading it from a file in Python?

Tags:

I have saved a keras model as a h5py file and now want to load it from disk.

When training the model I use:

from keras.models import Sequential

model = Sequential()
H = model.fit(....)

When the model is trained, I want to load it from disk with

model = load_model()

How can I get H from the model variable? It unfortunately does not have a history parameter that I can just call. Is it because the save_model function doesn't save history?

like image 508
A_toaster Avatar asked Dec 16 '17 06:12

A_toaster


People also ask

What is history in Keras?

In the Keras docs, we find: The History. history attribute is a dictionary recording training loss values and metrics values at successive epochs, as well as validation loss values and validation metrics values (if applicable).

Where are Keras models stored?

They are stored at ~/. keras/models/ . Upon instantiation, the models will be built according to the image data format set in your Keras configuration file at ~/. keras/keras.

What is Keras sequential ()?

Definition of Keras Sequential. Keras is an API that gets well with Neural network models related to artificial intelligence and machine learning so is the keras sequential which deals with ordering or sequencing of layers within a model.

How do I save a Keras model for later use?

Keras provides the ability to describe any model using JSON format with a to_json() function. This can be saved to a file and later loaded via the model_from_json() function that will create a new model from the JSON specification.


1 Answers

Unfortunately it seems that Keras hasn't implemented the possibility of loading the history directly from a loaded model. Instead you have to set it up in advance. This is how I solved it using CSVLogger (it's actually very convenient storing the entire training history in a separate file. This way you can always come back later and plot whatever history you want instead of being dependent on a variable you can easily lose stored in the RAM):

First we have to set up the logger before initiating the training.

from keras.callbacks import CSVLogger

csv_logger = CSVLogger('training.log', separator=',', append=False)
model.fit(X_train, Y_train, callbacks=[csv_logger])

The entire log history will now be stored in the file 'training.log' (the same information you would get, by in your case, calling H.history). When the training is finished, the next step would simply be to load the data stored in this file. You can do that with pandas read_csv:

import pandas as pd
log_data = pd.read_csv('training.log', sep=',', engine='python')

From here on you can treat the data stored in log_data just as you would by loading it from K.history.

More information in Keras callbacks docs.

like image 153
Jakob Avatar answered Sep 25 '22 18:09

Jakob