Downloads · 30 days
24
16% of all-time downloads
karthik-infobell25/news-topic-classifier
news-topic-classifier is a text classification model from karthik-infobell25. Use it when you need a label for a piece of text. The card lists the license as apache-2.0.
A news headline classifier that categorizes text into three topics: Technology, Sports, and Politics. Built using a linear probing approach with frozen BERT embeddings, achieving 95.24% test accuracy.
Downloads · 30 days
24
16% of all-time downloads
All-time downloads
150
Public
Parameters
109M
877 MB on disk
Likes
0
Public
Click a slice to open those files.
.bin438 MB · 50%
From the Hugging Face model README
A news headline classifier that categorizes text into three topics: Technology, Sports, and Politics. Built using a linear probing approach with frozen BERT embeddings, achieving 95.24% test accuracy.
Input → Tokenizer → Frozen BERT Encoder → [CLS] Token → Linear Layer → 3 Classes
| Epoch | Train Loss | Train Acc | Test Loss | Test Acc |
|---|---|---|---|---|
| 1 | 1.0584 | 45.24% | 0.8724 | 80.95% |
| 2 | 0.8449 | 71.43% | 0.7716 | 90.48% |
| 3 | 0.6893 | 90.48% | 0.6514 | 80.95% |
| 4 | 0.5194 | 92.86% | 0.5837 | 90.48% |
| 5 | 0.4683 | 88.10% | 0.5102 | 95.24% ⭐ |
| 6 | 0.3987 | 95.24% | 0.4573 | 95.24% |
| 7 | 0.3671 | 95.24% | 0.4358 | 95.24% |
| 8 | 0.3386 | 97.62% | 0.3938 | 90.48% |
| 9 | 0.2763 | 97.62% | 0.3787 | 85.71% |
| 10 | 0.2724 | 94.05% | 0.3713 | 90.48% |
precision recall f1-score support
Technology 0.78 1.00 0.88 7 Sports 1.00 0.67 0.80 6 Politics 1.00 1.00 1.00 8
accuracy 0.90 21 macro avg 0.93 0.89 0.89 21 weightedavg 0.93 0.90 0.90 21
Class-wise Performance:
Linear probing was chosen to:
import torch from transformers import AutoTokenizer from model import BERTLinearClassifier # Your custom class
Load model model = BERTLinearClassifier(model_name='bert-base-uncased', num_labels=3) checkpoint = torch.load('pytorch_model.pt', map_location='cpu') model.load_state_dict(checkpoint['model_state_dict']) model.eval()
Load tokenizer tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
Single prediction headline = "Apple releases new MacBook Pro with M3 chip"
inputs = tokenizer( headline, return_tensors='pt', max_length=128, padding='max_length', truncation=True )
with torch.no_grad(): logits = model(inputs['input_ids'], inputs['attention_mask']) prediction = torch.argmax(logits, dim=1).item() probabilities = torch.softmax(logits, dim=1)
Map to labels id2label = {0: 'Technology', 1: 'Sports', 2: 'Politics'} print(f"Predicted: {id2label[prediction]}") print(f"Confidence: {probabilities[prediction]:.2%}")
Output: Predicted: Technology Confidence: 94.5%
headlines = [ "Google unveils new AI model", "Manchester United wins Premier League", "Senate passes infrastructure bill" ]
for headline in headlines: inputs = tokenizer(headline, return_tensors='pt', max_length=128, padding='max_length', truncation=True)
with torch.no_grad(): logits = model(inputs['input_ids'], inputs['attention_mask']) pred = torch.argmax(logits, dim=1).item()
print(f"{headline} → {id2label[pred]}")
pytorch_model.pt: Model checkpoint with state dictconfig.json: Model configuration and label mappingstokenizer files: BERT tokenizer (from bert-base-uncased)Note: This model was trained as a learning project. For production use, consider models trained on larger, more diverse datasets.