从 Hugging Face 模型加载权重时出错

And*_* NR 2 python error-handling model huggingface-transformers

我正在使用变压器并且我已经加载了一个模型并且它工作正常:

from transformers import AutoModelForSequenceClassification
from transformers import AutoTokenizer

task='sentiment'
MODEL = "cardiffnlp/twitter-roberta-base-{task}"
tokenizer = AutoTokenizer.from_pretrained(MODEL)

# PT
model = AutoModelForSequenceClassification.from_pretrained(MODEL) 
model.save_pretrained(MODEL)
Run Code Online (Sandbox Code Playgroud)

但是如果我尝试加载另一个任务,例如“情感”或“仇恨”,我会收到此错误:

from transformers import AutoModelForSequenceClassification
from transformers import AutoTokenizer

task='emotion'
MODEL = "cardiffnlp/twitter-roberta-base-{task}"
tokenizer = AutoTokenizer.from_pretrained(MODEL)

# PT
model = AutoModelForSequenceClassification.from_pretrained(MODEL)  ## Here I get the error
model.save_pretrained(MODEL)
Run Code Online (Sandbox Code Playgroud)

这个错误:

OSError: Can't load weights for 'cardiffnlp/twitter-roberta-base-emotion'. Make sure that:

- 'cardiffnlp/twitter-roberta-base-emotion' is a correct model identifier listed on 'https://huggingface.co/models'

- or 'cardiffnlp/twitter-roberta-base-emotion' is the correct path to a directory containing a file named one of pytorch_model.bin, tf_model.h5, model.ckpt.
Run Code Online (Sandbox Code Playgroud)

我已经检查过,这些模型实际上存在是拥抱脸部模型,正如您在此处看到的那样,所以我不明白为什么不起作用。

编辑:我注意到,第一次运行它时,它适用于所有任务(仇恨、情感、情绪),但如果我尝试再次运行它,则会收到错误。

小智 6

# PT
model = AutoModelForSequenceClassification.from_pretrained(MODEL) 
model.save_pretrained(MODEL)
tokenizer.save_pretrained(MODEL)
Run Code Online (Sandbox Code Playgroud)

将第三行添加到此部分,重命名或删除已有的模型文件夹,然后重试。这次确实应该不止一次有效。我希望这能让你滚动!