Advanced Mathematics

Stochastic Gradient Descent and Adam

Gradient descent using the gradient of a small random batch instead of all the data, plus momentum and per-parameter step sizes (Adam).

Needs a step first: 18

When the loss is an average over millions of examples, computing the full gradient for each step is too slow. Stochastic gradient descent uses the gradient on a random minibatch: it is noisy but right on average (unbiased), and many cheap noisy steps beat a few exact ones. Momentum averages recent gradients to smooth the zig-zag, and Adam also divides each parameter's step by a running estimate of its gradient's size, so steep and flat directions move at similar speeds. Nearly every deep network is trained this way.

Where it shows up: It is the optimiser in training deep networks, convolutional networks, transformers and diffusion models.

How to hold it: Give it to the machine, then check

Let the library run SGD or Adam; know that a minibatch gradient is noisy but right on average, choose the batch size and learning rate, and check the training and validation loss curves.

What knowing it looks like

  • Show that the average of minibatch gradients over all batches equals the full gradient.
  • Carry out one Adam step by hand, including the bias correction.
  • Explain how the batch size trades the noise of each step against its cost.

Test yourself

The loss is L(w)=1N∑i12(w−xi)2L(w) = \frac1N\sum_i \tfrac12(w - x_i)^2 with data {1,3,5,7}\{1, 3, 5, 7\}, starting at w=0w = 0. (a) With learning rate 0.50.5, where does one step take ww using the full data, the batch {1,3}\{1, 3\}, and the batch {5,7}\{5, 7\}? (b) One Adam step on the batch {1,3}\{1, 3\} with η=0.01\eta = 0.01, β1=0.9\beta_1 = 0.9, β2=0.999\beta_2 = 0.999 and ϵ\epsilon negligible: what is the new ww?
Show the worked answer
The gradient is the mean of w−xiw - x_i. (a) Full: −4-4, so w=0+0.5⋅4=2w = 0 + 0.5\cdot4 = 2. Batch {1,3}\{1, 3\}: −2-2, so w=1w = 1. Batch {5,7}\{5, 7\}: −6-6, so w=3w = 3. The two batch gradients average to −4-4, the full gradient. (b) g=−2g = -2. m=0.1g=−0.2m = 0.1g = -0.2, v=0.001g2=0.004v = 0.001g^2 = 0.004. Bias-corrected: m^=−0.2/0.1=−2\hat m = -0.2/0.1 = -2, v^=0.004/0.001=4\hat v = 0.004/0.001 = 4. Step =η m^/v^=0.01⋅(−2)/2=−0.01= \eta\,\hat m/\sqrt{\hat v} = 0.01\cdot(-2)/2 = -0.01, so w=0−(−0.01)=0.01w = 0 - (-0.01) = 0.01. Adam's first step has size η\eta whatever the gradient's scale.

Needs a step first: 18

Learn it with an AI

Copy this prompt into any AI assistant. It will take you through step by step at your level and finish with the test above.

Be my maths tutor. I'm learning "Stochastic Gradient Descent and Adam" (Frontier tier): Gradient descent using the gradient of a small random batch instead of all the data, plus momentum and per-parameter step sizes (Adam).
I already know: Gradient Descent, Random Variables, Expectation and Variance.
Teach it Socratically: first ask what I already know; then explain the core idea with one concrete example and why it's true; then give me three problems, one at a time, easy to hard. Wait for my answer, check every step, and point out mistakes without giving the answer away.
Finish by testing me with this: The loss is $L(w) = \frac1N\sum_i \tfrac12(w - x_i)^2$ with data $\{1, 3, 5, 7\}$, starting at $w = 0$. (a) With learning rate $0.5$, where does one step take $w$ using the full data, the batch $\{1, 3\}$, and the batch $\{5, 7\}$? (b) One Adam step on the batch $\{1, 3\}$ with $\eta = 0.01$, $\beta_1 = 0.9$, $\beta_2 = 0.999$ and $\epsilon$ negligible: what is the new $w$?
Use LaTeX for formulas. When you say where it's used later (machine learning, quantum mechanics), be specific.

On the way to

Recommended