Math for ML, by Hand · part 3 of 4

Why Does Gradient Descent Bounce?

Math for ML, by Hand (part 3 of 4): derivatives, integrals, gradients, the chain rule and the Hessian, on a model you can train with a pencil.

Four columns listing twenty topics across four parts; Part 3 is highlighted. Frame 1.
You are in Part 3: fitting the model

Set the learning rate to 0.05 on our tiny model and the loss goes 1.386, 1.622, 1.436, 1.759. It rises, falls and rises again, and after 11 steps it is still above where it started. Nothing is wrong with the gradient.

By the end of this part the reason will be a single number, 0.031, read off the second derivatives of the loss. Getting there takes the whole of calculus for machine learning: slopes, areas, surfaces, gradients, the chain rule and curvature.

The tiny model

The cast is unchanged: A = [[2, 1], [1, 2]], w = (1, −1), b = 1. Both users get z = 0 and p = 0.5, and each has loss −ln 0.5 = 0.693, so the total over the two users is 1.386. Training means changing A, w and b to push that number down.

The derivative is the slope of a tangent

The derivative is the slope of the secant line in the limit as its two points merge.

Loss as a function of the bias. Secant slopes −0.380, −0.488, −0.499 approach the tangent slope −0.5. Frame 1.
The loss of the clicking user as we turn one knob, the bias b
Loss as a function of the bias. Secant slopes −0.380, −0.488, −0.499 approach the tangent slope −0.5. Frame 2.
Secant with h = 1: slope −0.380
Loss as a function of the bias. Secant slopes −0.380, −0.488, −0.499 approach the tangent slope −0.5. Frame 3.
h = 0.1: slope −0.488; h = 0.01: slope −0.499
Loss as a function of the bias. Secant slopes −0.380, −0.488, −0.499 approach the tangent slope −0.5. Frame 4.
In the limit: the tangent at b = 1 has slope −0.5 = p − y

Turn one knob, the bias b, and watch the clicking user's loss. At b = 1 it is 0.693. Take a second point h further along and draw the secant line through both. With h = 1 its slope is −0.380; with h = 0.1 it is −0.488; with h = 0.01 it is −0.499; with h = 0.001 it is −0.4999.

The slopes settle on −0.5. That limit is the derivative, the slope of the tangent at b = 1. For sigmoid followed by log-loss it has a clean closed form: ∂L/∂b = p − y = 0.5 − 1 = −0.5. Negative means increasing b lowers the loss.

At a minimum or maximum the derivative is 0, but a zero derivative alone does not prove either: f(x) = x³ has f′(0) = 0 and keeps rising through it. The second derivative decides. Here it is p(1 − p) = 0.25 > 0, so the curve bends upward.

By hand
h       secant slope (L(1+h) − L(1)) / h
1       −0.380
0.1     −0.488
0.01    −0.499
0.001   −0.4999
limit   p − y = 0.5 − 1 = −0.5
second derivative  p(1 − p) = 0.25

Why ML cares. Every weight update in training uses a derivative. Finite differences, the secant slopes above, are how practitioners check a hand-written gradient.

What it cannot do. One knob at a time, on a curve. Before moving to many knobs, the other half of calculus: area.

The integral is signed area

An integral adds up a function over an interval, counting area below the axis as negative.

The curve x squared minus 1 between 0 and 2. The part below the axis has signed area −2/3, the part above 4/3, total 2/3. Frame 1.
f(x) = x² − 1 on [0, 2]
The curve x squared minus 1 between 0 and 2. The part below the axis has signed area −2/3, the part above 4/3, total 2/3. Frame 2.
Tempting: area is always positive, so just add it up
The curve x squared minus 1 between 0 and 2. The part below the axis has signed area −2/3, the part above 4/3, total 2/3. Frame 3.
Below the axis counts negative: from 0 to 1 the integral is −2/3
The curve x squared minus 1 between 0 and 2. The part below the axis has signed area −2/3, the part above 4/3, total 2/3. Frame 4.
Above counts positive: from 1 to 2 it is 4/3
The curve x squared minus 1 between 0 and 2. The part below the axis has signed area −2/3, the part above 4/3, total 2/3. Frame 5.
Total signed area 2/3. Rectangles (Riemann sums) home in on it

