UER-py icon indicating copy to clipboard operation
UER-py copied to clipboard

您好,加载“chinese_roberta_L-12_H-128”预训练模型时,报错,说是不能加载pytorch的checkpoint权重,请问这是哪里的问题呢

Open PearlW1 opened this issue 4 years ago • 1 comments

from transformers import BertTokenizer, BertModel tokenizer = BertTokenizer.from_pretrained('./chinese_roberta_L-12_H-128/vocab.txt') model = BertModel.from_pretrained("./chinese_roberta_L-12_H-128/config.json") text = "用你喜欢的任何文本替换我。" encoded_input = tokenizer(text, return_tensors='pt') output = model(**encoded_input) print(output) 报错内容: `Calling BertTokenizer.from_pretrained() with the path to a single file or url is deprecated Traceback (most recent call last): File "/Users/wang/anaconda3/envs/torch/lib/python3.6/site-packages/transformers/modeling_utils.py", line 951, in from_pretrained state_dict = torch.load(resolved_archive_file, map_location="cpu") File "/Users/wang/anaconda3/envs/torch/lib/python3.6/site-packages/torch/serialization.py", line 595, in load return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args) File "/Users/wang/anaconda3/envs/torch/lib/python3.6/site-packages/torch/serialization.py", line 764, in _legacy_load magic_number = pickle_module.load(f, **pickle_load_args) _pickle.UnpicklingError: invalid load key, '{'.

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/Users/wang/bigdata/feedrec/LM_embedding/demo.py", line 9, in model = BertModel.from_pretrained("./chinese_roberta_L-12_H-128/config.json") File "/Users/wang/anaconda3/envs/torch/lib/python3.6/site-packages/transformers/modeling_utils.py", line 954, in from_pretrained "Unable to load weights from pytorch checkpoint file. " OSError: Unable to load weights from pytorch checkpoint file. If you tried to load a PyTorch model from a TF 2.0 checkpoint, please set from_tf=True. `

当前环境下,加载其他预训练模型是没有问题的,比如: 运行: python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('we love you'))" 会输出:[{'label': 'POSITIVE', 'score': 0.9998704791069031}]

PearlW1 avatar Jan 22 '21 08:01 PearlW1

您好,加载模型的时候只用指定到相应的文件夹就可以了。 tokenizer = BertTokenizer.from_pretrained('./chinese_roberta_L-12_H-128') model = BertModel.from_pretrained("./chinese_roberta_L-12_H-128") 您可以尝试一下能否解决问题。

hhou435 avatar Jan 25 '21 15:01 hhou435