Chapter 2

Gradient descent

Gradient descent is a method that improves the parameters of a model in many small steps, where each step moves every parameter a little in the direction that makes the loss smaller1. For example, the taxi model of Chapter 1 can start from a=0a = 0 and b=0b = 0 and get within 0.01 of the smallest loss after 144 steps.

Trying all values needs 11k11^k tries for kk parameters (see Section 1.2). The cost of one step of gradient descent grows only linearly with kk, because the step calculates one derivative for each parameter. For this reason, gradient descent can train models with billions of parameters.

2.1One parameter

Suppose that the training data has only one ride of 1 km that cost 3 dollars, and that the model is y=a⋅xy = a \cdot x. The loss is L(a)=(a−3)2L(a) = (a - 3)^2, and it is smallest when a=3a = 3.

The derivative L′(a)L'(a) tells how fast the loss changes when aa grows. In this case, L′(a)=2(a−3)L'(a) = 2(a - 3). When the derivative is negative, a larger aa gives a smaller loss, and when it is positive, a smaller aa does. For example, L′(0)=−6L'(0) = -6, so the loss falls when aa grows from 0.

Each step of gradient descent changes the parameter as follows: a←a−η⋅L′(a)a \leftarrow a - \eta \cdot L'(a) where η\eta is the learning rate. As an example, consider the steps when η=0.25\eta = 0.25 and the training starts from a=0a = 0:

step 0 1 2 3 4
aa 0 1.5 2.25 2.625 2.8125
L′(a)L'(a) −6 −3 −1.5 −0.75 −0.375
L(a)L(a) 9 2.25 0.5625 0.1406 0.0352

In this case, each step halves the distance between aa and 3, because a−0.25⋅2(a−3)=a−(a−3)/2a - 0.25 \cdot 2(a - 3) = a - (a - 3)/2. The following picture shows the loss and the first three steps, where the number next to each point is its step:

1 2 3 4 5 6 3 6 9 0 a L(a) 0 1 2 3

The steps become shorter near the minimum, because the derivative becomes smaller.

2.2The learning rate

The learning rate sets the length of the steps. With η=0.25\eta = 0.25, the parameter gets within 0.03 of 3 after 7 steps. With η=0.05\eta = 0.05, each step removes only 10% of the distance, and the same accuracy needs 44 steps.

However, a learning rate that is too large makes the training fail. With η=1\eta = 1, the step is a←6−aa \leftarrow 6 - a, so aa jumps between 0 and 6 and never gets nearer to 3. With η=1.25\eta = 1.25, the values of aa are 0, 7.5, −3.75 and 13.125, and the distance to 3 grows by half at every step.

In practice, we can choose the learning rate by trial on short training runs, and the learning rate is often made smaller as the training goes on.

2.3Many parameters

The gradient of the loss is the vector of its derivatives with respect to each parameter. It points in the direction where the loss grows fastest, so gradient descent moves the parameters against it. For the taxi model y=a⋅x+by = a \cdot x + b with nn rides, the derivatives are as follows:

∂L∂a=2n∑i=1n(axi+b−yi)xi∂L∂b=2n∑i=1n(axi+b−yi)\frac{\partial L}{\partial a} = \frac{2}{n} \sum_{i=1}^{n} (a x_i + b - y_i)\, x_i \qquad \frac{\partial L}{\partial b} = \frac{2}{n} \sum_{i=1}^{n} (a x_i + b - y_i)

For example, at a=0a = 0 and b=0b = 0, the errors of the three rides are −5, −6 and −9. In this case, ∂L/∂a=(2/3)(−5⋅1−6⋅2−9⋅3)=−88/3\partial L / \partial a = (2/3)(-5 \cdot 1 - 6 \cdot 2 - 9 \cdot 3) = -88/3 and ∂L/∂b=(2/3)(−5−6−9)=−40/3\partial L / \partial b = (2/3)(-5 - 6 - 9) = -40/3. The following table shows the training with η=0.05\eta = 0.05:

step aa bb loss
0 0 0 47.33
1 1.47 0.67 9.82
2 2.12 0.97 2.37
10 2.58 1.34 0.47
100 2.20 2.22 0.25
500 2.00 2.66 0.22

The loss falls fast in the first steps and slowly after that. Note that aa first grows past its best value 2 and then comes back, while bb grows slowly to 8/3. With η=0.2\eta = 0.2, the same training diverges.

Each step calculates the error of each ride and one derivative for each parameter, so a step takes O(nk)O(nk) time for nn examples and kk parameters. For the taxi model, we can write the derivatives by hand. For a neural network, Chapter 3 shows how backpropagation calculates them.

Gradient descent stops moving at a point where the gradient is 0. The loss of the taxi model has only one such point, but the loss of a neural network has many, and the result depends on the start.

2.4Batches

When the training data has millions of examples, a step over all of them is slow. Stochastic gradient descent2 calculates each step from a random batch of mm examples instead. For example, the following table shows the gradient at a=0a = 0 and b=0b = 0 for batches of one ride:

batch ∂L/∂a\partial L / \partial a ∂L/∂b\partial L / \partial b
ride 1 −10 −10
ride 2 −24 −12
ride 3 −54 −18
all rides −29.33 −13.33

Each batch gives a different gradient, but the average of the three batch gradients is exactly the gradient of all rides. Thus, the steps of stochastic gradient descent are noisy but correct on average, and each step costs only m/nm/n of a full step. In practice, language models are trained on batches of millions of tokens.

Most large models are trained with Adam3, a variant of stochastic gradient descent. Adam keeps two running averages for each parameter, one of its gradient and one of its squared gradient, and it scales each step by the recent size of the gradient. These averages and the gradient are the extra numbers per parameter that Section 11.3 counts.


  1. The method was published in 1847 by A. Cauchy: Méthode générale pour la résolution des systèmes d’équations simultanées.↩︎

  2. Stochastic gradient descent is based on a method published in 1951 by H. Robbins and S. Monro: A Stochastic Approximation Method.↩︎

  3. The method was published in 2014 by D. P. Kingma and J. Ba: Adam: A Method for Stochastic Optimization.↩︎