拥抱脸模型只工作一次,然后吐出Tokenizer错误



我在huggingface的网站上跟随这个例子,试图处理twitter情绪。我在PyCharm上运行python 3.9。代码在我第一次运行时运行得很好,但是如果我尝试再次运行代码而不做任何更改,我会得到以下错误:

OSError: Can't load tokenizer 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'
(make sure 'cardiffnlp/twitter-roberta-base-emotion' is not a path to a local directory with something else, in that case)
- or 'cardiffnlp/twitter-roberta-base-emotion' is the correct path to a directory containing relevant tokenizer file,

我注意到的一件事是Pycharm将创建一个名为"的文件夹;cardiffnlp";具有对应于不同任务的子文件夹;twitter roberta基本情绪&";在我的PyCharm项目文件夹中;venv";文件夹然而,如果我删除";twitter roberta基本情绪&";在代码第一次成功运行时创建的文件夹,代码将正常工作,并且";twitter roberta基本情绪&";文件夹将再次显示。

我的猜测是,这部分代码是将模型下载并保存到Pycharm。我只是不明白为什么它只是第一次起作用。我是否需要更改模型位置,因为如果文件已经存储在本地,则无需登录到URL即可获取该文件?

# download label mapping
labels=[]
mapping_link = f"https://raw.githubusercontent.com/cardiffnlp/tweeteval/main/datasets/{task}/mapping.txt"
with urllib.request.urlopen(mapping_link) as f:
html = f.read().decode('utf-8').split("n")
csvreader = csv.reader(html, delimiter='t')
labels = [row[1] for row in csvreader if len(row) > 1]

谢谢那些帮忙的人。

import torch
from transformers import RobertaTokenizer, RobertaForSequenceClassification
tokenizer = RobertaTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-emotion")
model = RobertaForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-emotion")
inputs = tokenizer(text, return_tensors="pt")
with torch.no_grad():
logits = model(**inputs).logits
predicted_class_id = logits.argmax().item()
print(model.config.id2label[predicted_class_id])

这对我很有效,用你想要的情感字符串代替"text"。这是埋在文件和幸运的工作。

https://huggingface.co/docs/transformers/main/en/model_doc/roberta#transformers.RobertaForSequenceClassification.

相关内容

最新更新