pytorch Huggingface变压器序列分类推断错误-无属性“prepare_inputs_for_generation”

taor4pac  于 2023-03-23  发布在  其他
关注(0)|答案(1)|浏览(181)

我尝试使用基于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

我在网上查了一下解决方案,但没有人有同样的错误。

shyt4zoc

shyt4zoc1#

首先,尝试升级你的变形金刚版本

pip install -U transformers>=4.27.1

如何使用模型的forward函数进行推理?

import torch

from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline

model_name = "distilbert-base-uncased"

tokenizer = AutoTokenizer.from_pretrained(model_name)

id2label = {0: "POSITIVE", 1: "NEGATIVE"}
label2id = {"POSITIVE": 0, "NEGATIVE": 1}
model = AutoModelForSequenceClassification.from_pretrained(
    model_name,
    num_labels=2,
    id2label=id2label,
    label2id=label2id
)

text = "I just had a really nice dinner"

inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)

with torch.no_grad():
  outputs = model.forward(**inputs)

print(outputs)

[out]:

SequenceClassifierOutput(loss=None, logits=tensor([[-0.0663, -0.1213]]), hidden_states=None, attentions=None)
SequenceClassifierOutput转换为dict
id2label = {0: "POSITIVE", 1: "NEGATIVE"}

with torch.no_grad():
  outputs = model.forward(**inputs)

  output_labels = [{'label': id2label[int(label)], 'score':float(max(score))} for label, score 
in zip(torch.argmax(outputs.logits, 1),  torch.softmax(outputs.logits, 1))]

  print(output_labels)

[out]:

[{'label': 'POSITIVE', 'score': 0.5391089916229248}]

如何使用流水线进行推理?

from transformers import pipeline

model_name = "distilbert-base-uncased"

classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
outputs = classifier(text)

print(outputs)

[out]:

[{'label': 'POSITIVE', 'score': 0.5137302875518799}]

相关问题