Keras 2.0 模型加载与保存

本文介绍了两种常见的Keras模型保存与加载方法:一种是直接使用HDF5文件,另一种是通过JSON序列化模型结构和权重。第一种方法适合快速加载已训练模型,而第二种方法提供了更详细的模型持久化选项。

转 https://blog.csdn.net/yangzm/article/details/82685415

第一种

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

 

第二种

保存

# keras library import  for Saving and loading model and weights
from keras.models import model_from_json
from keras.models import load_model
 
# serialize model to JSON
#  the keras model which is trained is defined as 'model' in this example
model_json = model.to_json()
 
with open("model_num.json", "w") as json_file:
    json_file.write(model_json)


# serialize weights to HDF5
model.save_weights("model_num.h5")

加载

# load json and create model
json_file = open('model_num.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_num.h5")
print("Loaded model from disk")
 
model.save('model_num.hdf5')
loaded_model=load_model('model_num.hdf5')

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值