2

I am new to transformers models and trying to extract aspect and sentiment for a sentence but having issues

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)


I am able to get the tensor what I need is the output to extract the aspect and sentiment for the overall sentence

I tried this however getting error

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}")

I am looking for below o/p or similar o/p

I am unable to write the logic as to how extract aspect and 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

PeCaDe
  • 277
  • 1
  • 8
  • 33
Dexter1611
  • 492
  • 1
  • 4
  • 15
  • Seems like the model you are using performs absa only for given aspects. That means it was trained with input sentences of the following structure: `[CLS] when tables opened up, the manager sat another party before us. [SEP] manager [SEP] `. – cronoik May 07 '23 at 20:39
  • @cronoik I am unable to get the aspect and sentiment, I want to use the pre trained model cannot get the output – Dexter1611 May 09 '23 at 23:21
  • @Bugface could you help me on this – Dexter1611 May 10 '23 at 00:11

1 Answers1

0

The model you are trying to use predicts the sentiment for a given aspect based on a text. That means, it requires text and aspect to perform a prediction. It was not trained to extract aspects from a text. You could use a keyword extraction model to extract aspects (compare this SO answer).

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)

Output:

{'food': ('Positive', 0.9973154664039612), 'service': ('Negative', 0.9935430288314819)}
cronoik
  • 15,434
  • 3
  • 40
  • 78
  • This seems to partially answer the question as the answer is providing by default the aspect as an input but it is not an output from the transformer. – PeCaDe May 23 '23 at 22:12
  • @PeCaDe can you elaborate on what you mean by the output of the transformer? – cronoik May 24 '23 at 11:10
  • @PeCaDe is correct the package generate aspect and we don't need to specify aspect it will be give both aspect and sentiment score – Dexter1611 May 26 '23 at 01:55
  • @Dexter1611 The model `yangheng/deberta-v3-base-absa-v1.1` doesn't work this way because it wasn't trained for it. The author uses some additional logic with PyABSA to extract the aspect first and uses the model the way I have shown. In case that's what you want to know, please open a separate question. – cronoik May 26 '23 at 04:22
  • The follow-up question is [here](https://stackoverflow.com/q/76337058/6664872). – cronoik May 26 '23 at 04:39
  • @Dexter1611 in the example you set the aspects in a list. So, this solution retrieves the sentiment. But what about not indicating the aspect in a list so the algorithm finds them? So, in my opinion this question is not solved or the Title is incorrect. – PeCaDe May 26 '23 at 09:07
  • The mentioned model wasn't trained for indicating aspects. The only way to achieve that is fine-tuning it. @PeCaDe – cronoik May 26 '23 at 09:27
  • 1
    @cronoik I agree, so I would suggest a change of title for a less confusing. something like: "aspect-based sentiment analysis" as the aspect is not extracted but provided. – PeCaDe May 26 '23 at 09:30