The idea
Key formulas
Demo loss and gradient
Loading lessons…
Just a moment, getting everything ready. The first load can take up to a minute.
Machine learning
Compare three update rules that use gradients to move model parameters toward lower loss.
An optimizer adjusts a model's parameters to reduce a loss: a numerical score for how poorly the model fits its training data. If the parameters are collected into a vector , the loss is a function . The gradient points in the direction of steepest local increase, so its negative points in the direction of steepest local decrease. A basic gradient-descent step is
where counts steps and is the learning rate. It sets the step size. The update is motivated by a local approximation: for a small displacement , the loss changes approximately by . Choosing makes this first-order change , which is nonpositive. This is a local argument, not a promise that any step size decreases the true loss; for a curved loss, large steps can overshoot.
For a dataset with examples, the usual training objective averages the individual example losses. Computing the exact gradient of this average at every step is called batch gradient descent. Stochastic gradient descent (SGD) instead samples one example (or a small mini-batch) and uses its gradient as an estimate of the full gradient. This reduces work per update, especially on large datasets, but introduces noise: an individual example may suggest a direction that differs from the average direction. Mini-batches trade off the cost of each update with the variability of the estimate.
SGD takes a step using the current gradient estimate:
If the gradient is computed from the whole dataset, ; if it comes from a sampled example or mini-batch, it is an estimate. The same update rule can therefore be used with either source of gradient, although “SGD” commonly means the stochastic or mini-batch version in model training.
Momentum adds a running velocity that remembers recent gradients. One common form is
where controls how much history is retained. If successive gradients point in a similar direction, the velocity builds and helps maintain progress. If their directions change rapidly, averaging can smooth oscillation. This history can be useful on a long, narrow valley, where steps tend to zigzag across the steep direction while slowly advancing along the shallow direction. Momentum does not remove the need to choose a suitable learning rate, and it is not guaranteed to help every objective.
Adam keeps two exponential moving averages: one of the gradients and one of their squares. The first behaves like a momentum estimate; the second estimates the scale of recent gradients in each parameter coordinate. Adam corrects the initial moving averages for their bias toward zero and uses the squared-gradient estimate to rescale its step. In simplified notation,
followed by bias corrections and , and an update of the form
Here the square and square root are coordinate-wise, and is a small constant for numerical stability. Adam adapts the effective step by coordinate while retaining a momentum-like average. That convenience is not a proof of convergence or a reason to ignore tuning; its behavior depends on the data, the loss, and the chosen hyperparameters.