我尝试使用基于pytorch的huggingface bert transformer模型运行基本的推理。然而,似乎我没有以正确的方式调用推理。现在我可以很好地加载模型和分词器,但是推理线给我错误。请注意,在下面的代码中,我实现了两种方法来进行推理,都是从网上找到的,但都给了我同样的错误。那么我错过了什么?
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
model_name = "distilbert-base-uncased"
text = "I just had a really nice dinner"
tokenizer = AutoTokenizer.from_pretrained(model_name)
inputs = tokenizer(text, return_tensors="pt")
id2label = {0: "POSITIVE", 1: "NEGATIVE"}
label2id = {"POSITIVE": 0, "NEGATIVE": 1}
model = AutoModelForSequenceClassification.from_pretrained(
"distilbert-base-uncased",
num_labels=2,
id2label=id2label,
label2id=label2id
)
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
outputs = model.forward(**inputs)
classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
outputs = classifier(text)
predicted_label_index = outputs.logits.argmax(-1).item()
predicted_label = id2label[predicted_label_index]
print(f"The predicted label for the text is: {predicted_label}")
错误消息为:
Traceback (most recent call last):
File "C:\Users\Owner\Desktop\transformers-v4.25-release\inference\infer_classifier_bert.py", line 18, in <module>
outputs = model.forward(**inputs)
File "C:\Users\Owner\Desktop\transformers-v4.25-release\src\transformers\models\distilbert\modeling_distilbert.py", line 775, in forward
logits = self.classifier(pooled_output) # (bs, num_labels)
File "C:\Users\Owner\Desktop\transformers-v4.25-release\src\transformers\generation\utils.py", line 2446, in classifier
model_inputs = self.prepare_inputs_for_generation(input_ids, **model_kwargs)
File "C:\Users\Owner\AppData\Local\Programs\Python\Python38\lib\site-packages\torch\nn\modules\module.py", line 1614, in __getattr__
raise AttributeError("'{}' object has no attribute '{}'".format(
AttributeError: 'DistilBertForSequenceClassification' object has no attribute 'prepare_inputs_for_generation'
Process finished with exit code 1
我在网上查了一下解决方案,但没有人有同样的错误。
1条答案
按热度按时间shyt4zoc1#
首先,尝试升级你的变形金刚版本
如何使用模型的forward函数进行推理?
[out]:
将
SequenceClassifierOutput
转换为dict[out]:
如何使用流水线进行推理?
[out]: