簡體   English   中英

如何加載在 google colab 上訓練的模型

[英]How to load a model trained on google colab

在訓練我使用保存的模型后,我正在按照本教程嘗試使用谷歌的 BERT 進行文本分類

# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")

我下載了模型並嘗試如下加載它們

json_file = open(os.path.join(self.root, 'model.json'), 'r')
        loaded_model_json = json_file.read()
        json_file.close()

        cs = get_custom_objects()
        cs['GlorotNormal'] = tf.keras.initializers.glorot_normal()
        cs['GlorotUniform'] = tf.keras.initializers.glorot_uniform()

        model = model_from_json(loaded_model_json, custom_objects=cs)
        print('loaded model json')
        # load weights into new model
        model.load_weights(os.path.join(self.root, 'model.h5'))

如果我從模型中刪除了 'ragged',函數mdoel_from_json掛起並且tf.keras.models.model_from_json會拋出ValueError: Unknown initializer: GlorotNormal加載模型的正確方法是什么?

在導入tensorflow之前,我必須添加os.environ['TF_KERAS'] = '1'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM