Instructions to use ZainabAlmahal/financial-topic-classifier-lora with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use ZainabAlmahal/financial-topic-classifier-lora with PEFT:
from peft import PeftModel from transformers import AutoModelForSequenceClassification base_model = AutoModelForSequenceClassification.from_pretrained("Qwen/Qwen2.5-0.5B") model = PeftModel.from_pretrained(base_model, "ZainabAlmahal/financial-topic-classifier-lora") - Notebooks
- Google Colab
- Kaggle
Financial Topic Classifier β Qwen2.5-0.5B + LoRA
A LoRA adapter for Qwen/Qwen2.5-0.5B that classifies short English financial texts (tweets) into one of 20 topics. It trains 1,099,264 parameters (0.22% of the base model): LoRA on the attention projections plus a linear classification head on the last token.
Labels
0 Analyst Update, 1 Fed | Central Banks, 2 Company | Product News, 3 Treasuries | Corporate Debt, 4 Dividend, 5 Earnings, 6 Energy | Oil, 7 Financials, 8 Currencies, 9 General News | Opinion, 10 Gold | Metals | Materials, 11 IPO, 12 Legal | Regulation, 13 M&A | Investments, 14 Macro, 15 Markets, 16 Politics, 17 Personnel Change, 18 Stock Commentary, 19 Stock Movement
Results
Test set: the dataset's official validation split (4,117 tweets), evaluated once after training. Approximate 95% margin on accuracy: Β±0.9 points.
| model | accuracy | macro_f1 | weighted_f1 |
|---|---|---|---|
| Majority class | 0.2069 | 0.0171 | 0.0710 |
| TF-IDF + LogReg (C=16, class_weight=balanced) | 0.8499 | 0.8284 | 0.8502 |
| Qwen2.5-0.5B + LoRA | 0.9111 | 0.8996 | 0.9110 |
On the 3,439 test tweets whose text never appears in the training data: accuracy 0.9023, macro F1 0.8842.
Training data
zeroshot/twitter-financial-news-topic (MIT license). The original train split was divided 90/10, stratified by label (seed 42), into train (15,291) and dev (1,699). Dev was used for checkpoint selection and to fit the calibration temperature. Largest/smallest class ratio in train: 81x.
Training procedure
- LoRA: r=8, alpha=16, dropout=0.05, target modules: k_proj, o_proj, q_proj, v_proj; classification head trained fully
- Max length 96 tokens with truncation; dynamic padding
- 3 epochs, learning rate 0.0002, AdamW, linear schedule with 71 warmup steps
- Effective batch size 32 (16 per device x 2 GPU)
- Precision: fp16 mixed precision
- Hardware: Tesla T4, Tesla T4; training time 26 min; peak GPU memory 6.0 GB
- Best checkpoint chosen by dev macro F1 (epoch 3)
| epoch | train_loss | dev_loss | dev_accuracy | dev_macro_f1 |
|---|---|---|---|---|
| 1 | 2.5867 | 0.6289 | 0.8946 | 0.8599 |
| 2 | 0.5215 | 0.5595 | 0.9123 | 0.8758 |
| 3 | 0.2281 | 0.5911 | 0.9264 | 0.9005 |
Most confused topic pairs (test set)
| class_a | class_b | a_predicted_as_b | b_predicted_as_a | total |
|---|---|---|---|---|
| Company | Product News | Stock Commentary | 14 | 14 | 28 |
| General News | Opinion | Macro | 14 | 12 | 26 |
| Company | Product News | General News | Opinion | 9 | 10 | 19 |
| General News | Opinion | Politics | 5 | 11 | 16 |
| Fed | Central Banks | Macro | 10 | 5 | 15 |
| Company | Product News | M&A | Investments | 6 | 9 | 15 |
| Energy | Oil | Macro | 6 | 7 | 13 |
| General News | Opinion | Legal | Regulation | 4 | 7 | 11 |
Calibration
Temperature scaling with T = 1.6 (fitted on dev) changes test expected calibration error from 0.043 to 0.011 without changing any prediction. Divide the logits by T before softmax when using confidence scores.
Limitations
- Trained on English financial tweets only; other domains, languages and long documents are untested.
- Several topics overlap in meaning (see the confused pairs), and each tweet carries exactly one label even when it covers two topics.
- Rare classes have few test examples (e.g. Gold | Metals | Materials: 13), so their per-class scores are noisy.
- Labels are used as provided by the dataset; some appear noisy.
How to use
import json, torch
from huggingface_hub import hf_hub_download
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from peft import PeftModel
repo = "ZainabAlmahal/financial-topic-classifier-lora"
labels = json.load(open(hf_hub_download(repo, "label_names.json")))
tokenizer = AutoTokenizer.from_pretrained(repo)
base = AutoModelForSequenceClassification.from_pretrained("Qwen/Qwen2.5-0.5B", num_labels=20)
base.config.pad_token_id = tokenizer.pad_token_id
model = PeftModel.from_pretrained(base, repo).merge_and_unload().eval()
inputs = tokenizer(["Apple beats quarterly earnings estimates"], truncation=True, max_length=96, return_tensors="pt")
with torch.no_grad():
probs = torch.softmax(model(**inputs).logits / 1.6, dim=-1) # 1.6 = calibration temperature
print(labels[probs.argmax(-1).item()], round(probs.max().item(), 3))
- Downloads last month
- 26
Model tree for ZainabAlmahal/financial-topic-classifier-lora
Base model
Qwen/Qwen2.5-0.5B