1.keras深度学习框架的训练保存过程
from keras.models import model_from_json
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")
!!!注意:这段程序必须放在训练(即fit()函数)语句的后面,因为是训练后才能得到模型的相关框架和权重参数。
2.keras深度学习框架的调用
from keras.models import model_from_json
# load json and create model
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("model.h5")
print("Loaded model from disk")
!!!注意:这段程序必须放在编译及预测(即compile()和predict函数)语句的前面,因为载入模型及相应权重后才能进行进行编译和预测。