简体   繁体   English

如何使用微调的 BERT model 进行句子编码?

[英]How to use fine-tuned BERT model for sentence encoding?

I fine-tuned the BERT base model on my own dataset following the script here:我按照此处的脚本在我自己的数据集上微调了 BERT 基础 model:

https://github.com/cedrickchee/pytorch-pretrained-BERT/tree/master/examples/lm_finetuning https://github.com/cedrickchee/pytorch-pretrained-BERT/tree/master/examples/lm_finetuning

I saved the model as a .pt file and I want to use it now for a sentence similarity task.我将 model 保存为.pt文件,现在我想将其用于句子相似性任务。 Unfortunately, it is not clear to me, how to load the fine-tuned model.不幸的是,我不清楚如何加载经过微调的 model。 I tried the following:我尝试了以下方法:

model = BertModel.from_pretrained('trained_model.pt')
model.eval()

This doesn't work.这行不通。 It says:它说:

ReadError: not a gzip file

So apparently, loading a .pt file with the from_pretrained method is not possible.显然,使用from_pretrained方法加载.pt文件是不可能的。 Can anyone help me out here?有谁可以帮我离开这里吗? Thank's a lot:!非常感谢:! :) :)

Edit: I saved the model in a s3 bucket as follows:编辑:我将 model 保存在 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())

To load a model with BertModel.from_pretrained() you need to have saved it using save_pretrained() (link) .要使用BertModel.from_pretrained()加载 model,您需要使用save_pretrained() (链接)保存它。

Any other storage method would require the corresponding load.任何其他存储方法都需要相应的负载。 I am not familiar with S3, but I assume you can use get_object (link) to retrieve the model, and then save it using the huggingface api.我不熟悉 S3,但我假设您可以使用get_object (link)来检索 model,然后使用 huggingface api 保存它。 From then on, you should be able to use from_pretrained() normally.从那时起,您应该可以正常使用from_pretrained()了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在另一个数据集上微调我训练的 model (bert) - How Fine-tuned my trained model (bert) on another dataset 保存“微调”的 bert 模型 - Saving a 'fine-tuned' bert model 如何在重新加载后在拥抱脸中使用微调的 model 进行实际预测? - How to use fine-tuned model in huggingface for actual prediction after re-loading? 将 LIME 解释应用到我为序列分类模型微调的 BERT 上? - Applying LIME interpretation on my fine-tuned BERT for sequence classification model? 在预训练的 BERT 上进行微调后如何导出/保存文本分类器 - How to export/save a text classifier after fine-tuned on a pre-trained BERT 使用微调的Inception-v3模型预测单个图像 - Use fine-tuned Inception-v3 model to predict on a single image 我们如何将字符串列表传递给经过微调的 bert model? - How can we pass a list of strings to a fine tuned bert model? 如何使用预训练模型进行文本分类?比较经过微调的 model 与未经微调的预训练 model - How to use pre-trained models for text classification?Comparing a fine-tuned model with a pre-trained model without fine-tuning 在 Docker 容器中加载微调的 simpletransformer model 时出错 - Error while loading fine-tuned simpletransformer model in Docker Container 如何获得微调的 TFBertModel 的隐藏状态? - How to get the hidden states of a fine-tuned TFBertModel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM