Skip to content Skip to sidebar Skip to footer

Keras, Best Way To Save State When Optimizing

I was just wondering what is the best way to save the state of a model while it it optimizing. I want to do this so I can run it for a while, save it, and come back to it some tim

Solution 1:

from keras.models import load_model

model.save('my_model.h5')  # creates a HDF5 file 'my_model.h5'del model  # deletes the existing model# returns a compiled model# identical to the previous one
model = load_model('my_model.h5')

You can use model.save(filepath) to save a Keras model into a single HDF5 file which will contain:

  • the architecture of the model, allowing to re-create the model
  • the weights of the model
  • the training configuration (loss, optimizer)
  • the state of the optimizer, allowing to resume training exactly where you left off.

You can then use keras.models.load_model(filepath) to reinstantiate your model. load_model will also take care of compiling the model using the saved training configuration (unless the model was never compiled in the first place).

Keras FAQ: How can I save a Keras model?

Solution 2:

You could create a tar archive containing the weights and the architecture, as well as a pickle file containing the optimizer state returned by model.optimizer.get_state().

Post a Comment for "Keras, Best Way To Save State When Optimizing"