如何保存Keras模型

咱們不推薦使用pickle或cPickle來保存Keras模型python

你能夠使用model.save(filepath)將Keras模型和權重保存在一個HDF5文件中,該文件將包含:json

  • 模型的結構,以便重構該模型
  • 模型的權重
  • 訓練配置(損失函數,優化器等)
  • 優化器的狀態,以便於從上次訓練中斷的地方開始

使用keras.models.load_model(filepath)來從新實例化你的模型,若是文件中存儲了訓練配置的話,該函數還會同時完成模型的編譯網絡

例子:ide

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

若是你只是但願保存模型的結構,而不包含其權重或配置信息,能夠使用:函數

# save as JSON
json_string = model.to_json()

# save as YAML
yaml_string = model.to_yaml()

這項操做將把模型序列化爲json或yaml文件,這些文件對人而言也是友好的,若是須要的話你甚至能夠手動打開這些文件並進行編輯。優化

固然,你也能夠從保存好的json文件或yaml文件中載入模型:spa

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

若是須要保存模型的權重,可經過下面的代碼利用HDF5進行保存。注意,在使用前須要確保你已安裝了HDF5和其Python庫h5pycode

model.save_weights('my_model_weights.h5')

若是你須要在代碼中初始化一個徹底相同的模型,請使用:input

model.load_weights('my_model_weights.h5')

若是你須要加載權重到不一樣的網絡結構(有些層同樣)中,例如fine-tune或transfer-learning,你能夠經過層名字來加載模型:string

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)