【问题标题】:Error loading weights from a Hugging Face model从 Hugging Face 模型加载权重时出错
【发布时间】:2021-07-15 13:50:41
【问题描述】:

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

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)

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

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)

这个错误:

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.

我已经检查过了,这些模型实际上是 Hugging Face 模型,你可以看到 here,所以我不明白为什么不工作。

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

【问题讨论】:

  • 我无法用 4.5.1 重现这个。您使用的是哪个变压器版本?如果您还没有使用 4.5.1,可以尝试升级它吗?
  • 我使用的是 3.1.0 版本。现在有了 4.5.1。我没有问题,代码运行。谢谢@cronoik
  • 编辑:使用 4.5.1。版本我也得到了错误。错误出现在我第二次运行代码时,从未在开始时出现。

标签: python error-handling model huggingface-transformers


【解决方案1】:
# PT
model = AutoModelForSequenceClassification.from_pretrained(MODEL) 
model.save_pretrained(MODEL)
tokenizer.save_pretrained(MODEL)

将第三行添加到此部分,重命名或删除您已有的模型文件夹,然后重试。这一次它确实应该不止一次地工作。我希望这能让你继续前进!

【讨论】:

  • 有效!你能解释一下为什么吗?为什么我不能第二次下载分词器?
  • 这不是我自己非常熟悉的东西,但是我在使用一些类似模型时遇到过几次问题是,如果你不保存 config/tokenizer/model(取决于关于每个使用的内容)这样他们都会抛出类似的错误。我假设这只是因为您在第二次运行它时只有一些代码想要在本地存储,所以它会恐慌并放弃。
  • 解决方案同样适用于TF版本...
猜你喜欢
  • 2020-10-19
  • 2021-08-10
  • 2021-06-23
  • 2021-11-19
  • 2021-03-10
  • 2021-10-22
  • 1970-01-01
  • 2022-01-15
  • 2021-09-06
相关资源
最近更新 更多