Build a tiny GPT
For engineers
Write a small character-level GPT in PyTorch, from tokenizer and causal self-attention to a training loop and sampling, to see what every LLM does inside
110 questions across four stages: Understand, Apply, Solve and Judge.
Try 5 questions
Practice items, not counted towards your mastery. No account needed.
Check yourself · 1 of 5
A character tokenizer is built for a corpus that contains 65 different characters. What is vocab_size?
Check yourself · 2 of 5
In causal self-attention, when is the lower-triangular mask applied?
Check yourself · 3 of 5
Why does a GPT add position embeddings to token embeddings?
Check yourself · 4 of 5
A new character-level model with 65 characters has not been trained at all. About what cross-entropy loss should the first step print?
Check yourself · 5 of 5
Which logits does a GPT use to choose the next token during generation?
Topics
- Character tokenizer and training batches4 min read · Map characters to ids, split train and validation text, and cut random fixed-length blocks with targets shifted by one token
- Causal self-attention in code5 min read · Queries, keys and values, scaled dot products, the lower-triangular mask and multi-head reshaping in a few dozen lines of PyTorch
- Stacking blocks into a GPT model5 min read · Token and position embeddings, residual blocks with layer norm and an MLP, and a final projection to vocabulary logits
- Training loop and reading loss curves5 min read · Cross-entropy loss, AdamW, learning-rate warmup and decay, and how train versus validation loss reveals learning, overfitting or a broken run
- Sampling text from your trained model5 min read · Autoregressive generation with temperature and top-k, cropping the context window, and why a small model writes fluent nonsense
- From toy GPT to production models4 min read · What separates a tiny model from frontier models: BPE tokenizers, data scale, compute, post-training, and reference projects such as nanoGPT and nanochat
Start here
- Neural Networks: Zero to Hero (Andrej Karpathy) (opens in a new tab)CourseFree
Free course that builds up to a GPT from scratch, with notebooks.
- nanogpt-lecture (Karpathy) (opens in a new tab)ToolFree
The code written in the "Let's build GPT" lecture, with a git history that follows it step by step.
- nanoGPT (Karpathy) (opens in a new tab)ToolFree
A compact GPT training script to compare your own model against.
110 questions · last verified 2026-12