Out of Sample
about

Part I, Optimisation, generalisation, adaptation

Downhill

Training a network is walking down a surface you cannot see, one gradient at a time. How big a step you take matters more than almost anything else.

Assumes partial derivatives, eigenvalues of a symmetric matrix.

Every network you will train in this course is fitted the same way. You have parameters θ\theta, a loss L(θ)L(\theta) that measures how wrong the network is on the training data, and the gradient ∇L(θ)\nabla L(\theta), which points uphill. You step the other way:

θt+1=θt−η ∇L(θt).\theta_{t+1} = \theta_t - \eta \, \nabla L(\theta_t).

The step size η\eta is called the learning rate.“Rate” is a misnomer we are stuck with. It is a length scale, not a speed: the distance moved is η\eta times the length of the gradient. Real networks have millions of parameters, but almost everything that goes right or wrong with gradient descent can be seen with two. The figure below shows two parameters, their loss as contour lines, and the path the parameters take.

The step that is too large

Start on the bowl at a step size of 0.60 and drag it upward. At first the path just gets there sooner. Past 1 it starts to zig-zag across the steep direction of the bowl, and past 2 it stops converging and flies off.

That threshold is no accident. Near a minimum every smooth loss looks like a quadratic, L(θ)≈12θ⊤HθL(\theta) \approx \tfrac12 \theta^\top H \theta, where HH is the Hessian. Write θ\theta in the eigenbasis of HH, with eigenvalues λi\lambda_i. Each coordinate then evolves on its own:

θt+1(i)=(1−ηλi) θt(i).\theta^{(i)}_{t+1} = (1 - \eta \lambda_i)\, \theta^{(i)}_t .

A coordinate shrinks only if ∣1−ηλi∣<1|1 - \eta\lambda_i| \lt 1, that is, if η<2/λi\eta \lt 2/\lambda_i. The largest curvature sets the limit for the whole problem:

η<2λmax⁡.\eta \lt \frac{2}{\lambda_{\max}}.

In the bowl here, λmax⁡=1\lambda_{\max} = 1, so the edge is at η=2\eta = 2.The readout beside the figure reports ηλmax⁡\eta\lambda_{\max} for the bowl. Watch it cross 2.

The direction that is too flat

The other coordinate shrinks by 1−ηλmin⁡1 - \eta\lambda_{\min} each step. If the step is held under the limit set by the steep direction, the flat direction crawls. The ratio κ=λmax⁡/λmin⁡\kappa = \lambda_{\max}/\lambda_{\min}, the condition number, measures how bad the mismatch is. Push the bowl’s elongation κ up and watch the number of steps needed grow roughly in proportion.

Badly conditioned losses are the normal case in deep learning, not the exception. Much of what follows in this course, including normalisation layers, careful initialisation and adaptive optimisers, is about making κ\kappa smaller, or acting as if it were.

Momentum

Heavy-ball momentum keeps a running velocity:

vt+1=βvt−η∇L(θt),θt+1=θt+vt+1.v_{t+1} = \beta v_t - \eta \nabla L(\theta_t), \qquad \theta_{t+1} = \theta_t + v_{t+1}.

Along the steep direction, successive gradients alternate in sign and largely cancel. Along the flat direction they agree and accumulate. Set momentum to 0.9 on the elongated bowl and compare the step counts. Then try the valley surface, Rosenbrock’s banana, where the floor curves and momentum has to turn a corner.

Adam goes further and rescales each coordinate by a running estimate of its gradient’s size.Kingma and Ba, 2015. In effect it is a cheap, diagonal guess at the curvature. On the bowl it is nearly indifferent to κ. On the saddle surface, start near the ridge and compare how long plain descent and Adam take to escape it.

Exercise

For the quadratic L=12θ⊤HθL = \tfrac12 \theta^\top H\theta with fixed step η\eta, find the η\eta that makes the slowest coordinate shrink as fast as possible, and the resulting rate.

Solution

The worst factor is max⁡(∣1−ηλmin⁡∣,∣1−ηλmax⁡∣)\max(|1-\eta\lambda_{\min}|, |1-\eta\lambda_{\max}|). It is smallest when the two are equal, at η⋆=2/(λmin⁡+λmax⁡)\eta^\star = 2/(\lambda_{\min}+\lambda_{\max}). The rate is then (κ−1)/(κ+1)(\kappa-1)/(\kappa+1) per step, which is close to 1 when κ\kappa is large.

Exercise

On the bowl with κ = 50 and a step size of 0.6, use the figure to find the momentum that reaches within 0.001 of the minimum in the fewest steps. Explain what goes wrong as β approaches 1.