I try to load a LSTM model (created by Keras) after using the command:
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
with the command:
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
and to print the lr and other hyper-parameters via the command :
loaded_model.summary()
I received all the information about the structure of the LSTM. But I did not receive the hyper-parameters information like the lr etc.
Learning rate is a parameter of the optimizer of the model and is not included in the model.summary()
output. If you want to find the value of learning rate, you can use optimizer
attribute of the model and use K.eval()
to evaluate the learning rate tensor and get its actual value:
print(K.eval(model.optimizer.lr))
Update: the optimizer of the model is not saved when you use to_json
method and therefore the above solution does not work. If you want to save the whole model including the layers weights as well as the optimizer (along with its state), you can use save
method:
model.save('my_model.h5')
Then you can load it using load_model
:
from keras.models import load_model
model = load_model('my_model.h5')
Alternatively, if you have used save_weights
method (to save the weights of layers) along with to_json
method (to save only the architecture of the model), then you can load back the weights after loading back the model using model_from_json
:
# load the architecture of model from json file ...
# load the weights
model.load_weights('model_weights.h5')
However, the optimizer in this second approach has not been saved and therefore you need to recompile the model (note that this means the state of the optimizer is lost and therefore you may not be able to easily to continue training the model without configuring the optimizer first; however, this is fine if you only want to use the model for prediction or retrain the model from scratch).
I highly recommend to read the related section in Keras FAQ as well: How can I save a Keras model?