Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download pre-trained BERT model locally

I am using the SentenceTransformers library (here: https://pypi.org/project/sentence-transformers/#pretrained-models) for creating embeddings of sentences using the pretrained model bert-base-nli-mean-tokens. I have an application that will be deployed to a device that does not have internet access. How can I save this model locally so that when I call it, it loads the model locally, rather than attempting to download from the internet? As the library maintainers make clear, the method SentenceTransformer downloads the model from the internet (see here: https://pypi.org/project/sentence-transformers/#pretrained-models) and I cannot find a method for saving the model locally.

like image 834
Josh Avatar asked Jul 20 '20 18:07

Josh


2 Answers

Hugging face usage

You can download the models locally by using the Hugging Face transformer library method.

from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/bert-base-nli-mean-tokens")
model = AutoModel.from_pretrained("sentence-transformers/bert-base-nli-mean-tokens")
tokenizer.save_pretrained('./local_directory/')
model.save_pretrained('./local_directory/')
like image 120
Shivan Avatar answered Oct 18 '22 06:10

Shivan


After instantiating the SentenceTransformer via download, you can then save it to any path of your choosing with the 'save()' method.

model = SentenceTransformer('distilbert-base-nli-stsb-mean-tokens')
model.save('/my/local/directory/for/models/')

The accepted answer doesn't work, as it doesn't have the encapsulating folder and config.json that SentenceTransformer is looking for

like image 29
Wes Smith Avatar answered Oct 18 '22 06:10

Wes Smith