Keras保存和载入训练好的模型和参数

1.保存模型

my_model = create_model_function( ...... )

my_model.compile( ...... )

my_model.fit( ...... )

model_name . save( filepath, overwrite: bool=True, include_optimizer: bool=True )

filepath:保存的路径

overwrite:如果存在源文件,是否覆盖

include_optimizer:是否保存优化器状态

ex : mymodel.save(filepath=“p402/my_model.h5”, includeoptimizer=False)

2.载入模型

my_model = keras . models . load_model( filepath )

# 载入后可以继续训练:

my_model . fit( X_train_2,Y_train_2 )

# 也可以直接评估:

preds = my_model . evaluate( X_test, Y_test )

3.仅保存模型的结构,而不包含其权重或配置信息

# save as JSON
json_string = model.to_json()
# save as YAML
yaml_string = model.to_yaml()

从保存好的json文件或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)

4.需要保存模型的权重,可通过下面的代码利用HDF5进行保存

model.save_weights('my_model_weights.h5')

若在代码中初始化一个完全相同的模型,请使用

model.load_weights('my_model_weights.h5')

5.若要加载权重到不同的网络结构(有些层一样)中,例如fine-tune或transfer-learning,可通过层名字来加载模型

model.load_weights('my_model_weights.h5', by_name=True)
"""
假如原模型为:
    model = Sequential()
    model.add(Dense(2, input_dim=3, name="dense_1"))
    model.add(Dense(3, name="dense_2"))
    ...
    model.save_weights(fname)
"""
# new model
model = Sequential()
model.add(Dense(2, input_dim=3, name="dense_1"))  # will be loaded
model.add(Dense(10, name="new_dense"))  # will not be loaded
 
# load weights from first model; will only affect the first layer, dense_1.
model.load_weights(fname, by_name=True)

参考:
Keras如何保存和载入训练好的模型和参数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值