Saving and loading the weights and the architecture of a model

Model architectures can be easily saved and loaded as follows:

# save as JSON json_string = model.to_json()# save as YAML yaml_string = model.to_yaml() # model reconstruction from JSON: from keras.models import model_from_json model = model_from_json(json_string) # model reconstruction from YAML model = model_from_yaml(yaml_string)

Model parameters (weights) can be easily saved and loaded as follows:

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')

Get Deep Learning with Keras now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.