This course contains the use of artificial intelligence.
A chat model can feel like magic: you type a question, and a fluent answer appears. But a language model is not a black box. It is a stack of small steps, and every one of them can be drawn on paper, checked with a calculator and written in a few lines of PyTorch.
Many engineers learn these steps the slow way, piecing them together from research papers and half-finished code. This course gives you the steps in order, one at a time, so each new idea rests on a piece you have already built and tested. When you have written every part of a GPT yourself, a loss that won't drop or a shape that doesn't fit stops being a mystery: you know where to look.
Every chapter follows the same three steps:
- Draw it. Short hand-drawn lessons explain one idea at a time, with small examples you can follow.
- Compute it by hand. Key numbers (softmax, attention scores, the loss, parameter counts, file sizes) are worked out with real values.
- Code it live. We type the code in a real notebook, run every cell, and check each output against the hand calculation.
What you'll build, chapter by chapter:
- What an LLM does: next-word prediction, and the map from raw text to an assistant.
- Text to numbers: tokens, byte pair encoding, sliding windows, token and position embeddings.
- Attention: attention for one word by hand, then query, key and value, the causal mask, dropout and multi-head attention.
- The GPT model: layer normalization, GELU, shortcut connections and the transformer block. Assemble GPT-2 and count its 124 million parameters.
- Pretraining: cross-entropy loss and perplexity, a full training loop, temperature and top-k, and OpenAI's real GPT-2 weights loaded into your own model.
- Fine-tuning for classification: turn GPT-2 into a spam classifier that reaches about 96% accuracy on messages it has never seen.
- Fine-tuning to follow instructions: train GPT-2 medium (355 million parameters) on 1,100 instructions, then let a larger model grade its answers from 0 to 100.
- LoRA: freeze the model and train only about 2% of the numbers, and still reach about 97% accuracy on the spam task.
- Reasoning models: load Qwen3-0.6B, write a grader and score it on MATH-500, make it think step by step, sample and vote, and let it fix its own answers. Then compute GRPO by hand, run reinforcement learning and distillation on a free GPU, and read the training logs to see what makes a run stable or collapse.
- Extras: the KV cache, long-context attention, mixture of experts, how GPT-2 becomes Llama, today's model family, and preference tuning.
Small models, the same ideas. The models you train here are small enough for an ordinary computer, but they are built from the same parts as today's large models: the same tokenizer ideas, the same attention, the same training loop. What you learn carries over.
What's included: 84 lectures, a quiz at the end of every chapter, and the notebooks from every live lesson, attached to the matching section. Chapters 1 to 8 also end with the exercises from the open-source LLMs-from-scratch repository, solved live.
Hardware: chapters 1 to 6 run on a normal laptop CPU. Chapters 7 and 8 train a larger model, so we switch to a free GPU in Google Colab, and the lessons show every step of that setup. Chapter 9 runs entirely in Colab on a free T4 GPU.
Narration uses an AI-synthesized voice. Code cells follow the open-source LLMs-from-scratch and reasoning-from-scratch repositories (Apache-2.0); the explanations, drawings and examples are our own.