Generally, a deep learning model takes a large amount of time to train, so its better to know how to save trained model. In this blog we will learn about how to save whole keras model i.e. its architecture, weights and optimizer state.
Lets first create a model in Keras. This is a simple autoencoder model. If you need to know more about autoencoders please refer this blog.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
from keras.models import Model from keras.layers import Dense, Input import matplotlib.pyplot as plt import numpy as np # creating an autoencoder model input_shape = Input(shape = (784,)) dense1 = Dense(512, activation = 'relu')(input_shape) dense2 = Dense(256, activation = 'relu')(dense1) encoded = Dense(32, activation = 'relu')(dense2) dense3 = Dense(256, activation = 'relu')(encoded) dense4 = Dense(512, activation = 'relu')(dense3) decoded = Dense(784, activation = 'relu')(dense4) autoencoder = Model(input_shape, decoded) m = 256 n_epoch = 25 autoencoder.compile(optimizer='adam', loss='mse') autoencoder.fit(output_X_train,output_X_train, epochs=n_epoch, batch_size=m, shuffle=True) |
Above we have created a Keras model named as “autoencoder“. Now lets see how to save this model.
Saving and loading only architecture of a model
In keras, you can save and load architecture of a model in two formats: JSON or YAML Models generated in these two format are human readable and can be edited if needed.
|
# saving and loading model architecture in json format # saving in json format json_model = autoencoder.to_json() json_file = open('autoencoder_json.json', 'w') json_file.write(json_model) # loading model architecture from json file from keras.models import model_from_json json_file = open('autoencoder_json.json', 'r') json_model = model_from_json(json_file.read()) |
|
# saving and loading model architecture in yaml format # saving in yaml format yaml_model = autoencoder.to_yaml() yaml_file = open('autoencoder_yaml.yaml', 'w') yaml_file.write(yaml_model) # loading model architecture from yaml file from keras.models import model_from_yaml yaml_file = open('autoencoder_yaml.yaml', 'r') yaml_model = model_from_yaml(yaml_file.read()) |
Saving and Loading Weights of a Keras Model
With model architecture you will also need model weights to predict output from trained model.
|
# saving model weights autoencoder.save_weights('autoencoder_weights.h5') # loading weights of a keras model json_model.load_weights('autoencoder_weights.h5') |
Saving and Loading Both Architecture and Weights in one File
|
# saving whole model autoencoder.save('autoencoder_model.h5') # loading whole model from keras.models import load_model model1 = load_model('autoencoder_model.h5') |
This will save following four parameters in “autoencoder_model.h5” file:
- Model Architecture
- Model Weights
- Loss and Optimizer
- State of the optimizer allowing to resume training where you left.
Hope you enjoy reading.
If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.