AutoTokenizer.from_pretrained无法加载本地保存的预训练令牌生成器(PyTorch)

时间:2020-06-19 14:17:45

标签: python deep-learning pytorch huggingface-transformers huggingface-tokenizers

我是PyTorch的新手,最近,我一直在尝试与《变形金刚》合作。我正在使用HuggingFace提供的预训练标记器。
我成功下载并运行了它们。但是,如果我尝试保存它们并再次加载,则会发生一些错误。
如果我使用AutoTokenizer.from_pretrained下载令牌生成器,那么它将起作用。

[1]:    tokenizer = AutoTokenizer.from_pretrained('distilroberta-base')
        text = "Hello there"
        enc = tokenizer.encode_plus(text)
        enc.keys()

Out[1]: dict_keys(['input_ids', 'attention_mask'])

但是,如果我使用tokenizer.save_pretrained("distilroberta-tokenizer")保存它并尝试在本地加载它,那么它将失败。

[2]:    tmp = AutoTokenizer.from_pretrained('distilroberta-tokenizer')


---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
/opt/conda/lib/python3.7/site-packages/transformers/configuration_utils.py in get_config_dict(cls, pretrained_model_name_or_path, **kwargs)
    238                 resume_download=resume_download,
--> 239                 local_files_only=local_files_only,
    240             )

/opt/conda/lib/python3.7/site-packages/transformers/file_utils.py in cached_path(url_or_filename, cache_dir, force_download, proxies, resume_download, user_agent, extract_compressed_file, force_extract, local_files_only)
    266         # File, but it doesn't exist.
--> 267         raise EnvironmentError("file {} not found".format(url_or_filename))
    268     else:

OSError: file distilroberta-tokenizer/config.json not found

During handling of the above exception, another exception occurred:

OSError                                   Traceback (most recent call last)
<ipython-input-25-3bd2f7a79271> in <module>
----> 1 tmp = AutoTokenizer.from_pretrained("distilroberta-tokenizer")

/opt/conda/lib/python3.7/site-packages/transformers/tokenization_auto.py in from_pretrained(cls, pretrained_model_name_or_path, *inputs, **kwargs)
    193         config = kwargs.pop("config", None)
    194         if not isinstance(config, PretrainedConfig):
--> 195             config = AutoConfig.from_pretrained(pretrained_model_name_or_path, **kwargs)
    196 
    197         if "bert-base-japanese" in pretrained_model_name_or_path:

/opt/conda/lib/python3.7/site-packages/transformers/configuration_auto.py in from_pretrained(cls, pretrained_model_name_or_path, **kwargs)
    194 
    195         """
--> 196         config_dict, _ = PretrainedConfig.get_config_dict(pretrained_model_name_or_path, **kwargs)
    197 
    198         if "model_type" in config_dict:

/opt/conda/lib/python3.7/site-packages/transformers/configuration_utils.py in get_config_dict(cls, pretrained_model_name_or_path, **kwargs)
    250                 f"- or '{pretrained_model_name_or_path}' is the correct path to a directory containing a {CONFIG_NAME} file\n\n"
    251             )
--> 252             raise EnvironmentError(msg)
    253 
    254         except json.JSONDecodeError:

OSError: Can't load config for 'distilroberta-tokenizer'. Make sure that:

- 'distilroberta-tokenizer' is a correct model identifier listed on 'https://huggingface.co/models'

- or 'distilroberta-tokenizer' is the correct path to a directory containing a config.json file

说目录中缺少'config.josn'。在检查目录时,我正在获取这些文件的列表:

[3]:    !ls distilroberta-tokenizer

Out[3]: merges.txt  special_tokens_map.json  tokenizer_config.json  vocab.json

我知道此问题已在较早之前发布,但是它们似乎都没有起作用。我也尝试遵循docs,但仍然无法使其正常工作。
任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

我在下面列出的代码中看到了几个问题:

  1. distilroberta-tokenizer是一个包含vocab配置等文件的目录。请确保首先创建此目录。

  2. 如果此目录包含config.json而不是NOT tokenizer_config.json,则使用AutoTokenizer起作用。因此,请重命名该文件。

我在下面修改了您的代码,它可以正常工作。

dir_name = "distilroberta-tokenizer"

if os.path.isdir(dir_name) == False:
    os.mkdir(dir_name)  

tokenizer.save_pretrained(dir_name)

#Rename config file now

#tmp = AutoTokenizer.from_pretrained(dir_name)   

我希望这会有所帮助!

谢谢!

答案 1 :(得分:1)

目前有一个issue正在调查中,该问题仅影响自动令牌生成器,而不影响诸如(RobertaTokenizer)之类的基础令牌生成器。例如,以下方法应该起作用:

from transformers import RobertaTokenizer

tokenizer = RobertaTokenizer.from_pretrained('YOURPATH')

要使用自动令牌生成器,您还需要保存配置以离线加载它:

from transformers import AutoTokenizer, AutoConfig

tokenizer = AutoTokenizer.from_pretrained('distilroberta-base')
config = AutoConfig.from_pretrained('distilroberta-base')

tokenizer.save_pretrained('YOURPATH')
config.save_pretrained('YOURPATH')

tokenizer = AutoTokenizer.from_pretrained('YOURPATH')

我建议 为令牌生成器和模型使用不同的路径,或者保留模型的config.json,因为对模型进行的一些修改会会存储在model.save_pretrained()期间创建的config.json中,并且在模型保存后如上述保存令牌生成器时将被覆盖(即,您将无法使用令牌生成器config.json加载修改后的模型)