Take f(x) = x² − 1 on [0, 2]. It dips below the axis until x = 1 and rises above it after. The tempting answer adds all the shaded area as positive. An integral keeps the sign: the part below contributes −2/3, the part above 4/3, and the total is 2/3.

Two routes give that number. The fast one uses the fact that integration undoes differentiation: x³/3 − x has derivative x² − 1, so the integral is its change from 0 to 2. The slow one cuts the interval into rectangles and adds their signed areas: 4, 8, 16 and 64 rectangles give sums that close in on 2/3.

By hand
F(x) = x³/3 − x,  F′(x) = x² − 1
F(1) − F(0) = −2/3      below the axis
F(2) − F(1) = 4/3       above the axis
total       = 2/3
midpoint rectangles: 4 → 0.6250, 8 → 0.6563, 16 → 0.6641, 64 → 0.6665

Why ML cares. The expected loss over a continuous distribution of inputs is an integral, and so are entropy and KL divergence for continuous variables. Training replaces each of these integrals with an average over a sample, which part 4 justifies.

What it cannot do. Still one variable. Our loss depends on many weights at once.

The loss as a surface

With two weights the loss is a height over a plane, a surface you can draw.

A 3-D loss surface over two weights and its contour map, a long narrow valley, with the start point marked. Frame 1.
Total loss of both users, as a function of the two weights (b fixed at 1)
A 3-D loss surface over two weights and its contour map, a long narrow valley, with the start point marked. Frame 2.
Seen from above: contour lines join points of equal loss
A 3-D loss surface over two weights and its contour map, a long narrow valley, with the start point marked. Frame 3.
The start, w = (1, −1), sits at 1.386

Fix b = 1 and let the two entries of w vary. Every pair (w₁, w₂) gives one total loss, so the loss is a surface over the plane of weights. Our starting point w = (1, −1) sits at height 1.386.

Seen from above, the surface becomes a contour map: each line joins weights with equal loss. These contours are long, thin and tilted. That shape is not decoration. It is the reason gradient descent struggles on this model, and section 16 measures it.

By hand
w = (1, −1):  z(x) = 0, z(x′) = 0
L = −ln 0.5 − ln 0.5 = 0.693 + 0.693 = 1.386

Why ML cares. A real network has millions of weights, so its loss surface cannot be drawn. The two-weight picture is still the right mental model, and the tools below work in any number of dimensions.

What it cannot do. A picture shows the shape, but training needs slopes in numbers.

Partial derivatives: freeze all but one

A partial derivative is an ordinary derivative along one axis, with every other variable held still.

Two slices of the loss through w = (1, −1), one along each weight, with tangent slopes making the gradient (−5, −5.5). Frame 1.
Freeze w₂ = −1 and vary only w₁: a slice of the surface
Two slices of the loss through w = (1, −1), one along each weight, with tangent slopes making the gradient (−5, −5.5). Frame 2.
Its slope at w₁ = 1 is ∂L/∂w₁ = −5 (clicking user)
Two slices of the loss through w = (1, −1), one along each weight, with tangent slopes making the gradient (−5, −5.5). Frame 3.
Freeze w₁ = 1 and vary w₂: slope ∂L/∂w₂ = −5.5
Two slices of the loss through w = (1, −1), one along each weight, with tangent slopes making the gradient (−5, −5.5). Frame 4.
Together: the gradient ∇L = (−5, −5.5)

Freeze w₂ at −1 and vary only w₁. The surface becomes a curve, and its slope at w₁ = 1 is an ordinary derivative: ∂L/∂w₁. Do the same along w₂. For the clicking user the two slopes are −5 and −5.5.

