如何使用微调的BERT模型进行句子编码?
How to use fine-tuned BERT model for sentence encoding?
我按照此处的脚本在我自己的数据集上微调了 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
很显然,无法使用 from_pretrained
方法加载 .pt
文件。有人可以帮我从这里出去吗?非常感谢!! :)
编辑:我将模型保存在 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())
我按照此处的脚本在我自己的数据集上微调了 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
很显然,无法使用 from_pretrained
方法加载 .pt
文件。有人可以帮我从这里出去吗?非常感谢!! :)
编辑:我将模型保存在 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())