【问题标题】:Huggingface models only work once, then spit out Tokenizer errorHuggingface 模型只工作一次,然后吐出 Tokenizer 错误
【发布时间】:2022-05-02 22:56:01
【问题描述】:

我正在关注 huggingface 网站上的 this 示例,尝试处理 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”的文件夹,其中包含与不同任务对应的子文件夹,例如我的 PyCharm 项目文件夹中的“twitter-roberta-base-sentiment”,就在我的“venv”上方文件夹。但是,如果我删除第一次成功运行代码时创建的“twitter-roberta-base-sentiment”文件夹,代码将正常工作,并且“twitter-roberta-base-sentiment”文件夹将再次出现。

我的猜测是这部分代码正在下载模型并将其保存到 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]

感谢各位的帮助。

【问题讨论】:

    标签: model pycharm huggingface-tokenizers huggingface-datasets


    【解决方案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])
    

    这对我有用,将“文本”替换为您想要表达情感的字符串。这被埋在文档中,幸运的是。

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

    【讨论】:

      猜你喜欢
      • 2020-10-02
      • 1970-01-01
      • 2022-01-22
      • 2015-11-22
      • 2018-07-28
      • 2022-01-14
      • 2018-06-18
      相关资源
      最近更新 更多