Stack them into a vector and you have the gradient, ∇L = (−5, −5.5). The closed form explains the numbers: z = w·h + b depends on w₁ through h₁ = 10 and on w₂ through h₂ = 11, and each is scaled by the same p − y = −0.5.

The two tangent lines span a tangent plane, the flat surface that best matches the loss near w. The gradient describes that plane completely.

By hand
∂L/∂w₁ = (p − y)·h₁ = −0.5 · 10 = −5
∂L/∂w₂ = (p − y)·h₂ = −0.5 · 11 = −5.5
∇L = [−5, −5.5]
finite differences (step 1e−6): [−5.000000, −5.500000]   ✓

Why ML cares. A framework's autograd computes this vector for every weight in the model on every step. The next section is what it does with it.

What it cannot do. The gradient is a direction and a steepness. It does not say how far to walk.

Gradient descent, and why it bounces

Step against the gradient, with a step size set by the steepest curvature.

Contour map of the loss. With learning rate 0.05 the path zig-zags and the loss bounces; with 0.02, below 2 over 65.47, it decreases every step to 1.204. Frame 1.
Start at w = (1, −1) on the contour map
Contour map of the loss. With learning rate 0.05 the path zig-zags and the loss bounces; with 0.02, below 2 over 65.47, it decreases every step to 1.204. Frame 2.
Tempting: walk along the gradient. It points uphill
Contour map of the loss. With learning rate 0.05 the path zig-zags and the loss bounces; with 0.02, below 2 over 65.47, it decreases every step to 1.204. Frame 3.
Step against it: −∇L points downhill
Contour map of the loss. With learning rate 0.05 the path zig-zags and the loss bounces; with 0.02, below 2 over 65.47, it decreases every step to 1.204. Frame 4.
Learning rate 0.05: the path zig-zags; loss 1.386, 1.622, 1.436, 1.759…
Contour map of the loss. With learning rate 0.05 the path zig-zags and the loss bounces; with 0.02, below 2 over 65.47, it decreases every step to 1.204. Frame 5.
The Hessian's directions: curvature 65.47 across the valley, 0.034 along it
Contour map of the loss. With learning rate 0.05 the path zig-zags and the loss bounces; with 0.02, below 2 over 65.47, it decreases every step to 1.204. Frame 6.
Stable steps need η < 2/65.47 = 0.031
Contour map of the loss. With learning rate 0.05 the path zig-zags and the loss bounces; with 0.02, below 2 over 65.47, it decreases every step to 1.204. Frame 7.
η = 0.02: every step goes down, 1.386, 1.249, 1.239… 1.204 after 12
Contour map of the loss. With learning rate 0.05 the path zig-zags and the loss bounces; with 0.02, below 2 over 65.47, it decreases every step to 1.204. Frame 8.
Still wrong about x′ (p = 0.596). The flat direction makes progress slow

The gradient points the way the loss rises fastest. A step of 0.01 along it raises the clicking user's loss from 0.693 to 0.770. The same step against it lowers the loss to 0.622, lower than a step of the same length along the w₁ axis (0.644) or the w₂ axis (0.640). So we subtract the gradient, scaled by a learning rate η.

With both users and η = 0.05, the loss goes 1.386, 1.622, 1.436, 1.759: it bounces, and after 11 steps it is still above its start. The path in weight space zig-zags across the narrow valley from section 14, overshooting the floor on every step.

The second derivatives say why. Their matrix, the Hessian, is H = [[29, 32.5], [32.5, 36.5]] for our two users, with eigenvalues 65.47 and 0.034. Along the steep direction the loss behaves like a parabola with curvature 65.47, and any step size above 2/65.47 = 0.031 overshoots it and lands higher on the far wall. 0.05 is above that limit.

At η = 0.02, below the limit, every step goes down: 1.386, 1.249, 1.239, reaching 1.204 after 12 steps. The descent is also slow, because the valley is about 1,900 times steeper across than along. After 12 steps the model still gives the user who did not click p = 0.596, and it takes 104 steps before both users are on the right side of 0.5. Momentum, Adam and feature scaling all exist to deal with this ratio.

