Covers derivatives of simple functions, a value object that records its operations, backward steps for each operation and for a whole graph, a neuron, a small network and a comparison with PyTorch. Watch it, then build your own version here.
COURSE / REAL PRACTICE
Build a language model
Build a small language model in eight graded steps: a scalar autograd engine, a bigram model, an embedding MLP, a byte-pair tokenizer, cached attention, a GPT module and the training loop around it. Lessons pair with public videos by Andrej Karpathy. Unofficial practice material, not affiliated with the video’s author.
Read the explanation and diagrams, then work through the practice. Your browser profile supplies your saved progress.
What you’ll work toward
Write the pieces of a small GPT yourself, check each one against PyTorch or an exact result, and finish by training a character model on a CPU that beats a bigram baseline.
Read an explanation, inspect the diagrams and work through the available checks, debugging exercises and builds. Feedback and prerequisite links guide the next step.
Before you start
This course builds on these skills:
Individual lessons can add prerequisites. Your lesson progress shows your actual readiness.
A look inside the first lesson
a, b = 2.0, -3.0
c = a * b # -6
d = c + a # -4
L = d * d # 16
dL_dd = 2 * d # d is used twice: -8
a_from_d = 1.0 * dL_dd # d = c + a, local derivative 1
a_from_c = b * dL_dd # c's grad is -8, then times b
b_grad = a * dL_dd
print(L, dL_dd, a_from_d + a_from_c, b_grad)Your lesson sequence
Follow the same units and lessons in either view. Prerequisite links and your practice progress stay with each lesson.
Predicting the next character
- LESSON 01.01
A scalar autograd engine
Build a Scalar type that records each operation, then walk the graph backwards to give every input its gradient, checked against torch autograd.
Open lesson - LESSON 01.02
A bigram model from counts
Count which character follows which, turn the table into smoothed row probabilities, score words by negative log-likelihood and sample new ones.
Open lesson - LESSON 01.03
An embedding MLP with hand-written gradients
Look characters up in an embedding table, add a tanh layer, and derive every gradient by hand, including the scatter-add that repeated characters need.
Open lesson
Tokens and attention
- LESSON 02.01
A byte-pair tokenizer
Turn text into UTF-8 bytes, learn merges by counting adjacent pairs, then encode by merge rank and decode back, with every round trip checked against a slow oracle.
Open lesson - LESSON 02.02
Causal attention with a key-value cache
Split the width into heads, mask each position to its past, and reuse stored keys and values so generating one token costs one new row.
Open lesson - LESSON 02.03
Assemble a GPT
Wire embeddings, pre-norm attention blocks, a tied output head and a loss into one GPT module, then check its size, its init and its causality.
Open lesson
Training and sampling
- LESSON 03.01
Schedules, clipping and accumulation
Build the pieces around the optimizer: warmup and cosine learning rates, weight decay groups, global gradient clipping, gradient accumulation and a wrapping batch loader.
Open lesson - LESSON 03.02
Capstone: train a small GPT and sample from it
Encode and split a text, draw shifted batches, train a small GPT keeping its best validation weights, and sample text at a chosen temperature.
Open lesson
WATCH ALONG
Videos for these lessons
One hand-picked talk, lecture or tutorial per lesson, in course order. They play here when you press play; nothing loads from the video provider before that.
6 videos· 13 h 51 min
-
-
The paired video. Its chapters cover counting bigrams in a table, sampling from it, the negative log-likelihood loss and smoothing with fake counts, then the same model as a one-layer neural network. Watch it, then build the table and the loss here.
03An embedding MLP with hand-written gradients
Builds a character-level MLP language model with an embedding lookup table, a hidden layer and cross-entropy, then trains it with minibatches and splits the data. This lesson is the same network with the gradients written by hand.
-
Covers Unicode and UTF-8, the byte-pair encoding algorithm, counting and merging pairs, training with a loop, then encoding and decoding. Watch the algorithm and implementation chapters first, then build your own version here against a different oracle.
05Causal attention with a key-value cache
A language model built from a bigram baseline up to self-attention, multiple heads and a transformer block. The self-attention, masking and multi-head chapters are the ones this lesson builds on; the cache is this lesson's own addition.
06Schedules, clipping and accumulation
A long build of a GPT-2 sized model and its training run. Its third section covers AdamW, gradient clipping, a warmup and cosine learning rate and gradient accumulation, the settings this lesson implements. Watch that section first.