Search

Search papers and lessons

Adam Optimizer

Level 3 · 16 min read

Adam keeps a running average of each parameter's gradient and of its squared gradient, then divides the first by the square root of the second, so every parameter gets its own adaptive step size.

On this page

  • Colour = topic mix
  • Calculus
  • Optimizers
  • Probability
  • Linear algebra
  • Losses & regularization
  • Discrete math
  • Sign in to see your progress

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 θt\theta_t be the parameter vector after step tt, starting from θ0\theta_0. Let α\alpha be the learning rate, β1\beta_1 and β2\beta_2 the decay rates of the two averages, and ϵ\epsilon a small constant that prevents division by zero. The gradient of the loss at step tt is

gt=∇θL(θt−1),g_t = \nabla_\theta L(\theta_{t-1}),

where ∇θL\nabla_\theta L 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 gtg_t and to gt2g_t^2 (element by element):

mt=β1mt−1+(1−β1) gtvt=β2vt−1+(1−β2) gt2\begin{aligned} m_t &= \beta_1 m_{t-1} + (1 - \beta_1)\, g_t \\ v_t &= \beta_2 v_{t-1} + (1 - \beta_2)\, g_t^2 \end{aligned}

Both start at zero: m0=0m_0 = 0 and v0=0v_0 = 0. The authors recommend α=0.001\alpha = 0.001, β1=0.9\beta_1 = 0.9, β2=0.999\beta_2 = 0.999 and ϵ=10−8\epsilon = 10^{-8}.

Bias correction

Because m0m_0 and v0v_0 are zero, both averages are biased towards zero in the first few steps. The expected value of mtm_t is (1−β1t)(1-\beta_1^t) times the true average gradient, not the average itself. Adam divides out that factor:

m^t=mt1−β1 t,v^t=vt1−β2 t\hat m_t = \frac{m_t}{1 - \beta_1^{\,t}}, \qquad \hat v_t = \frac{v_t}{1 - \beta_2^{\,t}}

The factor 1−β1 t1 - \beta_1^{\,t} is what the first memory has "filled up" by step tt. The plot below shows how far each average is from full at the start. With β2=0.999\beta_2 = 0.999, 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.

0102030405000.20.40.60.81step tfill factor
  • 1 − β₁ᵗ
  • 1 − β₂ᵗ
How full each moving average is at step t. The β₂ average takes about a thousand steps to settle, so its correction matters for a long time.

The update

The parameters move by

θt=θt−1−α m^tv^t+ϵ.\theta_t = \theta_{t-1} - \alpha \, \frac{\hat m_t}{\sqrt{\hat v_t} + \epsilon}.

The square root and the division are element by element. Each parameter gets its own effective step size, α/(v^t+ϵ)\alpha / (\sqrt{\hat v_t} + \epsilon), 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 gg, then m^t=g\hat m_t = g and v^t=g2\hat v_t = g^2, so the step is α⋅g/∣g∣=α\alpha \cdot g / |g| = \alpha in size. Adam moves each parameter by about the learning rate per step, whatever the gradient's scale.

Worked examples

Example
One parameter, two steps by hand

Minimise a loss whose gradient we are given as a sequence. Use α=0.1\alpha = 0.1, β1=0.9\beta_1 = 0.9, β2=0.999\beta_2 = 0.999, start at θ0=1\theta_0 = 1, and ignore ϵ\epsilon.

Step 1. The gradient is g1=0.5g_1 = 0.5.

  1. m1=0.9⋅0+0.1⋅0.5=0.05m_1 = 0.9 \cdot 0 + 0.1 \cdot 0.5 = 0.05
  2. v1=0.999⋅0+0.001⋅0.25=0.00025v_1 = 0.999 \cdot 0 + 0.001 \cdot 0.25 = 0.00025
  3. m^1=0.05/(1−0.9)=0.5\hat m_1 = 0.05 / (1 - 0.9) = 0.5
  4. v^1=0.00025/(1−0.999)=0.25\hat v_1 = 0.00025 / (1 - 0.999) = 0.25
  5. Step =0.1⋅0.5/0.25=0.1= 0.1 \cdot 0.5 / \sqrt{0.25} = 0.1, so θ1=1−0.1=0.9\theta_1 = 1 - 0.1 = 0.9

