【问题标题】:Download pre-trained sentence-transformers model locally在本地下载预训练的句子转换器模型
【发布时间】:2021-04-01 19:04:13
【问题描述】:

我正在使用 SentenceTransformers 库(此处:https://pypi.org/project/sentence-transformers/#pretrained-models)使用预训练模型 bert-base-nli-mean-tokens 创建句子嵌入。我有一个应用程序将部署到无法访问 Internet 的设备上。在这里,已经回答了,如何保存模型Download pre-trained BERT model locally。然而,我一直无法从本地保存的路径加载保存的模型。

当我尝试使用上述技术保存模型时,这些是输出文件:

('/bert-base-nli-mean-tokens/tokenizer_config.json',
 '/bert-base-nli-mean-tokens/special_tokens_map.json',
 '/bert-base-nli-mean-tokens/vocab.txt',
 '/bert-base-nli-mean-tokens/added_tokens.json')

当我尝试将其加载到内存中时,使用

tokenizer = AutoTokenizer.from_pretrained(to_save_path)

我来了

Can't load config for '/bert-base-nli-mean-tokens'. Make sure that:

- '/bert-base-nli-mean-tokens' is a correct model identifier listed on 'https://huggingface.co/models'

- or '/bert-base-nli-mean-tokens' is the correct path to a directory containing a config.json 

【问题讨论】:

  • 嘿,你能解决这个问题吗?
  • 是的,解决了
  • 你能更新解决方案吗

标签: word-embedding bert-language-model huggingface-tokenizers sentence-transformers


【解决方案1】:

您可以像这样下载和加载模型

from sentence_transformers import SentenceTransformer
modelPath = "local/path/to/model

model = SentenceTransformer('bert-base-nli-stsb-mean-tokens')
model.save(modelPath)
model = SentenceTransformer(modelPath)

这对我有用。您可以查看 SBERT 文档以了解 SentenceTransformer 类的模型详细信息 [此处][1]

[1]:https://www.sbert.net/docs/package_reference/SentenceTransformer.html#:~:text=class,Optional%5Bstr%5D%20%3D%20None)

【讨论】:

    【解决方案2】:

    有很多方法可以解决这个问题:

    • 假设您已经在本地(colab/notebook)训练了您的 BERT 基础模型,以便将其与 Huggingface AutoClass 一起使用,然后是模型(以及标记器、vocab.txt、配置、特殊标记和 tf/pytorch weights) 必须上传到 Huggingface。提到了执行此操作的步骤here。上传后,将使用您的用户名创建一个存储库,然后可以通过以下方式访问该模型:
    from transformers import AutoTokenizer
    from transformers import pipeline
    
    tokenizer = AutoTokenizer.from_pretrained("<username>/<model-name>")
    
    • 第二种方法是在本地使用经过训练的模型,这可以通过使用pipelines 来完成。以下是如何在本地为您的用例使用此模型训练(&保存)的示例(给出示例来自我在本地训练的 QA 模型):
    from transformers import AutoModelForQuestionAnswering,AutoTokenizer,pipeline
    nlp_QA=pipeline('question-answering',model='./abhilash1910/distilbert-squadv1',tokenizer='./abhilash1910/distilbert-squadv1')
    QA_inp={
        'question': 'What is the fund price of Huggingface in NYSE?',
        'context': 'Huggingface Co. has a total fund price of $19.6 million dollars'
    }
    result=nlp_QA(QA_inp)
    result
    

    还有其他方法可以解决此问题,但这些方法可能会有所帮助。这个预训练模型列表也可能是help

    【讨论】:

    • 我希望使用预训练的 bert-base-nli-mean-tokens 模型。第三种选择不可行,因为我无法访问本地系统。我应该能够保存一次(从互联网下载),然后,它应该从系统加载而无需任何互联网访问。 from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/bert-base-nli-mean-tokens") tokenizer.save_pretrained(local_path) loaded_tokenizer = AutoTokenizer.from_pretrained(local_path) 当我加载模型时,我得到了上面提到的错误
    猜你喜欢
    • 2022-10-20
    • 2023-03-10
    • 2021-02-19
    • 1970-01-01
    • 2017-08-19
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多