import torch.nn as nn from transformers import AutoModel class FinancialSentimentClassifier(nn.Module): def __init__(self, n_classes=3): super(FinancialSentimentClassifier, self).__init__() # Load the pre-trained financial encoder self.bert = AutoModel.from_pretrained("ProsusAI/finbert") # Regularization to prevent overfitting on the small dataset self.drop = nn.Dropout(p=0.3) # Final linear layer mapping hidden states (768) to our 3 classes self.out = nn.Linear(self.bert.config.hidden_size, n_classes) def forward(self, input_ids, attention_mask): # Extract the pooled output (representation of the [CLS] token) outputs = self.bert( input_ids=input_ids, attention_mask=attention_mask ) pooled_output = outputs.pooler_output output = self.drop(pooled_output) return self.out(output)