
π This article includes 4 downloadable practice files β
“It looks good” isn’t a measurement. Language models are compared with a few standard numbers, plus a set of prompts you check by eye every time you change something.
In this article
Loss and perplexity
Training minimises cross-entropy loss: how surprised the model is by the real next token. Perplexity = eloss, roughly “how many tokens it is choosing between”. A model guessing randomly among 40 characters has perplexity 40; ours scored about 1.1 on held-out text because the sample is so repetitive.
losses = [model(*get_batch(val, 64, 32))[1].item() for _ in range(20)]
avg = sum(losses) / len(losses)
print(avg, math.exp(avg))
Fixed test prompts
Keep 10-20 prompts that matter for your use, generate with a fixed seed and low temperature after each training run, and compare. Regressions (it used to answer, now it rambles) show up immediately.
Beyond loss
- Benchmarks (multiple-choice tests of knowledge and reasoning) for general models.
- Task metrics: accuracy for classification, exact match for extraction.
- Human review for tone, helpfulness and safety, which no number captures well.
Practice
Run after lesson 7. Then retrain for fewer steps and compare the perplexity.
π Practice files for this article
β¬ Download all 4 files (ZIP Β· 5 KB)
- πtinygpt.py (shared model code)The GPT model, tokenizer, batching and generation used in lessons 6-12.β¬ PY Β· 4 KB
- πLesson 9 scriptTested script for this lesson.β¬ PY Β· 917 B
- πExpected outputWhat the script printed in our test run (your numbers will be close).β¬ TXT Β· 243 B
- πsample.txtSmall training text; swap in any public-domain book for better results.β¬ TXT Β· 83 KB
Free to use for learning. Files with macros (.bas) are plain text β import them with Alt+F11 β File β Import File, and always test on a copy.
Stuck on a step? Ask a question and the AI answers using this article.