Reputation: 1288
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?
Upvotes: 27
Views: 17880
Reputation: 893
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.
Upvotes: 29
Reputation: 1288
Using pickle
to save the history object threw a whole host of errors. As it turns out you can instead use pickle
on H.history
instead of H
to save your history file!
Kind of annoying having to have a model and a history file saved, but whatever
Upvotes: 4