Gradient descent and optimizers, explained
The choice of optimizer and weight decay still decides whether a model trains well at all
What is gradient descent?
Gradient descent is a way to minimise an objective function. It moves a model's parameters in the opposite direction of the function's gradient, the direction of steepest increase. Sebastian Ruder's 2016 overview describes it as following the slope of a surface downhill until reaching a valley. A learning rate sets the size of each step.
Ruder sets out three variants. They differ only in how much data feeds each gradient calculation. Batch gradient descent uses the whole training set per update, which is accurate but slow and cannot learn online. Stochastic gradient descent (SGD) updates after every single example, which is faster but noisy. Mini-batch gradient descent, the version almost always used in practice, updates on small batches of between 50 and 256 examples, trading off the two.
Why does the learning rate matter so much?
The learning rate controls how far each update moves the parameters. Ruder's paper warns that a rate set too small makes convergence painfully slow. One set too large can cause the loss to fluctuate around the minimum or diverge outright. The same single rate also applies to every parameter by default, which is awkward when features occur at very different frequencies.
Fixed schedules that anneal the rate over time help but must be chosen in advance, Ruder notes. They cannot adapt to the data. This is the gap that adaptive methods such as Adagrad, RMSprop and Adam were built to close, by adapting the effective step size to each parameter's own gradient history.
What does momentum do?
Momentum, as described in Ruder's overview, adds a fraction of the previous update vector to the current one, typically weighted by a factor of around 0.9. Ruder's analogy is a ball rolling downhill: it speeds up in directions where gradients keep pointing the same way. It is damped in directions where they keep flipping sign. That fixes a specific failure mode of plain SGD, which oscillates badly in steep, narrow ravines around a local optimum rather than progressing along the valley floor.
A refinement called Nesterov accelerated gradient computes the gradient at an estimate of where the parameters are about to move, rather than where they currently sit. Ruder describes this as giving the rolling ball foresight. It lets the ball slow down before a slope turns upward again, which he reports improved RNN performance on several tasks in earlier work.
How does Adam work, and what is AdamW?
Adam, short for Adaptive Moment Estimation and introduced by Kingma and Ba, keeps a decaying average of past gradients, the first moment, like momentum. It also keeps a decaying average of past squared gradients, the second moment, as in RMSprop. It then corrects both for their bias toward zero early in training. Ruder's overview calls Adam's combination of momentum and adaptive per-parameter rates likely the best overall default, with proposed defaults of 0.9 for the first-moment decay, 0.999 for the second, and a small epsilon to avoid division by zero.
AdamW, from Loshchilov and Hutter's 2017 paper, changes how Adam applies weight decay, a penalty that shrinks weights toward zero to regularise the model. Standard implementations folded weight decay into the gradient as L2 regularisation. Loshchilov and Hutter prove this is mathematically equivalent to true weight decay for plain SGD but not for adaptive methods like Adam. Because Adam rescales gradients by their historic magnitude, folding decay into the gradient under-penalises weights that have had large gradients. AdamW instead applies the decay directly to the weights, separately from the gradient-based update step.
Does decoupling weight decay actually help?
Loshchilov and Hutter tested this on CIFAR-10 and ImageNet32x32, with a 26 2x64d and a 26 2x96d ResNet, training for between 100 and 1,800 epochs. Decoupled weight decay (AdamW) beat L2 regularisation in Adam under every learning-rate schedule they tried: fixed, step-drop and cosine annealing. Cosine annealing performed best overall.
At Adam's default learning rate of 0.001, the paper reports a 15% relative improvement in test error from AdamW over standard Adam on both datasets. Decoupling also made the learning rate and weight decay hyperparameters easier to tune independently, rather than needing to be retuned together. The authors say this closed most of the gap on CIFAR-10 between Adam and SGD with momentum, long considered the more reliable choice for image classification.
Key terms
- Gradient descent
- An algorithm that minimises an objective function by repeatedly moving a model's parameters in the opposite direction of the function's gradient.
- Learning rate
- The hyperparameter that sets how far each parameter update moves along the gradient's direction.
- Mini-batch gradient descent
- The common variant of gradient descent that computes each update from a small batch of training examples, typically 50 to 256, rather than one example or the whole dataset.
- Momentum
- A method that adds a fraction of the previous update to the current one, speeding convergence and damping oscillation in narrow valleys of the loss surface.
- Adam
- An optimizer that keeps decaying averages of past gradients and past squared gradients to set an adaptive, bias-corrected learning rate for each parameter.
- AdamW
- A variant of Adam that applies weight decay directly to the weights, separately from the gradient-based update, instead of folding it into the gradient as L2 regularisation.
- Weight decay
- A regularisation technique that shrinks a model's weights toward zero at each step to reduce overfitting.
- L2 regularisation
- A penalty added to the loss function proportional to the squared size of the weights, mathematically equivalent to weight decay for standard SGD but not for adaptive optimizers like Adam.
Written from
Written from the documents above by The Neural Net’s automated desk (claude-sonnet-5, theneuralnet/guides@1) without human review, and updated as news on this topic publishes. How this works.