TL;DR
Adam is a training rule for neural networks. It keeps two running averages for every parameter: one of the gradient (the direction the parameter wants to move) and one of the squared gradient (how large those pushes have been). It takes a step in the averaged direction, scaled down where the pushes have been large and up where they have been small. That gives each parameter its own step size, and it usually trains with far less hand tuning than plain gradient descent.
Where you'll see it: as the default optimizer when training transformers and other large networks. Attention Is All You Need and the Adam paper, Kingma and Ba (2014), are the usual references.
Intuition
Plain SGD uses one global learning rate. That is a problem when parameters live on different scales. Picture a long, narrow valley. Steep walls push one parameter back and forth violently, while the floor of the valley has a parameter that barely moves. A learning rate small enough to stop the wall oscillations makes the floor crawl. A learning rate large enough for the floor makes the walls explode.
Adam handles this with two memories per parameter:
- A momentum memory: the average of recent gradients. If the gradient keeps pointing the same way, the average stays large and the parameter keeps moving. If it flips back and forth, the average cancels out and the parameter stops zigzagging. This is the idea behind Momentum.
- A scale memory: the average of recent squared gradients. A parameter whose gradients are consistently large gets a large denominator and takes smaller steps. A parameter whose gradients are tiny gets a small denominator and takes relatively larger steps.
flowchart LR G[gradient g_t] --> M[average of g: direction m_t] G --> V[average of g squared: scale v_t] M --> S[step = m_t divided by square root of v_t] V --> S S --> U[parameters move against the step]
The direction comes from the first memory and the size comes from the second. Because the step is a ratio, multiplying all gradients by 10 changes almost nothing. That is a useful property when you cannot predict how big gradients will be in a new model.
The math
Let be the parameter vector after step , starting from . Let be the learning rate, and the decay rates of the two averages, and a small constant that prevents division by zero. The gradient of the loss at step is
where is the Gradient of the loss with respect to the parameters. The two memories are exponential moving averages, the same recursion as in Exponential Moving Average, applied to and to (element by element):
Both start at zero: and . The authors recommend , , and .
Bias correction
Because and are zero, both averages are biased towards zero in the first few steps. The expected value of is times the true average gradient, not the average itself. Adam divides out that factor:
The factor is what the first memory has "filled up" by step . The plot below shows how far each average is from full at the start. With , the second average is only about 5% filled after 50 steps, so without correction its estimate would be far too small and the first steps would be far too large.
- 1 − β₁ᵗ
- 1 − β₂ᵗ
The update
The parameters move by
The square root and the division are element by element. Each parameter gets its own effective step size, , which shrinks where gradients have been large and grows where they have been small.
A useful consequence: if the gradient for one parameter is a constant , then and , so the step is in size. Adam moves each parameter by about the learning rate per step, whatever the gradient's scale.
Worked examples
Minimise a loss whose gradient we are given as a sequence. Use , , , start at , and ignore .
Step 1. The gradient is .
- Step , so
The parameter moved by exactly . This is the bias-correction effect: without it, would be and the step would be .
Step 2. The gradient changes to , a smaller push.
- Step , so
Interpretation: the second gradient is five times smaller than the first, but the step is only about 0.8 times the first one. The momentum memory still remembers the big first push, so the step leans towards it, and the scale memory is still dominated by the earlier large squared gradient. Adam is not reacting to the gradient alone; it is reacting to its history.
Minimise , whose gradient is . Start at and use the same hyperparameters with . This Python snippet runs the update and prints the position at a few steps:
import math
alpha, b1, b2, eps = 0.1, 0.9, 0.999, 1e-8
x, m, v = 5.0, 0.0, 0.0
for t in range(1, 31):
g = 2 * x # gradient of x^2
m = b1 * m + (1 - b1) * g
v = b2 * v + (1 - b2) * g * g
m_hat = m / (1 - b1 ** t)
v_hat = v / (1 - b2 ** t)
x -= alpha * m_hat / (math.sqrt(v_hat) + eps)
if t in (1, 2, 5, 10, 20, 30):
print(f"t={t:2d} x={x:.5f}")Output:
t= 1 x=4.90000
t= 2 x=4.80006
t= 5 x=4.50098
t=10 x=4.00760
t=20 x=3.06034
t=30 x=2.20349Interpretation: the step is close to at the start, about 0.1 per step, so falls by roughly 0.1 a step while the gradient is steady. It is slow in absolute terms because the learning rate is small for this toy problem, but the step size is controlled: never overshoots the minimum at 0 in these 30 steps. Changing is the knob that sets the speed; Adam does not remove the need to choose it.
Tradeoffs and failure modes
Adam's defaults work across a wide range of models, which is why it is the common first choice. It also has real costs.
| Situation | What goes wrong | Better choice |
|---|---|---|
| Large, well-tuned vision models trained for many epochs | Adam can converge to solutions that generalise slightly worse than tuned SGD with momentum | SGD with momentum and a schedule, or AdamW with tuned weight decay |
| Weight decay added as an L2 term to the loss | The adaptive scaling also shrinks the decay term, so regularisation is weaker than intended | AdamW, which applies weight decay directly to the parameters |
| Very sparse gradients, such as large embedding tables | Rare parameters get large steps when their gradient finally arrives, which can be unstable | A warmup period, gradient clipping, or an optimizer built for sparse updates |
| Training is unstable early on | The second average is still tiny, so steps can be large before the memories settle | Learning-rate warmup, which ramps up over the first few thousand steps |
| Gradients occasionally spike | One huge gradient inflates and then freezes the parameter for a long time | Gradient clipping, see Gradient Norm Clipping |
| Memory is tight | Adam stores two extra numbers per parameter, so parameters plus optimizer state take three times the memory of the parameters alone, versus one times for plain SGD | Factored or 8-bit optimizer states, if the library supports them |
The main thing to keep in mind is that the learning rate still matters. Adam makes the step size per parameter roughly uniform and stable, but sets the overall speed, and a bad value fails in the same way it would for SGD.
History
Herbert Robbins and Sutton Monro introduced stochastic approximation in 1951, the idea behind SGD. Boris Polyak's momentum method came in 1964. The problem Adam addresses, that one learning rate fits poorly across parameters, led to a line of per-parameter methods in the 2010s. AdaGrad (Duchi, Hazan and Singer, 2011) accumulated all squared gradients, so its steps shrank steadily. RMSProp, proposed by Geoffrey Hinton in lecture slides around 2012, replaced that sum with a decaying average. Adam, by Diederik Kingma and Jimmy Ba, combined RMSProp's scale memory with a momentum memory and added bias correction. It was published in 2014 and presented at ICLR 2015.
It caught on because it worked with little tuning on many different networks, and because its defaults were a sensible starting point. Its paper became one of the most cited in machine learning. Later work, including AdamW, showed how to fix its interaction with weight decay, and most large-model training recipes still start from Adam or one of its variants.
Check yourself
QuestionRecall: what are the two running averages that Adam keeps for each parameter?Show answer
A moving average of the gradient, , which gives the direction and momentum, and a moving average of the squared gradient, , which gives the typical scale. The update divides the first by the square root of the second.
QuestionComputation: with , what is the bias-correction factor at step , and what does it do to ?Show answer
. Dividing by scales it up by a factor of about 3.7. Early in training, has only seen a few gradients, so it is small. The correction restores its scale so that the first steps are not artificially tiny.
QuestionWhy: why is Adam's step roughly the same size whether the gradients are around 0.001 or around 1000?Show answer
The step is . Multiplying every gradient by a constant multiplies by and by , so the ratio is unchanged. The learning rate sets the step size, not the gradient scale. The only exception is when gradients are so small that dominates the denominator.
QuestionApplied: you switch a model from SGD with momentum to Adam and training diverges in the first hundred steps. Name two changes you would try first, and why each might help.Show answer
Add a learning-rate warmup, so the step size starts small while the second average is still filling in and its bias correction is least reliable. Or lower the peak learning rate, since sets the overall speed for every parameter. Clipping gradients is a third option when a few steps produce huge gradients that inflate .
QuestionApplied: you use Adam with an L2 penalty added to the loss to regularise a network, and the weights are larger than expected. Explain the likely cause.Show answer
In Adam, the L2 gradient is added to the loss gradient before the moving averages. Parameters with large gradients get a large , which shrinks their step, so the penalty's effect is diluted for exactly the parameters it should constrain. Decoupled weight decay, as in AdamW, applies the shrinkage directly to the parameters and avoids this interaction.
Sources
- Kingma and Ba, Adam: A Method for Stochastic Optimization (2014)
- Ruder, An overview of gradient descent optimization algorithms (2016)
- Goodfellow, Bengio, Courville, Deep Learning (MIT Press), ch. 8
- Stochastic gradient descent (Wikipedia)