Training loops and optimisers

Part 0, Neural networks from absolute zero

Learning objectives

  • Write the training loop every optimiser shares: the loss and its gradient, then a step
  • State the step rules of gradient descent, momentum, Adam and L-BFGS, and read Adam's first step as its rate in every parameter
  • Compare the four on one network, one start and one training set by their losses and their fits of the column after 1000 steps
  • Say what L-BFGS's line search and memory cost, and how the book's PINNs combine Adam and L-BFGS

One Loop, Four Steps

Every optimiser runs the same loop: evaluate the loss and its gradient (Section 0.6), then step the parameters. What differs is the step. On one network, one seeded start and one training set, the book's fit of the Marmousi column, 1000 steps of gradient descent end 303 m/s RMS from the column, momentum 219 m/s, Adam 191 m/s and L-BFGS 41 m/s; L-BFGS reaches Adam's last loss by step 70. The step rule decides what the same number of steps buys.

Optimisers On The ColumnInteractive figure, enable JavaScript to interact.

The Problem

The network has two hidden layers of 16 tanh units, 321 parameters, and fits the Marmousi trace at x = 3600 m, the velocity over 1500 m/s against the depth over 1800 m, at 176 depths every 10 m. The loss is the mean squared misfit, full batch: every step sees every depth. The four runs are the book's own, trained once and kept in its training table, each from the same seed's network, whose loss is 2.53; plate (b)'s fits are scored against the column every 5 m.

Gradient Descent and Momentum

Gradient descent moves each parameter by the rate times its partial, θ←θ−ηg\theta \leftarrow \theta - \eta g, here at η=0.1\eta = 0.1. Its first step in plate (c) lies on a line of slope one: a parameter with a small partial hardly moves. After 1000 steps its loss is 0.0411. Momentum keeps a running sum of the partials, m←μm+gm \leftarrow \mu m + g and θ←θ−ηm\theta \leftarrow \theta - \eta m, here at η=0.02\eta = 0.02 and μ=0.9\mu = 0.9, so a direction that keeps its sign gathers speed; after 1000 steps its loss is 0.0219.

Adam

Kingma and Ba (2014) introduced Adam for first-order gradient-based optimisation of stochastic objective functions; its updates are invariant to a diagonal rescaling of the gradients. It keeps a running mean mm and a running mean square vv of each partial, corrects both for their start at zero, and moves each parameter by η m^/(v^+ϵ)\eta\, \hat m / (\sqrt{\hat v} + \epsilon). After one step m^/v^\hat m / \sqrt{\hat v} is the partial's sign, so Adam's first step moves every one of the 321 parameters by its rate, 0.01, whatever its partial: plate (c)'s points lie on a flat line. After 1000 steps its loss is 0.0167 and its fit 191 m/s RMS from the column.

L-BFGS

Liu and Nocedal (1989) studied L-BFGS, a limited-memory quasi-Newton method: it keeps the last mm steps of the parameters and the changes of the gradient over them, and a two-loop recursion applies the inverse-Hessian estimate those pairs define to the gradient. They showed it greatly accelerated by a simple scaling of its starting matrix, H0=(s⊤y/y⊤y) IH_0 = (s^\top y / y^\top y)\, I, which the book's kernel uses, with a step length from a line search that satisfies the Wolfe conditions. The book's run keeps 10 pairs and uses a strong Wolfe search. It reaches Adam's last loss, 0.0167, by step 70 and ends at 0.000764, 41 m/s RMS from the column. Its steps are dearer: over its 1000 iterations its line search evaluated the loss and its gradient 1246 times, and its memory holds 20 vectors of 321 numbers.

Which One

On a small, smooth, full-batch problem like this one, L-BFGS's use of the curvature pays. The book's wave PINNs (Part 4) take Adam's steps first, 2500 for the 1D wave and 2000 for the 2D, and then 500 of L-BFGS from where Adam left them.

What Comes Next

Section 0.9 asks what even the best of these fits leaves behind: the column's short wavelengths.

References

  • Kingma, D. P. and Ba, J. (2014). Adam: A method for stochastic optimization. arXiv:1412.6980.
  • Liu, D. C. and Nocedal, J. (1989). On the limited memory BFGS method for large scale optimization. Mathematical Programming, 45(3), 503-528.

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