python - 如何使用微调的 BERT 模型进行句子编码?
问题描述
我按照这里的脚本在我自己的数据集上微调了 BERT 基础模型:
https://github.com/cedrickchee/pytorch-pretrained-BERT/tree/master/examples/lm_finetuning
我将模型保存为.pt
文件,现在我想将其用于句子相似性任务。不幸的是,我不清楚如何加载微调模型。我尝试了以下方法:
model = BertModel.from_pretrained('trained_model.pt')
model.eval()
这行不通。它说:
ReadError: not a gzip file
显然,使用该方法加载.pt
文件from_pretrained
是不可能的。有谁可以帮我离开这里吗?非常感谢!!:)
编辑:我将模型保存在 s3 存储桶中,如下所示:
# Convert model to buffer
buffer = io.BytesIO()
torch.save(model, buffer)
# Save in s3 bucket
output_model_file = output_folder + "trained_model.pt"
s3_.put_object(Bucket="power-plant-embeddings", Key=output_model_file, Body=buffer.getvalue())
解决方案
推荐阅读
- python - 将齐次坐标(x0 = 1)添加到numpy中的图像
- java - 使用库 github.barteksc.pdfviewer 在我的应用程序中加载 PDF 时出现错误
- ruby-on-rails - 如何验证从我网站上的另一个网站发送的查询字符串
- java - MediaRecorder stop() 失败
- laravel - 如何从 return()->redirect()->back() 传递数据?
- javascript - 为什么我的函数在 javascript 中不起作用?
- c# - 多个连接字符串一一运行c#
- php - 生成新用户名并检查是否已经存在
- angular - 角度多个相同的孩子 - 传递不同的数据
- docker - 可以使用 traefik/docker 进行蓝/绿部署吗?