Download pre-trained BERT model locally
Asked Answered
C

2

9

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.

Coh answered 20/7, 2020 at 18:55 Comment(1)
I'm trying out the same thing. Were you able to identify it?Haiku
H
6

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/')
Hotien answered 22/7, 2020 at 13:33 Comment(2)
Hey, The generated config.json doesn't have a version. So I'm getting a KeyError: '__version__'Haiku
@LazyCoder How did you resolved this issue (KeyError: 'version')?Unrounded
D
5

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

Detoxicate answered 24/6, 2021 at 18:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.