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 with data , starting at . (a) With learning rate , where does one step take using the full data, the batch , and the batch ? (b) One Adam step on the batch with , , and negligible: what is the new ?
Show the worked answer
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.Needs first
- Gradient DescentRepeatedly stepping against the gradient, , to walk downhill towards a minimum.Undergraduate I · Optimisation & Information
- Random Variables, Expectation and VarianceA number decided by chance, summed up by its expectation (the long-run average) and its variance (how far it typically strays).Core · Probability & Statistics
Leads to
On the way to
Recommended
- Mathematics for Machine LearningDeisenroth, Faisal and Ong (Cambridge University Press) · BookA free book that gathers exactly the linear algebra, vector calculus, probability and optimisation machine learning uses, then applies them to regression, PCA and classification.
- Convex OptimizationStephen Boyd and Lieven Vandenberghe · BookThe standard text on convex sets, duality and Lagrange multipliers, free as a PDF from Stanford along with lecture slides and exercises.
- Information Theory, Inference, and Learning AlgorithmsDavid MacKay · BookMacKay's book connecting entropy, coding, Bayesian inference and neural networks, free to read online and still the one recommended for information theory with intuition.