DL4J中文文檔/Keras模型導(dǎo)入/函數(shù)模型

導(dǎo)入Keras函數(shù)模型入門

假設(shè)你使用Keras的函數(shù)API開始定義一個簡單的MLP:

from keras.models import Model
from keras.layers import Dense, Input

inputs = Input(shape=(100,))
x = Dense(64, activation='relu')(inputs)
predictions = Dense(10, activation='softmax')(x)
model = Model(inputs=inputs, outputs=predictions)
model.compile(loss='categorical_crossentropy',optimizer='sgd', metrics=['accuracy'])

image.gif

在Keras,有幾種保存模型的方法。你可以將整個模型(模型定義、權(quán)重和訓(xùn)練配置)存儲為HDF5文件,僅存儲模型配置(作為JSON或YAML文件)或僅存儲權(quán)重(作為HDF5文件)。以下是你如何做每一件事:

model.save('full_model.h5')  # save everything in HDF5 format

model_json = model.to_json()  # save just the config. replace with "to_yaml" for YAML serialization
with open("model_config.json", "w") as f:
    f.write(model_json)

model.save_weights('model_weights.h5') # save just the weights.

image.gif

如果你決定保存完整的模型,那么你將能夠訪問模型的訓(xùn)練配置,否則你將不訪問。因此,如果你想在導(dǎo)入之后在DL4J中進(jìn)一步訓(xùn)練模型,請記住這一點,并使用model.save(...)來持久化你的模型。

載加你的Keras模型

讓我們從推薦的方法開始,將完整模型加載回DL4J(我們假設(shè)它在類路徑上):

String fullModel = new ClassPathResource("full_model.h5").getFile().getPath();
ComputationGraph model = KerasModelImport.importKerasModelAndWeights(fullModel);

image.gif

萬一你沒有編譯你的Keras模型,它就不會有一個訓(xùn)練配置。在這種情況下,你需要顯式地告訴模型導(dǎo)入忽略訓(xùn)練配置,方法是將enforceTrainingConfig標(biāo)志設(shè)置為false,如下所示:

ComputationGraph model = KerasModelImport.importKerasModelAndWeights(fullModel, false);

image.gif

若要僅從JSON加載模型配置,請按如下使用KerasModelImport

String modelJson = new ClassPathResource("model_config.json").getFile().getPath();
ComputationGraphConfiguration modelConfig = KerasModelImport.importKerasModelConfiguration(modelJson)

image.gif

如果另外你還想加載模型權(quán)重與配置,那么以下是你要做的:

String modelWeights = new ClassPathResource("model_weights.h5").getFile().getPath();
MultiLayerNetwork network = KerasModelImport.importKerasModelAndWeights(modelJson, modelWeights)

image.gif

在后面兩種情況下,將不讀取訓(xùn)練配置。


KerasModel

[源碼]

從Keras(函數(shù)API)模型或序列模型配置構(gòu)建計算圖。

KerasModel

public KerasModel(KerasModelBuilder modelBuilder)
            throws UnsupportedKerasConfigurationException, IOException, InvalidKerasConfigurationException 

image.gif

(建議)(函數(shù)API)模型的構(gòu)建器模式構(gòu)造器。

  • 參數(shù) modelBuilder 構(gòu)建器對象
  • 拋出 IOException IO 異常
  • 拋出 InvalidKerasConfigurationException 無效的 Keras 配置
  • 拋出 UnsupportedKerasConfigurationException 不支持的 Keras 配置

getComputationGraphConfiguration

public ComputationGraphConfiguration getComputationGraphConfiguration()
            throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException 

image.gif

(不推薦)來自模型配置(JSON或YAML)、訓(xùn)練配置(JSON)、權(quán)重和“訓(xùn)練模式”布爾指示符的(函數(shù) API)模型的構(gòu)造器。當(dāng)內(nèi)置在訓(xùn)練模式時,某些不支持的配置(例如,未知的正則化器)將拋出異常。當(dāng)強(qiáng)制TrainingConfig= false時,這些將生成警告,但將被忽略。

  • 參數(shù) modelJson 模型配置JSON 字符串
  • 參數(shù) modelYaml 模型配置 YAML 字符串
  • 參數(shù) enforceTrainingConfig 是否實施訓(xùn)練相關(guān)配置
  • 拋出 IOException IO 異常
  • 拋出 InvalidKerasConfigurationException 無效的 Keras 配置
  • 拋出 UnsupportedKerasConfigurationException 不支持的 Keras 配置

getComputationGraph

public ComputationGraph getComputationGraph()
            throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException 

image.gif

從這個Keras模型配置構(gòu)建計算圖并導(dǎo)入權(quán)重。

  • 返回 ComputationGraph

getComputationGraph

public ComputationGraph getComputationGraph(boolean importWeights)
            throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException 

image.gif

從這個Keras模型配置構(gòu)建計算圖并(可選的)導(dǎo)入權(quán)重。

  • 參數(shù) importWeights 是否導(dǎo)入權(quán)重
  • 返回 ComputationGraph

翻譯:風(fēng)一樣的男子

image

如果您覺得我的文章給了您幫助,請為我買一杯飲料吧!以下是我的支付寶,意思一下我將非常感激!

image
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 這次回去,我發(fā)現(xiàn)父親老了很多。 六十出頭的年紀(jì),在這個全民駐顏逆生長的時代,其實算不得老。他的頭發(fā)卻快掉光了。走幾...
    暗夜之翼閱讀 275評論 0 0
  • 1. S型增長。當(dāng)一項新技術(shù)出現(xiàn)的時候,它不會立即被市場接受。但是隨著使用人數(shù)的增多,終端用戶之間互相交換信息的選...
    安妮李斯特閱讀 216評論 0 0
  • 這位英雌沒有天命 她愛盔甲 愛瑜伽 筋骨舒展 她是晨練戰(zhàn)士 河畔刷牙的英雄 是她年輕的衰老公 “寧靜喜樂 來自運(yùn)動...
    陳果_周綠閱讀 197評論 0 2

友情鏈接更多精彩內(nèi)容