The parameter moved by exactly α\alpha. This is the bias-correction effect: without it, m^1\hat m_1 would be 0.050.05 and the step would be 0.010.01.

Step 2. The gradient changes to g2=0.1g_2 = 0.1, a smaller push.

  1. m2=0.9⋅0.05+0.1⋅0.1=0.055m_2 = 0.9 \cdot 0.05 + 0.1 \cdot 0.1 = 0.055
  2. v2=0.999⋅0.00025+0.001⋅0.01=0.00025975v_2 = 0.999 \cdot 0.00025 + 0.001 \cdot 0.01 = 0.00025975
  3. m^2=0.055/(1−0.81)=0.2895\hat m_2 = 0.055 / (1 - 0.81) = 0.2895
  4. v^2=0.00025975/(1−0.998001)=0.12994\hat v_2 = 0.00025975 / (1 - 0.998001) = 0.12994
  5. Step =0.1⋅0.2895/0.12994=0.1⋅0.2895/0.3605=0.0803= 0.1 \cdot 0.2895 / \sqrt{0.12994} = 0.1 \cdot 0.2895 / 0.3605 = 0.0803, so θ2≈0.9−0.0803=0.8197\theta_2 \approx 0.9 - 0.0803 = 0.8197

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.

Example
A quadratic with a realistic learning rate

Minimise f(x)=x2f(x) = x^2, whose gradient is 2x2x. Start at x0=5x_0 = 5 and use the same hyperparameters with α=0.1\alpha = 0.1. 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.20349

Interpretation: the step is close to α⋅sign⁡(x)\alpha \cdot \operatorname{sign}(x) at the start, about 0.1 per step, so xx 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: xx never overshoots the minimum at 0 in these 30 steps. Changing α\alpha 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.

SituationWhat goes wrongBetter choice
Large, well-tuned vision models trained for many epochsAdam can converge to solutions that generalise slightly worse than tuned SGD with momentumSGD with momentum and a schedule, or AdamW with tuned weight decay
Weight decay added as an L2 term to the lossThe adaptive scaling also shrinks the decay term, so regularisation is weaker than intendedAdamW, which applies weight decay directly to the parameters
Very sparse gradients, such as large embedding tablesRare parameters get large steps when their gradient finally arrives, which can be unstableA warmup period, gradient clipping, or an optimizer built for sparse updates
Training is unstable early onThe second average is still tiny, so steps can be large before the memories settleLearning-rate warmup, which ramps α\alpha up over the first few thousand steps
Gradients occasionally spikeOne huge gradient inflates vtv_t and then freezes the parameter for a long timeGradient clipping, see Gradient Norm Clipping
Memory is tightAdam 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 SGDFactored 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 α\alpha 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, mtm_t, which gives the direction and momentum, and a moving average of the squared gradient, vtv_t, which gives the typical scale. The update divides the first by the square root of the second.

QuestionComputation: with β1=0.9\beta_1 = 0.9, what is the bias-correction factor 1−β1 31 - \beta_1^{\,3} at step t=3t = 3, and what does it do to m3m_3?Show answer

1−0.93=1−0.729=0.2711 - 0.9^3 = 1 - 0.729 = 0.271. Dividing m3m_3 by 0.2710.271 scales it up by a factor of about 3.7. Early in training, mtm_t 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 m^t/(v^t+ϵ)\hat m_t / (\sqrt{\hat v_t} + \epsilon). Multiplying every gradient by a constant cc multiplies m^t\hat m_t by cc and v^t\sqrt{\hat v_t} by ∣c∣|c|, 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 ϵ\epsilon 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 α\alpha sets the overall speed for every parameter. Clipping gradients is a third option when a few steps produce huge gradients that inflate vtv_t.

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 v^t\hat v_t, 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