By hand
total ∇L at w = (1, −1):  (−5, −5.5) + 0.5·(4, 5) = (−3, −3)
H = [[29, 32.5], [32.5, 36.5]]   eigenvalues 0.034, 65.47
η_max = 2 / 65.47 = 0.031
η = 0.05: 1.386 → 1.622 → 1.436 → 1.759 …   bounces
η = 0.02: 1.386 → 1.249 → 1.239 → … → 1.204 (12 steps, every one lower)

Why ML cares. Learning-rate warm-up, gradient clipping and adaptive optimizers are all responses to the same fact: the safe step depends on curvature, and curvature differs wildly between directions.

What it cannot do. A gradient only exists once we can differentiate through the whole model. Our model is four boxes in a row, which needs the chain rule.

The chain rule, box by box (backpropagation)

Backpropagation multiplies the local derivative of each box, from the loss backwards, reusing each result.

The model's four boxes with forward values; gradients flow back: −0.5 at z, (−0.5, 0.5) at h, and the outer product [[−1.5, −2], [1.5, 2]] for A, matched by finite differences. Frame 1.
Forward pass with the numbers
The model's four boxes with forward values; gradients flow back: −0.5 at z, (−0.5, 0.5) at h, and the outer product [[−1.5, −2], [1.5, 2]] for A, matched by finite differences. Frame 2.
Start at the end: ∂L/∂z = p − y = −0.5
The model's four boxes with forward values; gradients flow back: −0.5 at z, (−0.5, 0.5) at h, and the outer product [[−1.5, −2], [1.5, 2]] for A, matched by finite differences. Frame 3.
Back through z = w·h + b: ∂L/∂h = −0.5 · w = (−0.5, 0.5)
The model's four boxes with forward values; gradients flow back: −0.5 at z, (−0.5, 0.5) at h, and the outer product [[−1.5, −2], [1.5, 2]] for A, matched by finite differences. Frame 4.
Back through h = Ax: ∂L/∂A = (∂L/∂h) xᵀ = [[−1.5, −2], [1.5, 2]]
The model's four boxes with forward values; gradients flow back: −0.5 at z, (−0.5, 0.5) at h, and the outer product [[−1.5, −2], [1.5, 2]] for A, matched by finite differences. Frame 5.
Check by nudging each entry of A by 10⁻⁶: the same four numbers
The model's four boxes with forward values; gradients flow back: −0.5 at z, (−0.5, 0.5) at h, and the outer product [[−1.5, −2], [1.5, 2]] for A, matched by finite differences. Frame 6.
One forward pass, one backward pass, every gradient

The model is four boxes: h = Ax, z = w·h + b, p = σ(z), and the log-loss. To train A we need four numbers, one per entry. The chain rule says to multiply local derivatives, starting at the loss and walking back.

The last two boxes collapse into one clean number: for sigmoid plus log-loss, ∂L/∂z = p − y = −0.5. The box before is z = w·h + b, whose derivative with respect to h is w, so ∂L/∂h = −0.5·w = (−0.5, 0.5).

The first box is h = Ax. For a vector-to-vector map the derivative is a matrix, the Jacobian, and for h = Ax with respect to x it is A itself. With respect to A, the gradient is an outer product: the column ∂L/∂h times the row xᵀ, giving [[−1.5, −2], [1.5, 2]].

Check it the slow way: nudge each of A's four entries by 10⁻⁶ and rerun the model. The four finite-difference slopes match to six decimals. Backpropagation is the bookkeeping that saves us from running the model once per weight.

By hand
forward:   h = [10, 11],  z = 0,  p = 0.5,  L = 0.693
backward:  ∂L/∂z = −0.5
           ∂L/∂h = −0.5 · [1, −1] = [−0.5, 0.5]
           ∂L/∂A = [−0.5, 0.5]ᵀ [3, 4] = [[−1.5, −2], [1.5, 2]]
           ∂L/∂x = Aᵀ [−0.5, 0.5] = [−0.5, 0.5]
