fancyzhx/ag_news
Viewer • Updated • 128k • 120k • 211
How to use ZyroGod/distilgpt2-news-topic-classification with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("text-classification", model="ZyroGod/distilgpt2-news-topic-classification") # pip install -U transformers accelerate
# Load model directly
from transformers import AutoTokenizer, AutoModelForSequenceClassification
tokenizer = AutoTokenizer.from_pretrained("ZyroGod/distilgpt2-news-topic-classification")
model = AutoModelForSequenceClassification.from_pretrained("ZyroGod/distilgpt2-news-topic-classification", device_map="auto")This repository provides a fine-tuned distilgpt2 (82M parameter causal language model) adapted for 4-class sequence classification on the AG News dataset (fancyzhx/ag_news).
The model categorizes news headlines and article snippets into four distinct domains:
World (Class 0)Sports (Class 1)Business (Class 2)Sci/Tech (Class 3)The model was evaluated on a held-out test split of 1,000 balanced samples:
| Metric | Score | Percentage |
|---|---|---|
| Test Accuracy | 0.9130 |
91.30% |
| Weighted F1 | 0.9131 |
91.31% |
| Macro F1 | 0.9134 |
91.34% |
| Weighted Precision | 0.9142 |
91.42% |
| Weighted Recall | 0.9130 |
91.30% |
| Class | Precision | Recall | F1-Score | Accuracy | Test Support |
|---|---|---|---|---|---|
| Sports | 97.58% |
96.80% |
97.19% |
96.80% |
250 |
| World | 92.98% |
91.84% |
92.40% |
91.84% |
245 |
| Sci/Tech | 85.02% |
91.53% |
88.16% |
91.53% |
248 |
| Business | 90.12% |
85.21% |
87.60% |
85.21% |
257 |
distilgpt2 (6 decoder layers, 12 attention heads, 768 embedding dimensions, ~82M parameters).summary_type = "cls_index").padding_side = "left"): Crucial for decoder-only causal models so that the final position in the sequence contains the target text token rather than padding tokens.pad_token = eos_token): DistilGPT2 has no default pad token, so eos_token (<|endoftext|>, ID 50256) is reused for padding.fancyzhx/ag_news (2,400 balanced train, 400 val, 1,000 test)adamw_torch_fused (weight decay: 0.01)from transformers import pipeline
# Load classifier pipeline directly from Hugging Face Hub
classifier = pipeline("text-classification", model="ZyroGod/distilgpt2-news-topic-classification")
# Predict on news headlines
headline = "NASA's James Webb Space Telescope discovers oldest galaxy cluster ever observed."
result = classifier(headline)
print(result)
# Output: [{'label': 'Sci/Tech', 'score': 0.9852}]
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
model_name = "ZyroGod/distilgpt2-news-topic-classification"
# Load tokenizer and model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
model.eval()
# Ensure proper left padding for causal sequence classification
tokenizer.padding_side = "left"
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
headline = "Wall Street rallies as Federal Reserve signals potential interest rate cuts."
inputs = tokenizer(
headline,
return_tensors="pt",
truncation=True,
max_length=128,
padding=True
)
with torch.no_grad():
outputs = model(**inputs)
probabilities = torch.softmax(outputs.logits, dim=-1)[0]
id2label = model.config.id2label
for idx, prob in enumerate(probabilities):
label = id2label.get(str(idx), id2label.get(idx, f"Class {idx}"))
print(f"{label:<10s}: {prob.item():.2%}")
| Label ID | Category Name | Description |
|---|---|---|
0 |
World |
International news, geopolitics, conflicts, foreign diplomacy |
1 |
Sports |
Competitions, tournament coverage, team news, athlete statistics |
2 |
Business |
Financial markets, stocks, macroeconomic policy, corporations |
3 |
Sci/Tech |
Science, space exploration, software, hardware, AI breakthroughs |
This model is licensed under the Apache 2.0 License.