【问题标题】:I've downloaded bert pretrained model 'bert-base-cased'. I'm unable to load the model with help of BertTokenizer我已经下载了 bert 预训练模型“bert-base-cased”。我无法在 BertTokenizer 的帮助下加载模型
【发布时间】:2021-03-04 15:50:42
【问题描述】:

我已经下载了 bert 预训练模型 'bert-base-cased.我无法在 BertTokenizer 的帮助下加载模型。我正在尝试使用bert tokenizer。在 bert-pretrained-model 文件夹中,我有 config.json 和 pytorch_model.bin。

tokenizer = BertTokenizer.from_pretrained(r'C:\Downloads\bert-pretrained-model')

我遇到了类似的错误

OSError                                   Traceback (most recent call last)
<ipython-input-17-bd4c0051c48e> in <module>
----> 1 tokenizer = BertTokenizer.from_pretrained(r'\Downloads\bert-pretrained-model')

~\sentiment_analysis\lib\site-packages\transformers\tokenization_utils_base.py in from_pretrained(cls, pretrained_model_name_or_path, *init_inputs, **kwargs)
   1775                 f"- or '{pretrained_model_name_or_path}' is the correct path to a directory containing relevant tokenizer files\n\n"
   1776             )
-> 1777             raise EnvironmentError(msg)
   1778 
   1779         for file_id, file_path in vocab_files.items():

OSError: Can't load tokenizer for 'C:\Downloads\bert-pretrained-model'. Make sure that:

- 'C:\Downloads\bert-pretrained-model' is a correct model identifier listed on 'https://huggingface.co/models'

- or 'C:\Downloads\bert-pretrained-model' is the correct path to a directory containing relevant tokenizer files

当我尝试使用 BertModel 加载时,它正在加载。但是当我尝试使用 BertTokenizer 时,它没有加载。

【问题讨论】:

  • 你错过了vocab.json
  • 我有 config.json,我在哪里可以得到 vocab.json?我正在尝试使用 Hugging Face、Torch 和 Bert 进行情绪分析
  • 我应该只下载 vocab.txt 并将其放在模型文件夹中还是应该下载任何额外的文件?
  • 您还需要tokenizer_config.json

标签: nlp pytorch bert-language-model huggingface-transformers huggingface-tokenizers


【解决方案1】:

你正在使用什么版本的转换器?我有一个类似的问题,解决方案是将转换器升级到最新的(如当前的 4.3.3)版本(我使用的是旧的 2..1 版本,因为我必须运行旧代码)并且它有效。看起来旧版本的转换器在从本地路径加载语言模型时存在这个问题。

我建议在单独的虚拟环境中升级您的转换器,这样您就不会弄乱其他代码。如果您不使用虚拟环境,强烈建议您现在就使用,这是link 中安装和创建虚拟环境(包括 Windows,考虑到您的情况)的一种很好且简单的方法。

替代建议: 这可能不是您问题的答案,但我建议直接使用预训练的语言模型,而不是下载它并指向其本地路径。至少这是拥抱脸的推荐方式。唯一的缺点是当您没有快速互联网时,可能需要一段时间才能加载它。除此之外,包含这行代码而不是您的代码将很容易解决您的问题:

tokenizer = BertTokenizer.from_pretrained("bert-base-cased")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 2020-12-08
    • 1970-01-01
    相关资源
    最近更新 更多