fancyzhx/ag_news
Viewer • Updated • 128k • 119k • 190
How to use theshekslaw/ag_news_bert_full with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("text-classification", model="theshekslaw/ag_news_bert_full") # Load model directly
from transformers import AutoTokenizer, AutoModelForSequenceClassification
tokenizer = AutoTokenizer.from_pretrained("theshekslaw/ag_news_bert_full")
model = AutoModelForSequenceClassification.from_pretrained("theshekslaw/ag_news_bert_full")Fine-tuned :contentReference[oaicite:1]{index=1} model for 4-class news topic classification using the :contentReference[oaicite:2]{index=2} dataset.
The model classifies news articles into:
| Property | Value |
|---|---|
| Base Model | bert-base-uncased |
| Task | Text Classification |
| Framework | PyTorch + Transformers |
| Dataset | AG News |
| Classes | 4 |
| Test Accuracy | 94.7% |
| Macro F1 | 0.94 |
| ID | Label |
|---|---|
| 0 | World |
| 1 | Sports |
| 2 | Business |
| 3 | Sci/Tech |
| Parameter | Value |
|---|---|
| Epochs | 3 |
| Batch Size | 16 |
| Learning Rate | 2e-5 |
| Max Sequence Length | 128 |
| Warmup Steps | 500 |
| Optimizer | AdamW |
| Weight Decay | 0.01 |
| LR Scheduler | Linear Warmup + Linear Decay |
| Gradient Clipping | Max Norm = 1.0 |
| Split | Samples |
|---|---|
| Train | 120,000 |
| Test | 7,600 |
Dataset source:
:contentReference[oaicite:3]{index=3}
| Class | Precision | Recall | F1-Score |
|---|---|---|---|
| World | 0.97 | 0.95 | 0.96 |
| Sports | 0.98 | 0.99 | 0.99 |
| Business | 0.92 | 0.91 | 0.92 |
| Sci/Tech | 0.91 | 0.93 | 0.92 |
pip install transformers torch
from transformers import pipeline
classifier = pipeline(
"text-classification",
model="theshekslaw/ag_news_bert_full"
)
result = classifier(
"Apple announces new MacBook Pro with M3 chip"
)
print(result)
Example output:
[{'label': 'Sci/Tech', 'score': 0.9895}]
| Text | Prediction |
|---|---|
| Apple announces new MacBook Pro with M3 chip | Sci/Tech |
| Federal Reserve raises interest rates | Business |
| UN calls for ceasefire in ongoing conflict | World |
| Real Madrid wins Champions League final | Sports |
The model was fine-tuned using a custom PyTorch training loop instead of the Hugging Face Trainer API.
Key components:
DataCollatorWithPadding@misc{ag_news_bert_classifier,
title={AG News BERT Classifier},
author={Abhishek Pandey},
year={2026},
publisher={Hugging Face}
}