Functions, parameters and gradient descent
Models, loss, and the gradient that trains them
Chapter 1 of 21 · I. Foundations · about 30 minutes. Needs only calculus and linear algebra.
A model is a function with adjustable numbers
Section titled “A model is a function with adjustable numbers”A model is a function that turns an input into an output. The semicolon separates what you feed in () from the parameters , the numbers inside the function that you are free to change. The simplest example is a line:
A modern language model is the same kind of object. It is a function from a sequence of tokens to a probability distribution over the next token, and is a list of billions of numbers (for example, 8 billion for Llama 3 8B). Everything in this guide is about how that function is built, how the numbers are found, and what can go wrong.
Loss: a single number that says how wrong the model is
Section titled “Loss: a single number that says how wrong the model is”Suppose you have examples (, ), …, (, ) where is the answer you want. Choose a loss that measures the error of one prediction, and average it over the data. For regression the usual choice is squared error:
is a function of only, because the data are fixed. Training means finding that makes small. For a line this is ordinary least squares. For a language model the loss is different (cross-entropy, Chapter 4) but the idea is identical.
The gradient points uphill
Section titled “The gradient points uphill”For a function of many variables, the gradient is the vector of partial derivatives:
Take a small step . A first-order Taylor expansion says
The change in is a dot product, so it is largest when points along (steepest ascent) and most negative when points against it. That gives the update rule of gradient descent:
(eta) is the learning rate. Substituting into the expansion gives , a decrease, as long as is small enough for the approximation to hold.
Worked example: fitting a line
Section titled “Worked example: fitting a line”For , the chain rule gives
Each partial derivative is an average of the errors, weighted by the input the parameter multiplies. Repeating , moves the line toward the data.
Try it: the learning rate decides everything
Section titled “Try it: the learning rate decides everything”Below, a function with two valleys. Press “25 steps” and change the learning rate and starting point. Notice three behaviors: a small rate is slow but safe, a moderate rate lands in whichever valley is downhill from the start, and a large rate overshoots and diverges.
Why too large a step diverges
Section titled “Why too large a step diverges”Take the simplest bowl, , with . Its derivative is , so one step gives
Each step multiplies by . That shrinks toward 0 only if , which means . The number is the curvature. For many dimensions, the same argument applies along each direction of the loss surface, and the largest curvature (the largest eigenvalue of the Hessian matrix of second derivatives) sets the stability limit. A narrow valley, where curvature is large in one direction and small in another, forces a small that makes progress along the valley slow. Chapter 3 shows how momentum and Adam address this.
Stochastic gradient descent
Section titled “Stochastic gradient descent”Computing over the whole dataset every step is wasteful when the dataset has trillions of tokens. Instead, take a random minibatch of examples and compute the gradient of the average loss over only. This estimate is unbiased (its expected value is the true gradient) and noisy. The noise is not just a cost: a very large batch gives a more accurate direction but few updates, while a small batch gives many noisy ones. Language models are trained with batches of millions of tokens.
Training loss is not the goal
Section titled “Training loss is not the goal”A model with enough parameters can memorize its training examples and still fail on new ones. We therefore keep some data aside, the validation set, and report the loss there. Training and validation loss that diverge indicate overfitting. This will matter again in security: a model that has memorized training text can be made to reveal it (Chapter 20).
Check yourself
Section titled “Check yourself”For f(x) = ½·a·x² with a = 4, what range of learning rates converges?
. At the factor , so oscillates between forever.
Why does −∇L give the direction of steepest descent?
To first order, the change in for a step is . For a fixed step length, a dot product is most negative when points exactly opposite to .
Compute ∂L/∂b for L = (1/2)(b − 3)². What is the update with η = 0.1 from b = 0?
. Update: .
Papers and books
Section titled “Papers and books”- Chapters 4 (numerical computation) and 5 (machine learning basics) cover this material carefully.
- A modern textbook; chapters 2–4 and 6 are the companion to this chapter.
- Free, with runnable code for every concept.