1. 程式人生 > >Keras 深度學習程式碼筆記——模型儲存與載入

Keras 深度學習程式碼筆記——模型儲存與載入

你可以使用model.save(filepath)將Keras模型和權重儲存在一個HDF5檔案中,該檔案將包含:

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

使用keras.models.load_model(filepath)來重新例項化你的模型,如果檔案中儲存了訓練配置的話,該函式還會同時完成模型的編譯
例子:

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檔案中載入模型:

# 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庫h5py

model.save_weights('my_model_weights.h5')

如果你需要在程式碼中初始化一個完全相同的模型,請使用:

model.load_weights('my_model_weights.h5')

如果你需要載入權重到不同的網路結構(有些層一樣)中,例如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)