Small GPT-2 trained from scratch on Reuters news articles
A GPT-2 style language model trained from randomly initialised weights (no pretrained GPT-2 weights) with the
self-supervised next-token prediction objective on the title + body of every article in
ingeniumacademy/reuters_articles, using the tokenizer
ingeniumacademy/gpt2-reuters-tokenizer. Made for Assignment 02 – Self-Supervised Learning.
Model configuration
| n_layer | n_head | n_embd | n_positions | vocab_size | dropout | activation | parameters |
|---|
| 6 | 6 | 384 | 256 | 52000 | 0.1 | gelu_new | 30,713,856 |
Training
| num_train_epochs | per_device_train_batch_size | learning_rate | lr_scheduler_type | warmup_steps | weight_decay | max_grad_norm | logging_steps | eval_strategy | fp16 | seed | training_time_min | hardware |
|---|
| 5 | 32 | 0.0005 | cosine | 84 | 0.1 | 1.0 | 33 | epoch | True | 42 | 7.9 | Tesla T4 |
Loss per epoch:
| epoch | train_loss (avg in epoch) | val_loss | val_perplexity |
|---|
| 1 | 6.2865 | 5.2368 | 188.07 |
| 2 | 4.7557 | 4.723 | 112.51 |
| 3 | 4.289 | 4.4783 | 88.08 |
| 4 | 3.9998 | 4.3727 | 79.25 |
| 5 | 3.8549 | 4.3556 | 77.91 |
Final results
| split | loss | perplexity |
|---|
| train | 3.7455 | 42.33 |
| validation | 4.3556 | 77.91 |
| test | 4.1651 | 64.4 |
Extra runs (Q4)
| run | changed_setting | parameters | validation_loss | test_loss | test_perplexity |
|---|
| main | – (baseline) | 30713856 | 4.3556 | 4.1651 | 64.4 |
| epochs_2 | epochs 5 → 2 | 30713856 | 5.0158 | 4.7983 | 121.31 |
| layers_2 | Transformer layers 6 → 2 | 23616000 | 4.4715 | 4.281 | 72.31 |
Files in this repository
- root: the main model (
config.json, model.safetensors, generation_config.json) and the tokenizer
results/: loss curves, training logs (CSV), generated texts, next-token tables, report_values.json
experiments/epochs_2, experiments/layers_2: the two models trained for Q4
How to use
import torch
from transformers import AutoTokenizer, GPT2LMHeadModel
tok = AutoTokenizer.from_pretrained("Che-01/gpt2-reuters-from-scratch")
model = GPT2LMHeadModel.from_pretrained("Che-01/gpt2-reuters-from-scratch")
ids = tok(tok.eos_token + "OIL PRICES RISE\n", return_tensors="pt").input_ids
out = model.generate(ids, max_new_tokens=60, do_sample=True, top_p=0.9, temperature=0.7, pad_token_id=tok.eos_token_id)
print(tok.decode(out[0], skip_special_tokens=True))
Limitations
A small model trained on a small, old (1987) news corpus: the text looks like Reuters copy, but facts and numbers are invented.
For learning purposes only.