python深度学习keras库学习(1)

1.如何存储keras模型
若只保存模型框架架构,不保存连接权值

# save as JSON
json_string = model.to_json()

# save as YAML
yaml_string = model.to_yaml()

2.如何从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)

3.步骤(1)中存储方式,只能存储模型框架,却不保存连接权值,下面介绍连接权值的保存和读取操作方式

model.save_weights('my_model_weights.h5')

model.load_weights('my_model_weights.h5')

JASON模型数据的读写操作与模型连接权值的保存及读取操作

json_string = model.to_json()
open('my_model_architecture.json', 'w').write(json_string)
model.save_weights('my_model_weights.h5')

# elsewhere...
model = model_from_json(open('my_model_architecture.json').read())
model.load_weights('my_model_weights.h5')

你可能感兴趣的:(python,深度学习)