Tensorflow 如何加载在google colab上训练的模型

Tensorflow 如何加载在google colab上训练的模型,tensorflow,keras,google-colaboratory,Tensorflow,Keras,Google Colaboratory,在培训了我使用的模型之后,我尝试使用google的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&qu

在培训了我使用的模型之后,我尝试使用google的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”,那么json中的函数
mdoel\u将挂起
json中的
tf.keras.models.model\u抛出
ValueError:未知初始值设定项:GlorotNormal

加载模型的正确方法是什么?

在导入
tensorflow之前,我必须添加
os.environ['TF_KERAS']=“1”
,为什么不直接导入
model.save(“model.h5”)
和使用
keras.models.load\u model
?我记得我很久以前就试过了,遇到了不同的问题,建议分别保存模型和重量