Probability, Information & Optimization
Softmax, cross-entropy as surprise, and gradient descent — the physics of training.
- → Compute softmax with temperature and predict how the distribution shifts
- → Interpret cross-entropy loss and perplexity as measures of surprise
- → Trace gradient descent on a loss surface and explain divergence
A language model is a probability distribution
Strip away everything else and a language model is one function: given the tokens so far, produce a probability for every possible next token. GPT-2 has 50,257 tokens in its vocabulary, so every forward pass ends with 50,257 numbers that are all positive and sum to 1.
The model doesn't produce probabilities directly. It produces logits — unbounded real-valued scores, one per token — and the softmax function converts them:
Read it term by term: exponentiate each logit (making everything positive and amplifying differences), then divide by the total (making them sum to 1). The temperature divides the logits first: small exaggerates gaps between logits, large washes them out.
Training needs a score for “how wrong was that prediction?”. The universal choice is cross-entropy loss: the negative log-probability the model assigned to the token that actually came next, . Assign the true next token 100% → loss 0. Assign it 1% → loss nats. Loss is literally surprise: rare-under-your-model events hurt in proportion to how confidently you ruled them out.
Training is rolling downhill
A model is a function with parameters (weights). Cross-entropy gives a single number saying how badly the current parameters predict the training data. Put those together and training becomes an optimization problem: find weights that make the loss small.
Gradient descent is the entire trick. The gradient points in the direction of steepest loss increase, so step the other way:
with learning rate controlling step size. Backpropagation is not a separate algorithm — it is the chain rule from calculus, applied efficiently through the network's computation graph so that one backward pass yields the gradient for every one of the billions of parameters at once.
Feel it: softmax & descent
Two toys. First: the exact softmax computation that ends every LLM forward pass — drive the temperature to the extremes and watch entropy respond. Second: gradient descent on a bumpy 1-D loss — your job is to find a learning rate that escapes the local minimum but still converges.
Things to try: (1) Set = 0.05 — this is what greedy decoding feels like. (2) Make two logits equal and watch their probabilities lock together at any temperature. (3) In the descent toy, find the critical learning rate where behavior flips from converging to bouncing forever.
Problem set
Pencil problems first — they're quick and they make the quiz easy. The code problem is the classic rite of passage; budget an hour.
Logits over a 4-token vocab: . Compute the softmax probabilities at (two decimal places is fine). Then, without recomputing from scratch, say what happens to the probabilities if every logit has 10 added to it.
A model assigns the true next token probability 0.5 at position A and 0.001 at position B. What is the cross-entropy loss (in nats) at each position? How many positions like A does it take to “pay for” one position like B in the average loss?
For loss where and is the correct token, show that . Interpret the result in one sentence.
Follow Karpathy's micrograd video (linked in Go deeper) and build a tiny autograd engine: a Value class with +, *, tanh, and a backward() that applies the chain rule through the graph. Train a 2-layer net on XOR.
Success check: your gradients match finite differences to ~1e-4, and the XOR net reaches loss < 0.05.
Check yourself
Go deeper
One video series to actually do, one to watch, and references to keep.