check:     finite differences on A = [[−1.5, −2], [1.5, 2]]   ✓

Why ML cares. PyTorch and JAX build this backward pass automatically from the forward code. Knowing its shape explains vanishing gradients (many small factors multiplied together) and why activations are stored during the forward pass.

What it cannot do. Gradients say which way is down. They do not say whether a flat spot is a minimum, which is the Hessian's job.

The Hessian: reading the shape of a flat spot

The signs of the Hessian's eigenvalues say whether a flat spot is a minimum, a maximum or a saddle.

A bowl, a cap and a saddle with their Hessian eigenvalues; the cap has positive determinant too. Our model's Hessian has determinant 0 with one user and 2.25 with two. Frame 1.
Three flat spots: a bowl, a cap and a saddle
A bowl, a cap and a saddle with their Hessian eigenvalues; the cap has positive determinant too. Our model's Hessian has determinant 0 with one user and 2.25 with two. Frame 2.
Tempting: det H > 0 means a minimum. The cap also has det = 4
A bowl, a cap and a saddle with their Hessian eigenvalues; the cap has positive determinant too. Our model's Hessian has determinant 0 with one user and 2.25 with two. Frame 3.
The eigenvalue signs decide: all positive is a minimum, mixed is a saddle
A bowl, a cap and a saddle with their Hessian eigenvalues; the cap has positive determinant too. Our model's Hessian has determinant 0 with one user and 2.25 with two. Frame 4.
Our model with one user: det H = 0, eigenvalues 0 and 55.25. Flat in one direction
A bowl, a cap and a saddle with their Hessian eigenvalues; the cap has positive determinant too. Our model's Hessian has determinant 0 with one user and 2.25 with two. Frame 5.
With both users: det = 2.25, eigenvalues 0.034 and 65.47. A minimum, but nearly flat

A bowl, a cap and a saddle all have zero gradient at the centre. The Hessian, the matrix of second derivatives, tells them apart. A common shortcut says: positive determinant means minimum. The cap breaks it. Its Hessian is [[−2, 0], [0, −2]], with determinant 4, the same as the bowl's.

The reliable test reads the eigenvalues. All positive is a minimum, all negative a maximum, mixed signs a saddle. In two dimensions the determinant is the product of the two eigenvalues, so it can only tell you whether their signs agree, not which sign they share.

Our own model shows a second failure. With only the clicking user, H = [[25, 27.5], [27.5, 30.25]] and det H = 0, so the determinant test says nothing. The eigenvalues, 0 and 55.25, explain it: the loss is completely flat in one direction, because one data point cannot pin down two weights. Adding the second user gives det H = 2.25, eigenvalues 0.034 and 65.47: a minimum, but in a valley that is almost flat along its floor.

By hand
one user:   H = 0.25·[[100, 110], [110, 121]] = [[25, 27.5], [27.5, 30.25]]
            det = 756.25 − 756.25 = 0     eigenvalues 0, 55.25
two users:  H = [[29, 32.5], [32.5, 36.5]]
            det = 1058.5 − 1056.25 = 2.25   eigenvalues 0.034, 65.47

Why ML cares. Large networks have far more saddles than poor local minima, and flat directions are everywhere. Second-order optimizers and sharpness-aware training work directly with this curvature.

What it cannot do. Everything here treats p as a number to push around. Part 4 asks what it means.

Where this leaves us

The one number to remember is 2/λmax. The gradient tells you which way to step; the largest curvature tells you how far you can step without overshooting.

Part 4 turns to what p means. A probability of 0.5 is a statement about the long run, the loss is an expected value, and the log-loss is a cross-entropy, which is also why the 0.693 keeps appearing.

Next: part 4, Why Is Your Loss Function an Expected Value?