微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AutoModelForSequenceClassification 需要 PyTorch 库,但在您的环境中找不到

如何解决AutoModelForSequenceClassification 需要 PyTorch 库,但在您的环境中找不到

我正在尝试使用 roberta 转换器和预训练模型,但我不断收到此错误

    ImportError: 
AutoModelForSequenceClassification requires the PyTorch library but it was not found in your environment. Checkout the instructions on the
installation page: https://pytorch.org/get-started/locally/ and follow the ones that match your environment.

这是我的代码

# Tasks:
# emoji,emotion,hate,irony,offensive,sentiment
# stance/abortion,stance/atheism,stance/climate,stance/feminist,stance/hillary

task='sentiment'
MODEL = f"cardiffnlp/twitter-roberta-base-{task}"

tokenizer = AutoTokenizer.from_pretrained(MODEL)
# download label mapping
labels=[]
mapping_link = f"https://raw.githubusercontent.com/cardiffnlp/tweeteval/main/datasets/{task}/mapping.txt"
model = AutoModelForSequenceClassification.from_pretrained(MODEL)
model.save_pretrained(MODEL)
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]

我确保 PyTorch 已安装并正常工作:

enter image description here

解决方法

我遇到了同样的问题。我通过重新启动内核解决了这个问题。

,

在导入 HuggingFace Transformer 之前,您需要先安装 Pytorch。

否则你将不得不重新启动内核

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。