Search code examples
pythonnlppytorchbert-language-modelhuggingface-transformers

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:

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. Unfortunately, it is not clear to me, how to load the fine-tuned 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. Can anyone help me out here? Thank's a lot!! :)

Edit: I saved the model in a s3 bucket as follows:

# 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())

Solution

  • To load a model with BertModel.from_pretrained() you need to have saved it using save_pretrained() (link).

    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. From then on, you should be able to use from_pretrained() normally.