MoonSay /Tutorials/Machine Learning Interview #9
Contents
  1. 01Introduction
  2. 02Gradient descent
  3. 03The learning rate
  4. 04Batch gradient descent
  5. 05Stochastic gradient descent
  6. 06Mini-batch gradient descent
  7. 07Momentum and Adam
  8. 08Common misconceptions
  9. 09Interview questions and model answers
  10. 10Summary
  11. References
Gradient Descent/Chapter 03 of 10

03The learning rate

3.1One dimension: a bowl

Take the simplest loss, a bowl J(\theta)=\frac a2\theta^2 with curvature a>0 and minimum at 0. Its gradient is a\theta, so the update (2) is

\theta_{t+1}=\theta_t-\eta a\,\theta_t=(1-\eta a)\,\theta_t,\qquad\text{so}\qquad \theta_t=(1-\eta a)^t\,\theta_0. \tag{4}

Everything depends on the factor 1-\eta a (Figure 1, with a=1 and \theta_0=-2.5):

  • 0<\eta a<1: we approach from one side; at \eta a=0.1 each step removes only 10\% of the distance: we crawl.
  • \eta a=1: the factor is 0 and we land on the minimum in one step (Newton’s step, \eta=1/a).
  • 1<\eta a<2: the factor is negative, so we overshoot and bounce, but still converge (-0.5 at \eta a=1.5).
  • \eta a>2: every step overshoots more than the last; at \eta a=2.1 the distance grows 10\% and the loss 21\% per step.

So gradient descent converges on the bowl if and only if 0<\eta<2/a.

Gradient descent on the bowl J=\frac a2\theta^2 with a=1, starting at \theta_0=-2.5 (open circle), for four learning rates. Each step multiplies \theta by 1-\eta a: 0.9, 0, -0.5 and -1.1. Computed for this tutorial, with the same values as the companion video.
Figure 1. Gradient descent on the bowl J=\frac a2\theta^2 with a=1, starting at \theta_0=-2.5 (open circle), for four learning rates. Each step multiplies \theta by 1-\eta a: 0.9, 0, -0.5 and -1.1. Computed for this tutorial, with the same values as the companion video.
Try itFind the learning-rate limit

Move the learning rate. On the bowl J=\tfrac a2\theta^2 every step multiplies \theta by 1-\eta a, equation (4). Left: the first steps on the bowl. Right: the distance to the minimum, step by step, on a log scale.

3.2Many dimensions: the eigenvalues decide

Near a minimum, any smooth loss looks like a quadratic, J(\theta)\approx J^*+\frac12(\theta-\theta^*)^{\top}H(\theta-\theta^*), with gradient H(\theta-\theta^*). The error e_t=\theta_t-\theta^* then evolves as

e_{t+1}=(I-\eta H)\,e_t . \tag{5}

Write e_t in the eigenvectors of H. Each coordinate is an independent bowl with curvature \lambda_i and is multiplied by 1-\eta\lambda_i at every step. All coordinates converge if and only if |1-\eta\lambda_i|<1 for all i, that is

0<\eta<\frac{2}{\lambda_{\max}} . \tag{6}

The steepest direction sets the speed limit. The slowest direction sets the speed: the error shrinks by \max_i|1-\eta\lambda_i| per step. The best constant learning rate balances the flattest and steepest directions, \eta=2/(\lambda_{\min}+\lambda_{\max}), and gives the rate

\max_i|1-\eta\lambda_i|=\frac{\kappa-1}{\kappa+1},\qquad \kappa=\frac{\lambda_{\max}}{\lambda_{\min}} . \tag{7}

With \kappa=100 the error shrinks by only 99/101=0.980 per step, so it takes about 115 steps to gain one decimal digit. A large condition number means a long, narrow valley: the steep walls force a small learning rate, and the flat floor is then crossed at a crawl (Goodfellow et al., 2016, §4.3, §8.2).

Worked example 1 (The regression is a narrow valley). For least squares the Hessian is H=\frac1n\sum_i\;(x_i,1)^{\top}(x_i,1), a constant matrix. In the running example its eigenvalues are 0.114 and 11.229, so \kappa=98.9 and the learning rate must stay below 2/11.229=0.178; we use 0.05. The valley is narrow because the inputs are not centered: x averages 3.011, so changing the slope w also shifts every prediction, and w and b fight each other. Replacing x by x-\bar x makes the Hessian eigenvalues 1.0 and 1.275, \kappa=1.27, and gradient descent would reach the bottom in a handful of steps. This is why inputs are normalized to zero mean and unit variance before training (LeCun et al., 1998, §4.3).

Pause and think. Your training loss suddenly becomes NaN after a few hundred steps. What is the first thing to try?

Show the answer

Lower the learning rate: an exploding loss is the signature of \eta above 2/\lambda_{\max} in some direction (or of a curvature that grew during training). Learning-rate warmup and gradient clipping protect against the same failure.