欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在Keras中實(shí)現(xiàn)保存和加載權(quán)重及模型結(jié)構(gòu)

 更新時(shí)間:2020年06月15日 10:34:51   作者:Microstrong0305  
這篇文章主要介紹了在Keras中實(shí)現(xiàn)保存和加載權(quán)重及模型結(jié)構(gòu),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

1. 保存和加載模型結(jié)構(gòu)

(1)保存為JSON字串

json_string = model.to_json()

(2)從JSON字串重構(gòu)模型

from keras.models import model_from_json
model = model_from_json(json_string)

(3)保存為YAML字串

yaml_string = model.to_yaml()

(4)從YAML字串重構(gòu)模型

model = model_from_yaml(yaml_string)

2. 保存和加載模型權(quán)重(參數(shù))

from keras.models import load_model
 
# 創(chuàng)建HDF5文件'my_model.h5',保存模型參數(shù)
model.save('my_model.h5')
 
# 加載模型參數(shù)
load_model('my_model.h5')

2.1 處理已保存模型中的自定義層(或其他自定義對(duì)象)

如果要加載的模型包含自定義層或其他自定義類(lèi)或函數(shù),則可以通過(guò) custom_objects 參數(shù)將它們傳遞給加載機(jī)制:

from keras.models import load_model
 
# 假設(shè)你的模型包含一個(gè) AttentionLayer 類(lèi)的實(shí)例
model = load_model('my_model.h5', custom_objects={'AttentionLayer': AttentionLayer})

或者,你可以使用 自定義對(duì)象作用域:

from keras.utils import CustomObjectScope
with CustomObjectScope({'AttentionLayer': AttentionLayer}):
  model = load_model('my_model.h5')

自定義對(duì)象的處理與 load_model, model_from_json, model_from_yaml 的工作方式相同:

from keras.models import model_from_json
model = model_from_json(json_string, custom_objects={'AttentionLayer': AttentionLayer})

2019年6月1號(hào)更新:

更詳細(xì)的使用方法:

如何保存Keras模型?

(1)一個(gè)HDF5文件即保存模型的結(jié)構(gòu)又保存模型的權(quán)重

我們不推薦使用pickle或cPickle來(lái)保存Keras模型。

你可以使用model.save(filepath)將Keras模型和權(quán)重保存在一個(gè)HDF5文件中,該文件將包含:

模型的結(jié)構(gòu),以便重構(gòu)該模型

模型的權(quán)重

訓(xùn)練配置(損失函數(shù),優(yōu)化器等)

優(yōu)化器的狀態(tài),以便于從上次訓(xùn)練中斷的地方開(kāi)始

使用keras.models.load_model(filepath)來(lái)重新實(shí)例化你的模型,如果文件中存儲(chǔ)了訓(xùn)練配置的話,該函數(shù)還會(huì)同時(shí)完成模型的編譯。

例子:

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

(2)只保存模型的結(jié)構(gòu)

如果你只是希望保存模型的結(jié)構(gòu),而不包含其權(quán)重或配置信息,可以使用:

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

這項(xiàng)操作將把模型序列化為json或yaml文件,這些文件對(duì)人而言也是友好的,如果需要的話你甚至可以手動(dòng)打開(kāi)這些文件并進(jìn)行編輯。

當(dāng)然,你也可以從保存好的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)只保存模型的權(quán)重

如果需要保存模型的權(quán)重,可通過(guò)下面的代碼利用HDF5進(jìn)行保存。注意,在使用前需要確保你已安裝了HDF5和其Python庫(kù)h5py。

model.save_weights('my_model_weights.h5')

如果你需要在代碼中初始化一個(gè)完全相同的模型,請(qǐng)使用:

model.load_weights('my_model_weights.h5')

如果你需要加載權(quán)重到不同的網(wǎng)絡(luò)結(jié)構(gòu)(有些層一樣)中,例如fine-tune或transfer-learning,你可以通過(guò)層名字來(lái)加載模型:

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中實(shí)現(xiàn)保存和加載權(quán)重及模型結(jié)構(gòu)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論