如何保存和加载自定义暹罗伯特模型

发布于 2025-01-12 16:48:58 字数 812 浏览 2 评论 0原文

我正在遵循有关如何训练暹罗 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丘比特射中我 2025-01-19 16:48:58

尝试使用 tf.saved_model.save 保存模型:

tf.saved_model.save(model, 'models/bert_siamese_v1')
model = tf.saved_model.load('models/bert_siamese_v1')

保存过程中收到的警告可能会

f = model.signatures["serving_default"]
x1 = tf.random.uniform((1, 128), maxval=100, dtype=tf.int32)
x2 = tf.random.uniform((1, 128), maxval=100, dtype=tf.int32)
x3 = tf.random.uniform((1, 128), maxval=100, dtype=tf.int32)
print(f)
print(f(attention_masks = x1, input_ids = x2, token_type_ids = x3))
ConcreteFunction signature_wrapper(*, token_type_ids, attention_masks, input_ids)
  Args:
    attention_masks: int32 Tensor, shape=(None, 128)
    input_ids: int32 Tensor, shape=(None, 128)
    token_type_ids: int32 Tensor, shape=(None, 128)
  Returns:
    {'dense': <1>}
      <1>: float32 Tensor, shape=(None, 3)
{'dense': <tf.Tensor: shape=(1, 3), dtype=float32, numpy=array([[0.40711606, 0.13456087, 0.45832306]], dtype=float32)>}

Try using tf.saved_model.save to save your model:

tf.saved_model.save(model, 'models/bert_siamese_v1')
model = tf.saved_model.load('models/bert_siamese_v1')

The warning you get during saving can apparently be ignored. After loading your model, you can use it for inference f(test_data):

f = model.signatures["serving_default"]
x1 = tf.random.uniform((1, 128), maxval=100, dtype=tf.int32)
x2 = tf.random.uniform((1, 128), maxval=100, dtype=tf.int32)
x3 = tf.random.uniform((1, 128), maxval=100, dtype=tf.int32)
print(f)
print(f(attention_masks = x1, input_ids = x2, token_type_ids = x3))
ConcreteFunction signature_wrapper(*, token_type_ids, attention_masks, input_ids)
  Args:
    attention_masks: int32 Tensor, shape=(None, 128)
    input_ids: int32 Tensor, shape=(None, 128)
    token_type_ids: int32 Tensor, shape=(None, 128)
  Returns:
    {'dense': <1>}
      <1>: float32 Tensor, shape=(None, 3)
{'dense': <tf.Tensor: shape=(1, 3), dtype=float32, numpy=array([[0.40711606, 0.13456087, 0.45832306]], dtype=float32)>}
暮光沉寂 2025-01-19 16:48:58

看来你有两个选择

 model.save_weights('./checkpoints/my_checkpoint')
   
 model = create_model()
   
 model.load_weights('./checkpoints/my_checkpoint') 

调用 model.save 将模型的架构、权重和训练配置保存在单个文件/文件夹中。这允许您导出模型,以便无需访问原始 Python 代码*即可使用它。由于优化器状态已恢复,因此您可以从上次中断的位置继续训练。

保存模型

# Create and train a new model instance.
model = create_model()
model.fit(train_images, train_labels, epochs=5)

# Save the entire model as a SavedModel.
!mkdir -p saved_model
model.save('saved_model/my_model')

加载模型

new_model = tf.keras.models.load_model('saved_model/my_model')

您似乎正在混合两种方法,保存模型和加载权重。

It seems you have two options

 model.save_weights('./checkpoints/my_checkpoint')
   
 model = create_model()
   
 model.load_weights('./checkpoints/my_checkpoint') 

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

# Create and train a new model instance.
model = create_model()
model.fit(train_images, train_labels, epochs=5)

# Save the entire model as a SavedModel.
!mkdir -p saved_model
model.save('saved_model/my_model')

load model

new_model = tf.keras.models.load_model('saved_model/my_model')

It seems that you are mixing both approaches, saving model and loading weights.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文