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 05 of 10

05Stochastic gradient descent

5.1One random example per step

Stochastic gradient descent (SGD) goes to the other extreme: at each step it picks one example i_t uniformly at random and steps along that example’s gradient,

\theta_{t+1}=\theta_t-\eta_t\,\nabla\ell_{i_t}(\theta_t). \tag{9}

A step is n times cheaper than a batch step. In the time batch gradient descent takes one step, SGD takes n.

5.2Unbiased but noisy

Proposition 1 (Unbiasedness). If i is drawn uniformly from \{1,\dots,n\}, then \E_i[\nabla\ell_i(\theta)]=\nabla J(\theta).

\E_i[\nabla\ell_i(\theta)]=\sum_{i=1}^{n}\;\frac1n\nabla\ell_i(\theta)=\nabla\Big(\frac1n\sum_{i=1}^{n}\;\ell_i(\theta)\Big)=\nabla J(\theta).

On average, the stochastic gradient is the true gradient; individually, it can be far off. Its spread is measured by the variance \sigma^2(\theta)=\E_i\|\nabla\ell_i(\theta)-\nabla J(\theta)\|^2. At the start point of the running example the true gradient has length 12.72 and the typical single-example gradient misses it by about the same amount, and 18.3\% of the examples have a gradient that points uphill, with a negative component along \nabla J (Figure 3, top row). Stepping along one of them makes the loss worse. Yet the steps are right on average, so the path zigzags but heads downhill. The noise is not only a cost: on the saddle points and flat regions of a deep network’s loss, the random kicks keep SGD moving where the exact gradient is almost zero.

5.3The noise ball and the Robbins–Monro conditions

Near the minimum the true gradient vanishes, but the single-example gradients do not: each example still has its own residual. With a constant learning rate SGD therefore never settles; it keeps bouncing in a noise ball around \theta^*. For a strongly convex loss, the expected excess loss converges to a level proportional to the learning rate (Bottou et al., 2018, Theorem 4.6): halve \eta and you halve the ball, but you also halve the speed. To converge to the minimum itself, the learning rate must decay. Robbins and Monro (1951) gave the classic conditions:

\sum_{t=1}^{\infty}\;\eta_t=\infty,\qquad \sum_{t=1}^{\infty}\;\eta_t^2<\infty . \tag{10}

The first condition lets the steps travel any distance, so the iterates can reach the minimum from anywhere; the second makes the total injected noise finite, so the bouncing dies out. A schedule \eta_t=\eta_0\,\gamma/(\gamma+t) satisfies both (the harmonic series diverges, the sum of 1/t^2 converges) and gives an expected excess loss that falls like 1/t (Bottou et al., 2018, Theorem 4.7).

Worked example 2 (Constant versus decaying learning rate). Start SGD exactly at \theta^* of the running example and run 2{,}500 steps (Figure 2). With the constant \eta=0.05 the iterates wander off and stay at an average distance of 0.121 from \theta^* over the last 1{,}000 steps, with an average excess loss J-J^*=0.0581. With \eta_t=0.05\cdot200/(200+t), which has fallen to 0.0037 by the end, the average distance is 0.056 and the excess loss 0.0042, fourteen times smaller.

SGD started at the minimum of the running example. (a) The last 1{,}000 of 2{,}500 iterates: a constant learning rate keeps bouncing in a wide noise ball; a decaying one settles near \theta^* (star). (b) Distance to \theta^*, averaged over 100 steps. Computed for this tutorial, with the same data and seed as the companion video.
Figure 2. SGD started at the minimum of the running example. (a) The last 1{,}000 of 2{,}500 iterates: a constant learning rate keeps bouncing in a wide noise ball; a decaying one settles near \theta^* (star). (b) Distance to \theta^*, averaged over 100 steps. Computed for this tutorial, with the same data and seed as the companion video.

Pause and think. Does \eta_t=\eta_0/\sqrt t satisfy the Robbins–Monro conditions?

Show the answer

The first, yes: \sum1/\sqrt t diverges. The second, no: \sum1/t also diverges. In practice such schedules are still used, often with averaging of the iterates, but the classic guarantee needs the squares to add up to a finite number.