Sentiment Analysis: Fine-Grained and Aspect-Based
Module: Natural Language Processing | Difficulty: Advanced
Aspect-Based Sentiment
Document-Level Sentiment
Hierarchical Attention
Word level: Sentence level:
Multimodal Sentiment
import torch
import torch.nn as nn
class AspectSentiment(nn.Module):
def __init__(self, bert_model, n_sentiments=3):
super().__init__()
self.bert = bert_model
self.aspect_attention = nn.Linear(768, 1)
self.classifier = nn.Linear(768, n_sentiments)
def forward(self, input_ids, attention_mask, aspect_mask):
outputs = self.bert(input_ids, attention_mask=attention_mask)
hidden = outputs.last_hidden_state
attn_weights = torch.softmax(self.aspect_attention(hidden).squeeze(-1), dim=1)
aspect_repr = torch.bmm(attn_weights.unsqueeze(1), hidden).squeeze(1)
return self.classifier(aspect_repr)
Research Insight: Aspect-based sentiment analysis is more useful than document-level because it identifies specific opinions about different aspects. The key challenge is aspect term extraction, which requires understanding implicit aspects.