gpt2-reuters-cs4681 - an autoregressive language model built from scratch
2 min readupdated
A compact 4-layer GPT-2 architecture language model trained from scratch on randomly initialized weights without using pretrained checkpoints. Built on a Reuters financial news dataset, the model learns the statistical patterns, financial terminology, and syntax of 1980s news wires. It targets researchers, students, and developers exploring self-supervised learning methods and text generation.
Key features
- Trained from scratch on ~13 MB of Reuters financial news without a pretrained base.
- Compact architecture: 4 transformer layers, 4 attention heads, and about 16.5 million parameters.
- Uses a specialized BPE tokenizer with a vocabulary size of 52,000 items.
- Includes full training and testing metrics, including cross-entropy loss and perplexity.
What you can do
- Test autoregressive text generation on financial prompts in a Python environment.
- Use the model as an educational example for pretraining small transformer decoders.
- Apply as a baseline for comparing small custom models against large counterparts.
Example
import torch
from transformers import AutoTokenizer, GPT2LMHeadModel
repo_id = "the-sithum/gpt2-reuters-cs4681"
tokenizer = AutoTokenizer.from_pretrained(repo_id)
model = GPT2LMHeadModel.from_pretrained(repo_id)
model.eval()
prompt = "PENTAGON STOPS NEW BUSINESS WITH TOSHIBA"
inputs = tokenizer(prompt, return_tensors="pt")
with torch.no_grad():
output_ids = model.generate(
**inputs,
max_new_tokens=50,
do_sample=True,
top_k=50,
top_p=0.9,
temperature=0.8,
repetition_penalty=1.2,
pad_token_id=tokenizer.eos_token_id
)
print(tokenizer.decode(output_ids[0], skip_special_tokens=True))
Read more
Details• Metrics: Test Loss — 3.9115, Test Perplexity (PPL) — 49.97 after 5 training epochs.
• Hardware requirements: trained on an NVIDIA Tesla T4 GPU (~15.64 GB VRAM), checkpoint size around 66 MB.
• Limitations: trained exclusively on 1987 news, lacks knowledge of modern events, and tends to repeat when generating long sequences.
• Training details: AdamW optimizer, learning rate 5e-4, FP16 mixed precision.










Comments