Views
No views yet
1from transformers import BertTokenizer, BertForSequenceClassification
2import torch
3
4tokenizer = BertTokenizer.from_pretrained("Karthik1610/finbert-financial-sentiment")
5model = BertForSequenceClassification.from_pretrained("Karthik1610/finbert-financial-sentiment")
6
7text = "The company reported record profits and raised its annual dividend by 25%."
8inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=64, padding="max_length")
9with torch.no_grad():
10 logits = model(**inputs).logits
11probs = torch.softmax(logits, dim=1).squeeze()
12
13id2label = model.config.id2label
14pred_id = int(probs.argmax())
15print(id2label[pred_id], float(probs[pred_id]))
16# positive 0.99...{0: "negative", 1: "neutral", 2: "positive"} (model.config.id2label) - note
this is not the same order as the base ProsusAI/finbert checkpoint's native config
({0: positive, 1: negative, 2: neutral}). Always read the id-to-label mapping from
model.config rather than hardcoding it; the repo's eval/label_mapping.py does this
defensively for exactly that reason.ankurzing/sentiment-analysis-for-financial-news on Kaggle) - 4,838 English-language
financial news sentences, annotated by finance professionals with negative / neutral /
positive sentiment. Class distribution: 59.4% neutral, 28.2% positive, 12.5% negative.ProsusAI/finbertnotebooks/finbert_training.ipynb in the GitHub repo.eval/harness.py in the repo) against
four baselines - majority-class, TF-IDF+LogisticRegression, ProsusAI/finbert zero-shot,
and bert-base-uncased fine-tuned the same way - on both the in-domain PhraseBank test
split and an out-of-distribution set (FiQA
Task-1 sentiment, thresholded from its native [-1, 1] score).| Model | Macro-F1 | Accuracy |
|---|---|---|
| This model (fine-tuned FinBERT) | 0.883 | 0.886 |
| FinBERT, zero-shot | 0.865 | 0.872 |
| bert-base-uncased, fine-tuned | 0.845 | 0.850 |
| TF-IDF + Logistic Regression | 0.716 | 0.752 |
| Majority-class (floor) | 0.248 | 0.594 |
| Model | Macro-F1 | Accuracy |
|---|---|---|
| FinBERT, zero-shot | 0.482 | 0.498 |
| This model (fine-tuned FinBERT) | 0.414 | 0.403 |
| bert-base-uncased, fine-tuned | 0.428 | 0.417 |
| TF-IDF + Logistic Regression | 0.289 | 0.299 |
| Majority-class (floor) | 0.049 | 0.079 |
@article{araci2019finbert,
title={FinBERT: Financial Sentiment Analysis with Pre-trained Language Models},
author={Araci, Dogu},
journal={arXiv preprint arXiv:1908.10063},
year={2019}
}
@article{malo2014good,
title={Good debt or bad debt: Detecting semantic orientations in economic texts},
author={Malo, Pekka and Sinha, Ankur and Korhonen, Pekka and Wallenius, Jyrki and Takala, Pyry},
journal={Journal of the Association for Information Science and Technology},
year={2014}
}