Spaces:
Sleeping
Sleeping
Download src/model.py from abdurrehmansaeed/AnchorQuant: direct link, hf CLI and curl.
- Browser
- Download file 946 Bytes
-
https://huggingface.co/spaces/abdurrehmansaeed/AnchorQuant/resolve/main/src/model.py
- Command line
-
hf download hf://spaces/abdurrehmansaeed/AnchorQuant/src/model.py
-
curl -L -o model.py https://huggingface.co/spaces/abdurrehmansaeed/AnchorQuant/resolve/main/src/model.py
946 Bytes
| 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) |