如何保存和加载自定义暹罗伯特模型
我正在遵循有关如何训练暹罗 bert 网络的教程:
https://keras.io/examples /nlp/semantic_similarity_with_bert/
一切都很好,但我不确定在训练并保存模型后保存模型的最佳方法是什么。 有什么建议吗?
我正在尝试使用
model.save('models/bert_siamese_v1')
创建一个包含 save_model.bp keras_metadata.bp 的文件夹和两个子文件夹(变量和资产),
然后我尝试加载它
model.load_weights('models/bert_siamese_v1/')
:给我这个错误:
2022-03-08 14:11:52.567762: W tensorflow/core/util/tensor_slice_reader.cc:95] Could not open models/bert_siamese_v1/: Failed precondition: models/bert_siamese_v1; Is a directory: perhaps your file is in a different file format and you need to use a different restore operator?
最好的方法是什么?
I am following this tutorial on how to train a siamese bert network:
https://keras.io/examples/nlp/semantic_similarity_with_bert/
all good, but I am not sure what is the best way to save the model after train it and save it.
any suggestion?
I was trying with
model.save('models/bert_siamese_v1')
which creates a folder with save_model.bp keras_metadata.bp and two subfolders (variables and assets)
then I try to load it with:
model.load_weights('models/bert_siamese_v1/')
and it gives me this error:
2022-03-08 14:11:52.567762: W tensorflow/core/util/tensor_slice_reader.cc:95] Could not open models/bert_siamese_v1/: Failed precondition: models/bert_siamese_v1; Is a directory: perhaps your file is in a different file format and you need to use a different restore operator?
what is the best way to proceed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用 tf.saved_model.save 保存模型:
保存过程中收到的警告可能会
Try using
tf.saved_model.save
to save your model:The warning you get during saving can apparently be ignored. After loading your model, you can use it for inference
f(test_data)
:看来你有两个选择
调用 model.save 将模型的架构、权重和训练配置保存在单个文件/文件夹中。这允许您导出模型,以便无需访问原始 Python 代码*即可使用它。由于优化器状态已恢复,因此您可以从上次中断的位置继续训练。
保存模型
加载模型
您似乎正在混合两种方法,保存模型和加载权重。
It seems you have two options
Call model.save to save a model's architecture, weights, and training configuration in a single file/folder. This allows you to export a model so it can be used without access to the original Python code*. Since the optimizer-state is recovered, you can resume training from exactly where you left off.
Save model
load model
It seems that you are mixing both approaches, saving model and loading weights.