Skip to content

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 f(x;θ)f(x; \theta) that turns an input xx into an output. The semicolon separates what you feed in (xx) from the parameters θ\theta, the numbers inside the function that you are free to change. The simplest example is a line:

f(x; w,b)=w x+bθ=(w,b)f(x;\,w,b)=w\,x+b\qquad \theta=(w,b)

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 θ\theta 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 (x1x_{1}, y1y_{1}), …, (xnx_{n}, yny_{n}) where yy 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:

L(θ)=1n∑i=1n(f(xi;θ)−yi)2L(\theta)=\frac1n\sum_{i=1}^{n}\bigl(f(x_i;\theta)-y_i\bigr)^2

LL is a function of θ\theta only, because the data are fixed. Training means finding θ\theta that makes LL 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.

For a function of many variables, the gradient is the vector of partial derivatives:

∇L(θ)=(∂L∂θ1,∂L∂θ2,…,∂L∂θp)\nabla L(\theta)=\Bigl(\frac{\partial L}{\partial\theta_1},\frac{\partial L}{\partial\theta_2},\dots,\frac{\partial L}{\partial\theta_p}\Bigr)

Take a small step δ\delta. A first-order Taylor expansion says

L(θ+δ)≈L(θ)+∇L(θ)⋅δL(\theta+\delta)\approx L(\theta)+\nabla L(\theta)\cdot\delta

The change in LL is a dot product, so it is largest when δ\delta points along ∇L\nabla L (steepest ascent) and most negative when δ\delta points against it. That gives the update rule of gradient descent:

θ←θ−η ∇L(θ)\theta\leftarrow\theta-\eta\,\nabla L(\theta)

η\eta (eta) is the learning rate. Substituting δ=−η∇L\delta = -\eta \nabla L into the expansion gives L(θ+δ)≈L(θ)−η∥∇L∥2L(\theta + \delta) \approx L(\theta) - \eta \lVert \nabla L\rVert ^{2}, a decrease, as long as η\eta is small enough for the approximation to hold.

For L(w,b)=(1/n)∑(w⋅xi+b−yi)2L(w, b) = (1 / n)\sum(w\cdot x_{i} + b - y_{i})^{2}, the chain rule gives

∂L∂w=2n∑i (wxi+b−yi) xi∂L∂b=2n∑i (wxi+b−yi)\begin{aligned}\frac{\partial L}{\partial w}&=\frac2n\sum_i\,(w x_i+b-y_i)\,x_i\\[4pt]\frac{\partial L}{\partial b}&=\frac2n\sum_i\,(w x_i+b-y_i)\end{aligned}

Each partial derivative is an average of the errors, weighted by the input the parameter multiplies. Repeating w←w−η⋅∂L/∂ww \leftarrow w - \eta \cdot \partial L / \partial w, b←b−η⋅∂L/∂bb \leftarrow b - \eta \cdot \partial L / \partial b 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.

Take the simplest bowl, f(x)=12⋅a⋅x2f(x) = \tfrac12 \cdot a\cdot x^{2}, with a>0a > 0. Its derivative is a⋅xa\cdot x, so one step gives

x←x−η a x=(1−ηa) xx\leftarrow x-\eta\,a\,x=(1-\eta a)\,x

Each step multiplies xx by (1−ηa)(1 - \eta a). That shrinks xx toward 0 only if ∣1−ηa∣<1\lvert1-\eta a\rvert<1, which means 0<η<2/a0 < \eta < 2 / a. The number aa 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 η\eta that makes progress along the valley slow. Chapter 3 shows how momentum and Adam address this.

Computing ∇L\nabla L over the whole dataset every step is wasteful when the dataset has trillions of tokens. Instead, take a random minibatch BB of examples and compute the gradient of the average loss over BB 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.

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).

For f(x) = ½·a·x² with a = 4, what range of learning rates converges?

0<η<2/a=0.50 < \eta < 2 / a = 0.5. At η=0.5\eta = 0.5 the factor (1−ηa)=−1(1 - \eta a) = -1, so xx oscillates between ±x0\pm x_{0} forever.

Why does −∇L give the direction of steepest descent?

To first order, the change in LL for a step δ\delta is ∇L⋅δ\nabla L\cdot \delta. For a fixed step length, a dot product is most negative when δ\delta points exactly opposite to ∇L\nabla L.

Compute ∂L/∂b for L = (1/2)(b − 3)². What is the update with η = 0.1 from b = 0?

∂L/∂b=b−3=−3\partial L / \partial b = b - 3 = -3. Update: b←0−0.1⋅(−3)=0.3b \leftarrow 0 - 0.1\cdot (-3) = 0.3.

  • 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.