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
Machine Learning Interview #9 · Tutorial

Gradient DescentBatch, Stochastic, Mini-Batch and Adam

How many examples should each step look at, and how to make every step smarter

A tutorial companion to the video Machine Learning Interview #9: Gradient Descent: Batch, Stochastic, Mini-Batch and Adam PDF

01Introduction

1.1The question

“What is the difference between batch, stochastic and mini-batch gradient descent?” is one of the most common machine learning interview questions, and the follow-up is almost always “and what do momentum and Adam add?” Training a model means minimizing an average loss over the training data, and gradient descent does it by repeatedly stepping downhill. The three variants differ only in how they estimate the downhill direction: all the data gives the exact direction at a high price, one example a cheap but noisy guess, and a small random batch sits in between, at about the cost of one example on a GPU. Momentum and Adam then use the history of past gradients to make every step smarter.

1.2Prerequisites and notation

Prerequisites. Partial derivatives and the gradient, the dot product and eigenvalues of a symmetric matrix, and expectation and variance. Episode 7 (loss functions) explains the losses being minimized here.

Notation. The model has parameters \theta\in\mathbb{R}^d. The training set has n examples, and \ell_i(\theta) is the loss on example i. The training loss is the average

J(\theta)=\frac1n\sum_{i=1}^{n}\;\ell_i(\theta). \tag{1}

\nabla J(\theta) is its gradient, \eta>0 the learning rate (step size), t the step counter, b the batch size, \mathcal{B}_t the set of examples used at step t, and \theta^* a minimizer with loss J^*. H=\nabla^2J is the Hessian, with eigenvalues \lambda_{\min}\le\dots\le\lambda_{\max}; \kappa=\lambda_{\max}/\lambda_{\min} is the condition number. One epoch is one pass over the training data.

1.3A short history

Cauchy (1847) proposed the gradient method in a three-page note: to solve a system of equations, make a non-negative function decrease by moving each variable against its partial derivative; he wanted it for computing the orbits of celestial bodies. Robbins and Monro (1951) showed how to find the root of a function that can only be measured with noise, using step sizes that shrink at the right speed; applied to a gradient, their scheme is stochastic gradient descent (SGD). Polyak (1964) introduced the heavy-ball (momentum) method, and Nesterov (1983) an accelerated version with a provably faster rate. For neural networks, LeCun et al. (1998) explained why stochastic updates beat batch updates on large, redundant data sets, and Bottou (2010) argued that SGD is the method of choice for large-scale learning. Per-parameter step sizes came with AdaGrad (Duchi et al., 2011) and RMSProp (Tieleman and Hinton, 2012); Adam (Kingma and Ba, 2015) combined them with momentum, and AdamW (Loshchilov and Hutter, 2019) fixed how it handles weight decay. Goyal et al. (2017) showed that mini-batches of 8{,}192 images can train an ImageNet model in one hour.