GPT-2 trained from scratch on Reuters articles
A small GPT-2 language model trained from random initialisation (no pretrained weights) on the
ingeniumacademy/reuters_articles dataset,
using the ingeniumacademy/gpt2-reuters-tokenizer tokenizer.
Built for CS4681 (Advanced Machine Learning), Assignment 02 — Self-Supervised Learning, University of Moratuwa.
Notebook with full code and outputs: https://www.kaggle.com/code/birunthabanrajendram/self-supervised-learning
Model
| Architecture | GPT-2: 4 layers, 4 heads, hidden size 256, context 256 tokens, dropout 0.1 |
| Parameters | 16,537,088 (13,377,536 in embeddings) |
| Vocabulary | 52,000 (byte-level BPE, input/output embeddings tied) |
| Objective | Causal language modelling (next-token prediction) |
Training
- Data: 17,262 training articles; each formatted as
title + "\n\n" + body+<|endoftext|>, concatenated and split into 256-token blocks (3.06M training tokens). - Setup: 5 epochs (1,870 steps), batch size 32, AdamW, learning rate 5e-4 with cosine decay and 5% warm-up, weight decay 0.01, bf16, seed 42.
- Hardware: 1 × Tesla T4, about 20 minutes.
Results
| Split | Loss | Perplexity |
|---|---|---|
| Validation | 4.411 | 82.3 |
| Test | 4.192 | 66.1 |
Full training and evaluation history is in run.json.
Usage
The model expects a Reuters-style title followed by two newline tokens, matching the training format:
import torch
from transformers import AutoTokenizer, GPT2LMHeadModel
repo = "birunthabanr/gpt2-reuters-scratch"
tok = AutoTokenizer.from_pretrained(repo)
model = GPT2LMHeadModel.from_pretrained(repo).eval()
title = "OIL PRICES RISE ON OPEC OUTPUT CUTS"
nl = tok("\n", add_special_tokens=False)["input_ids"]
ids = torch.tensor([tok(title, add_special_tokens=False)["input_ids"] + nl + nl])
out = model.generate(ids, attention_mask=torch.ones_like(ids), max_new_tokens=100,
do_sample=True, top_k=50, top_p=0.95, pad_token_id=tok.eos_token_id)
print(tok.decode(out[0][ids.shape[1]:], skip_special_tokens=True))
Limitations
- Trained only on 1987 Reuters business and commodity news: it has no general knowledge and responds to any prompt in Reuters style.
- Small model and 256-token context: text is fluent over short spans but drifts off topic, repeats itself, and produces inconsistent figures.
- Intended for coursework and demonstration only, not for any real use.