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

09Interview questions and model answers

8 questions · click a question to see a brief answer
Q1What is the difference between batch, stochastic and mini-batch gradient descent?
Brief answer

All three run \theta\leftarrow\theta-\eta\,\hat g and differ in the gradient estimate \hat g: the average over all n examples (exact, O(nd) per step, one update per epoch), over one random example (unbiased, n times cheaper, very noisy), or over a random batch of b (variance \sigma^2/b, parallel on a GPU, n/b updates per epoch). Mini-batch is the default in deep learning, usually with b between 32 and 512; an epoch then gives \lceil n/b\rceil updates (3{,}907 for a million examples and b=256).

Q2Why do we move against the gradient?
Brief answer

To first order J(\theta+\Delta)-J(\theta)\approx\nabla J^{\top}\Delta, and by Cauchy–Schwarz this is most negative, for a fixed step length, when \Delta points opposite to \nabla J. It is the steepest local direction, not the direction to the minimum.

Q3How do you choose the learning rate, and what happens if it is too large or too small?
Brief answer

Each eigen-direction is multiplied by 1-\eta\lambda: too small is slow, and \eta>2/\lambda_{\max} diverges. Search on a log scale, watch the training loss, and use warmup and decay. The condition number \kappa sets how slow the flat directions are; normalizing inputs reduces it (here from 98.9 to 1.27).

Q4SGD uses a noisy gradient. Why does it still work?
Brief answer

Because the noisy gradient is unbiased: \E_i[\nabla\ell_i]=\nabla J, so the steps are right on average. With a constant learning rate it ends in a noise ball of size proportional to \eta; with a decaying rate satisfying \sum\eta_t=\infty and \sum\eta_t^2<\infty (Robbins–Monro) it converges. The noise also helps escape saddle points and flat regions.

Q5How does the batch size affect training?
Brief answer

The gradient variance is \sigma^2/b, so noise falls like 1/\sqrt b while the cost rises like b until the GPU is saturated. Larger batches allow larger learning rates (the linear scaling rule with warmup of Goyal et al. (2017) trained ResNet-50 on ImageNet in one hour with b=8{,}192), but give fewer updates per epoch, hit diminishing returns, and very large batches can generalize worse (Keskar et al., 2017).

Q6What does momentum do, and why does it help?
Brief answer

It steps along a running sum of past gradients, v\leftarrow\beta v+\nabla J, \theta\leftarrow\theta-\eta v. Consistent directions add up (steps up to 1/(1-\beta)=10 times longer for \beta=0.9), oscillating ones cancel. On a quadratic it improves the rate from (\kappa-1)/(\kappa+1) to (\sqrt\kappa-1)/(\sqrt\kappa+1). Nesterov’s version evaluates the gradient at the look-ahead point.

Q7Explain Adam. Why does it need bias correction?
Brief answer

Adam keeps a momentum average m of the gradients and an RMSProp average s of their squares, and steps by \eta\,\hat m/(\sqrt{\hat s}+\epsilon) per parameter (defaults 0.001, 0.9, 0.999, 10^{-8}). Both averages start at zero, so \E[m_t]=(1-\beta_1^t)\E[g]; dividing by 1-\beta^t removes this bias, which matters in the first hundreds of steps. Each step is about \eta in size per coordinate, independent of the gradient’s scale. AdaGrad (sum of all squared gradients, so steps shrink forever) and RMSProp (moving average) are its two ancestors.

Q8Adam or SGD? And what is AdamW?
Brief answer

AdamW is the default for transformers and is robust to the learning rate; SGD with momentum and a schedule remains strong for convolutional image models. AdamW decays the weights directly instead of adding an L2 term to the gradient, which Adam would rescale per parameter; with this fix Adam competes with SGD on image classification (Loshchilov and Hutter, 2019).