Can’t load the configuration of ‘xxxxxx’. If you were trying to load it from ‘https://huggingface.co/models’, make sure you don’t have a local directory with the same name. Otherwise, make sure ‘xxxxxx’ is the correct path to a directory containing a config.json file
from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
model = AutoModel.from_pretrained("bert-base-uncased")
tokenizer.save_pretrained('./bert/')
model.save_pretrained('./bert/')
Loading model from pytorch_pretrained_bert into transformers library
查看Huggingface官方Discussion帖子Loading model from pytorch_pretrained_bert into transformers library,有这样一段话:
Hi. This is probably caused by the transformer verison. You might downgrade your transformer version from 4.4 to 2.8 with pip install transformers==2.8.0
因此尝试将transformers版本降到2.8.0。
首先查看transformers版本:
pip show transformers
输出信息显示版本为4.26.1:
Name: transformers
Version: 4.26.1
Summary: State-of-the-art Machine Learning for JAX, PyTorch and TensorFlow
Home-page: https://github.com/huggingface/transformers
Author: The Hugging Face team (past and future) with the help of all our contributors (https://github.com/huggingface/transformers/graphs/contributors)
Author-email: transformers@huggingface.co
License: Apache
Location: xxxxxxxxxxxxxxxxxxxxxxx
Requires: filelock, huggingface-hub, importlib-metadata, numpy, packaging, pyyaml, regex, requests, tokenizers, tqdm
Required-by:
遇到一串错误,其中一行是:
During handling of the above exception, another exception occurred:
卸载transformers:
pip uninstall transformers
随后安装:
pip install transformers==2.8.0
遇到错误:
ERROR: Could not find a version that satisfies the requirement boto3 (from transformers) (from versions: none)
ERROR: No matching distribution found for boto3
Missing key(s) in state_dict: “bert.embeddings.position_ids”.
安装2.8.0版本的transformers库后,运行程序报错:
Missing key(s) in state_dict: “bert.embeddings.position_ids”.
参考这篇博客稍加改造后,加入以下代码:
cudnn.benchmark = True
仍然报错:
TypeError: ‘BertTokenizer’ object is not callable
检索到GitHub的一个相关Issue:TypeError: ‘BertTokenizer’ object is not callable #53,该Issue的回复指出:
Transformers fails “TypeError: ‘BertTokenizer’ object is not callable” if the installed version is <v3.0.0 . In the requirements file, transformers should be “transformers>=3.0.0”
运行程序,可以得到结果,伴随着输出如下内容:
Some weights of the model checkpoint at ./bert/ were not used when initializing BertModel: [‘embeddings.position_ids’]
– This IS expected if you are initializing BertModel from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPretraining model).
– This IS NOT expected if you are initializing BertModel from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).