使用拥抱面模型提取方面和情感进行基于方面的情感分析 yangheng

问题描述 投票:0回答:1

我是变形金刚模型的新手,试图提取句子的方面和情感但遇到问题

from transformers import AutoTokenizer, AutoModelForSequenceClassification

model_name = "yangheng/deberta-v3-base-absa-v1.1"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
text = "The food was great but the service was terrible."
inputs = tokenizer(text, return_tensors="pt")
outputs = model(**inputs)


我能够得到张量我需要的是提取整个句子的方面和情感的输出

我试过了但是出错了

sentiment_scores = outputs.logits.softmax(dim=1)
aspect_scores = sentiment_scores[:, 1:-1]

aspects = [tokenizer.decode([x]) for x in inputs["input_ids"].squeeze()][1:-1]
sentiments = ['Positive' if score > 0.5 else 'Negative' for score in aspect_scores.squeeze()]

for aspect, sentiment in zip(aspects, sentiments):
    print(f"{aspect}: {sentiment}")

我正在寻找以下 o/p 或类似的 o/p

我无法写出如何提取 aspect 和 sentiment 的逻辑

text -The food was great but the service was terrible

aspect- food ,sentiment positive
aspect - service, sentiment negative


or at overall level

aspect - food, sentiment positive

python nlp huggingface-transformers sentiment-analysis
1个回答
0
投票

您尝试使用的模型基于文本预测给定方面的情绪。这意味着,它需要

text
aspect
来执行预测:

import torch
import torch.nn.functional as F
from transformers import AutoTokenizer, AutoModelForSequenceClassification

model_name = "yangheng/deberta-v3-base-absa-v1.1"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

aspects = ["food", "service"]
text = "The food was great but the service was terrible."
sentiment_aspect = {}
for aspect in aspects:
  inputs = tokenizer(text, aspect, return_tensors="pt")

  with torch.inference_mode():
    outputs = model(**inputs)

  scores = F.softmax(outputs.logits[0], dim=-1)
  label_id = torch.argmax(scores).item()
  sentiment_aspect[aspect] = (model.config.id2label[label_id], scores[label_id].item())

print(sentiment_aspect)

输出:

{'food': ('Positive', 0.9973154664039612), 'service': ('Negative', 0.9935430288314819)}
© www.soinside.com 2019 - 2024. All rights reserved.