Gradient descent by hand

Part 0, Neural networks from absolute zero

Learning objectives

  • Write one gradient-descent step and carry it out for two parameters
  • Say why the learning rate must stay below 2/λmax⁡2/\lambda_{\max} and what λmax⁡\lambda_{\max} is
  • Explain why a long valley makes gradient descent slow at any stable rate
  • Read what a rate past the limit does on a loss with plateaus

One Subtraction a Step

Gradient descent moves every parameter a step against its slope: θk+1=θk−η ∇L(θk)\theta_{k+1} = \theta_k - \eta\,\nabla L(\theta_k), one subtraction a parameter a step. On Section 0.4's valley, one neuron's weight and bias on the Marmousi2 column, the rate η\eta is capped by the valley's steepest curvature: past 2/λmax⁡=7.062/\lambda_{\max} = 7.06 the descent is thrown out of the valley, and below it the floor, which curves 413 times less, is crawled. From the seed's own random start, 200 steps at 10, 30 and 70 % of the limit end 254, 222 and 213 m/s RMS from the column, against the minimum's 211 m/s; only the last comes within 1 % of it, at step 168. At 150 % of the limit the first step throws the neuron onto the plateau, 843 m/s, and it never comes back.

Gradient Descent Step By StepInteractive figure, enable JavaScript to interact.

A Step Written Out

The descent starts where the trained neuron's own run started, its seed's Glorot draw: w=0.6574w = 0.6574, b=0b = 0, 891 m/s RMS, not chosen near the minimum (it is 5.23 from it in ww and 4.19 in bb). The output layer is held at the trained neuron's, as in Section 0.4. At step 0 the kernel's gradient is (∂L/∂w,∂L/∂b)=(0.1705,0.5593)(\partial L/\partial w, \partial L/\partial b) = (0.1705, 0.5593); at 70 % of the limit, η=4.939\eta = 4.939, the step is

w1=0.6574−4.939×0.1705=−0.1848,b1=0−4.939×0.5593=−2.762.w_1 = 0.6574 - 4.939 \times 0.1705 = -0.1848, \qquad b_1 = 0 - 4.939 \times 0.5593 = -2.762.

That is all a step is; the work is in the gradient, which Section 0.6 builds by the chain rule. Plate (c) writes out any step you choose. The column is the window at x = 3600 m of the Marmousi2 model, a 2D acoustic, constant-density slice of a synthetic model, not field data; the start, the rates and the 200 steps are the book's own.

The Limit

Near the minimum the loss is close to a quadratic with curvatures λmax⁡=0.2835\lambda_{\max} = 0.2835 and λmin⁡=0.0006866\lambda_{\min} = 0.0006866 along its two principal directions (Section 0.4). A step multiplies the error along a direction of curvature λ\lambda by 1−ηλ1 - \eta\lambda, so the steep direction shrinks only if ∣1−ηλmax⁡∣<1|1 - \eta\lambda_{\max}| < 1, that is η<2/λmax⁡=7.06\eta < 2/\lambda_{\max} = 7.06. Then the flat direction shrinks by a factor of at least 1−2λmin⁡/λmax⁡1 - 2\lambda_{\min}/\lambda_{\max} a step, which here is more than 0.995: hundreds of steps to make progress along the floor. The steepest direction caps the rate, and the flattest sets the pace.

Four Rates

At 10 % of the limit, η=0.71\eta = 0.71, the descent creeps down the wall and crawls along the floor: 254 m/s after 200 steps. At 30 %, 222 m/s. At 70 %, η=4.94\eta = 4.94, it reaches the floor and moves along it fastest, 213 m/s, within 1 % of the minimum from step 168. At 150 %, η=10.58\eta = 10.58, its first step carries it across the valley and out of it, onto the plateau where the neuron is off, 843 m/s RMS; there the slope is too small ever to bring it back. On a quadratic bowl a rate past the limit diverges; on a network's loss, with its plateaus, it can just as well stall.

Why Optimisers

Ruder (2016) surveys the gradient-descent variants and the challenges they meet, among them choosing the rate and the slow progress along ravines; momentum, Adam and L-BFGS, Section 0.8's optimisers, are answers to this valley. PINN losses, with several terms of very different curvature, are where the problem is worst (Part 3).

What Comes Next

Section 0.6 shows where the gradient in step (c) comes from: the chain rule, run backwards through the network.

References

  • Ruder, S. (2016). An overview of gradient descent optimization algorithms. arXiv:1609.04747.

This page is prerendered for SEO and accessibility. The interactive widgets above hydrate on JavaScript load.