BERT-base AG News News Classifier

Fine-tuned bert-base-uncased on AG News for 4-class news topic classification. 94.74% accuracy · 94.75 macro F1 — trained on RTX 4060 with fp16 in under an hour.


Model Description

This model is a fine-tuned version of bert-base-uncased on the AG News dataset, a benchmark corpus of 120,000 news headlines across four topic categories.

It classifies a short news headline or sentence into one of four classes:

ID Label
0 World
1 Sports
2 Business
3 Sci/Tech

Built as part of the DevelopersHub AI/ML Engineering Internship (Advanced Track, 2026).


Intended Use

  • Primary: Single-sentence or short headline news topic classification (English).
  • Good for: News aggregators, content tagging pipelines, NLP demos.
  • Out of scope: Long-form articles (>512 tokens), non-English text, opinion classification.

Training Details

Dataset

Split Size
Train 120,000
Test 7,600

Classes are perfectly balanced (30,000 / 1,900 per class).

Hyperparameters

Parameter Value
Base model bert-base-uncased
Learning rate 2e-5
Batch size 16
Epochs 3
Mixed precision fp16
Optimizer AdamW
Warmup ratio 0.1
Max sequence len 128

Hardware

Trained on NVIDIA RTX 4060 Laptop GPU (8 GB VRAM) using HuggingFace Trainer.


Evaluation Results

Overall

Metric Value
Accuracy 94.74%
Macro F1 0.9475
Weighted F1 0.9474

Per-Class F1

Class F1
World 0.95
Sports 0.99
Business 0.92
Sci/Tech 0.93

Baseline Comparison

Model Accuracy
TF-IDF + Logistic Reg. ~91%
This model (BERT) 94.74%

How to Use

Quick inference (HuggingFace pipeline)

from transformers import pipeline

classifier = pipeline(
    "text-classification",
    model="ArmanXAI/bert-base-agnews-news-classifier"
)

result = classifier("NASA launches new telescope to study deep space phenomena.")
print(result)
# [{'label': 'Sci/Tech', 'score': 0.984}]

Manual inference

from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch

model_name = "ArmanXAI/bert-base-agnews-news-classifier"
tokenizer  = AutoTokenizer.from_pretrained(model_name)
model      = AutoModelForSequenceClassification.from_pretrained(model_name)

labels = ["World", "Sports", "Business", "Sci/Tech"]

def classify(text: str) -> dict:
    inputs = tokenizer(text, return_tensors="pt",
                       truncation=True, max_length=128)
    with torch.no_grad():
        logits = model(**inputs).logits
    pred  = torch.argmax(logits, dim=-1).item()
    score = torch.softmax(logits, dim=-1)[0][pred].item()
    return {"label": labels[pred], "score": round(score, 4)}

print(classify("Apple unveils M4 chip with major AI performance gains."))
# {'label': 'Sci/Tech', 'score': 0.9821}

Limitations

  • English only — performance drops significantly on other languages.
  • Short text — trained on headline-length inputs (~7–15 tokens); degrades on paragraphs > 256 tokens.
  • Temporal drift — AG News corpus is from 2004–2005; accuracy may drop on very recent entity names, products, or events.
  • 4 coarse classes — not suitable for fine-grained topic classification.

Training Code

See the training notebook in the notebooks/ folder of this repository, or on GitHub: DevelopersHub-AI-Internship-Advance-Tasks


About the Author

Arman Adil Mangat — AI/ML Engineer, BS AI '26, UMT Lahore 🇵🇰

Specializing in Embedded AI + NLP. Building GENIE — a fully offline voice assistant on ESP32-S3 (Whisper + Mistral 7B, no cloud, $22 hardware).


Citation

@misc{mangat2026bertagNews,
  author    = {Arman Adil Mangat},
  title     = {BERT-base Fine-tuned on AG News for News Topic Classification},
  year      = {2026},
  publisher = {Hugging Face},
  url       = {https://huggingface.co/ArmanXAI/bert-base-agnews-news-classifier}
}

License

MIT — free to use, modify, and distribute with attribution.

Downloads last month
13
Safetensors
Model size
0.1B params
Tensor type
F32
·
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Model tree for ArmanXAI/bert-base-agnews-news-classifier

Finetuned
(6852)
this model

Dataset used to train ArmanXAI/bert-base-agnews-news-classifier

Space using ArmanXAI/bert-base-agnews-news-classifier 1

Evaluation results