Search

Search papers and lessons

Adam: A Method for Stochastic Optimization

Diederik P. Kingma, Jimmy Ba · 2014

Kingma and Ba's 2014 paper introduces Adam, an optimizer that adapts a step size for every parameter from running estimates of the gradient's first and second moments, with bias correction for the first steps.

On this page

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

TL;DR

Adam is a first-order optimizer for training models on noisy objectives. It keeps a running average of each parameter's gradient and of its squared gradient, corrects both averages for their start-up bias, and takes a step whose size adapts to each parameter separately. The paper shows it works well across several model types with little tuning, and gives a convergence guarantee for convex problems.

Where you'll see it: the default optimizer for training most neural networks, including transformers, image models and fine-tuning runs, usually in the variant AdamW that adds decoupled weight decay.

The problem

Training a model means minimizing a loss over its parameters. The loss is usually an average over a minibatch of data, so each gradient is a noisy estimate of the true gradient. Plain stochastic gradient descent uses one global learning rate. That rate has to suit every parameter at once, and it has to be tuned by hand and often decayed on a schedule.

Two other families existed in 2014. Methods such as AdaGrad adapt the rate per parameter, but accumulate squared gradients forever, so the effective rate shrinks toward zero on long runs. RMSProp uses a decaying average of squared gradients, which suits non-stationary settings, and methods with Momentum smooth the gradient direction without scaling steps per parameter. The paper aims to combine AdaGrad's strength on sparse gradients with RMSProp's strength online, works on non-stationary and sparse gradients, and needs little memory and little tuning.

Key idea

Keep two exponential moving averages per parameter, both updated from the current minibatch gradient:

  • the first moment, a smoothed gradient that gives the direction, and
  • the second moment, a smoothed squared gradient that estimates how large the gradient typically is.

Step in the direction of the first moment, divided elementwise by the square root of the second. A parameter whose gradients are consistently large takes smaller steps. A parameter that rarely receives a gradient takes larger ones when it does. Because the averages decay, the method forgets old gradients, unlike AdaGrad, which keeps adding them.

The averages start at zero, so early estimates are too small. The paper's bias correction divides each average by 1−βt1 - \beta^t, which removes the bias exactly in the case of stationary gradients.

The method

Let gt=∇θft(θt−1)g_t = \nabla_\theta f_t(\theta_{t-1}) be the gradient of the minibatch objective at step tt. The algorithm uses a step size α\alpha, decay rates β1,β2∈[0,1)\beta_1, \beta_2 \in [0, 1), and a small constant ϵ\epsilon. The paper's default values are α=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}.

Initialise m0=0m_0 = 0, v0=0v_0 = 0 and t=0t = 0. At each step:

mt=β1mt−1+(1−β1) gt,vt=β2vt−1+(1−β2) gt2,m_t = \beta_1 m_{t-1} + (1 - \beta_1)\, g_t, \qquad v_t = \beta_2 v_{t-1} + (1 - \beta_2)\, g_t^2,

where gt2g_t^2 is the elementwise square. Then correct the bias:

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}}.

Finally update the parameters:

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

Every operation is elementwise, so the cost per step is a few vector operations on memory the size of the parameters, two extra copies of the parameter vector in total. The paper highlights this as one reason it suits large models.

The bias-correction argument

Suppose the gradients are drawn from a fixed distribution with mean E[g]\mathbb{E}[g]. Unrolling the recursion shows that E[mt]=(1−β1 t) E[g]\mathbb{E}[m_t] = (1 - \beta_1^{\,t}) \, \mathbb{E}[g]. The average is pulled toward the zero it started from, by the factor 1−β1 t1 - \beta_1^{\,t}. Dividing by that factor gives an unbiased estimate of the mean gradient. The same argument applies to vtv_t with β2\beta_2. The correction matters for much longer there, because β2\beta_2 is close to 1 and the second moment fills up slowly.

The effective step

The ratio m^t/(v^t+ϵ)\hat m_t / (\sqrt{\hat v_t} + \epsilon) is roughly a signal-to-noise ratio. If the gradient has the same sign every step, m^t\hat m_t and v^t\sqrt{\hat v_t} are of similar size, and the step is close to α\alpha in magnitude. If the gradient flips sign and averages out, m^t\hat m_t is small compared with v^t\sqrt{\hat v_t}, and the step shrinks. The step size in any coordinate is therefore approximately bounded by α\alpha regardless of the gradient scale. (With the defaults, the exact worst case, a gradient that is zero at every step but the current one, allows a step up to about 3.16 α3.16\,\alpha.) The paper notes this makes the method invariant to diagonal rescaling of the gradients: multiplying one parameter's gradient by a constant leaves its step unchanged.

Convergence

For convex online problems the paper proves a regret bound. Regret compares the total loss of the algorithm with the loss of the best fixed parameter in hindsight. Under a step size that decays as αt=α/t\alpha_t = \alpha / \sqrt{t} and a first-moment coefficient β1,t\beta_{1,t} that decays exponentially, the regret after TT steps grows like O(T)O(\sqrt{T}) in the paper's framework, which matches the best known rate for this setting. The bound is stated under assumptions such as bounded gradients and a bounded feasible set. It is a guarantee about the average performance over a sequence, not a claim about deep networks, which are not convex.

AdaMax

The paper also derives a variant that replaces the second moment's square root with the infinity norm, ut=max⁡(β2ut−1,∣gt∣)u_t = \max(\beta_2 u_{t-1}, |g_t|). This removes the need for the ϵ\epsilon term and the second bias correction, and the step becomes αm^t/ut\alpha \hat m_t / u_t. The authors present it as a stable alternative with a simpler form.

Results

The paper's experiments all report training cost against passes over the data, with minibatches of 128.

  • Logistic regression on MNIST (convex, L2L_2-regularised, step size decayed as 1/t1/\sqrt{t}). Adam converges about as fast as SGD with Nesterov momentum, and both beat AdaGrad.
  • Logistic regression on IMDB reviews, using sparse 10,000-word bag-of-words features with 50% dropout noise. Here AdaGrad beats SGD with Nesterov momentum by a large margin, and Adam converges as fast as AdaGrad. This is the sparse-gradient case the method was designed for.
  • Multilayer networks on MNIST (two hidden layers of 1,000 ReLU units). Adam beats the other first-order methods with dropout. Against the quasi-Newton method SFO it makes faster progress per iteration and per second; SFO is 5 to 10 times slower per iteration and failed to converge with dropout.
  • A convolutional network on CIFAR-10. AdaGrad and Adam both fall quickly at first, but Adam and SGD with momentum then converge considerably faster than AdaGrad. The authors observe that the second-moment estimate shrinks to near zero after a few epochs and is dominated by ϵ\epsilon, so the gain here comes mostly from the first moment reducing minibatch variance. Adam shows only marginal improvement over SGD with momentum, though it sets per-layer scales without hand tuning.
  • Bias correction, on a variational autoencoder (one hidden layer of 500 units, 50-dimensional latent). Without correction, eta_2 close to 1 causes unstable training, especially in the first epochs. The best results used bias correction with 1−β21 - \beta_2 small, and Adam did equal or better than the uncorrected, RMSProp-with-momentum version at every hyperparameter setting tried.

The figures are curves of training cost, not final test accuracies, so the paper supports claims about optimisation speed rather than about generalisation.

Why it mattered

Adam became the default choice in deep learning within a few years of publication. Frameworks ship it as a one-line option, and most published training recipes for transformers and vision models use it or a close relative. The method also made per-parameter adaptive learning rates routine, which shaped how practitioners think about the learning rate as a hyperparameter.

Later work changed the details. Loshchilov and Hutter showed that adding weight decay to the gradient interacts badly with the adaptive scaling, and proposed AdamW, which decouples the decay. Researchers have also reported cases where Adam generalises worse than SGD, and a later paper gave a simple convex counterexample showing that the convergence argument fails for some choices of the second-moment averaging. The paper's core ideas, moment estimates with bias correction, remain in nearly every successor.

Reading guide

Read the paper in this order, with these prerequisites in hand.

  1. Abstract and introduction. Needs stochastic gradient descent and Gradient Descent. The problem is minimizing a noisy objective with a first-order method.
  2. The algorithm section. Needs Exponential Moving Average for the two moment updates, and Momentum for the first moment's role. The update rule is the one in The method section of this review.
  3. The bias-correction part. Needs Mean and Variance for the second moment as an average of squares, and the geometric series for the factor 1−βt1 - \beta^t.
  4. The convergence analysis. Needs Convex Function and the idea of regret from online learning. Skip on a first read if the rest is clear.
  5. The experiments. Needs the loss and model descriptions from the earlier sections, and Adam Optimizer for a gentler walkthrough of the update.
  6. The AdaMax variant. Needs the infinity norm and the main algorithm.

Check yourself

QuestionIn one or two sentences, what does Adam store for each parameter, and what does it do with those stored values?Show answer

Adam stores two running averages per parameter: a smoothed gradient and a smoothed squared gradient. It steps in the direction of the smoothed gradient, divided elementwise by the square root of the smoothed squared gradient, so each parameter gets its own effective step size.

QuestionWith β1=0.9\beta_1 = 0.9 and gradients that are constantly equal to 2, what is the bias-corrected first moment m^1\hat m_1 at step 1?Show answer

Start with m0=0m_0 = 0. Then m1=0.9⋅0+0.1⋅2=0.2m_1 = 0.9 \cdot 0 + 0.1 \cdot 2 = 0.2. The correction divides by 1−0.91=0.11 - 0.9^1 = 0.1, so m^1=0.2/0.1=2\hat m_1 = 0.2 / 0.1 = 2. Without correction the estimate would have been 0.2, a tenth of the true gradient.

QuestionWhy does Adam take smaller steps for a parameter whose gradients oscillate in sign than for one whose gradients always point the same way?Show answer

For an oscillating gradient, the first moment averages positive and negative values toward zero, so its magnitude is small compared with the square root of the second moment, which measures typical gradient size regardless of sign. The ratio, and therefore the step, is small. For a consistent gradient the first moment stays close to the typical size, so the ratio is close to 1 and the step is close to α\alpha.

QuestionA parameter's gradient was 100 for a long time and then drops to 0.01 for many steps. Compare how AdaGrad and Adam would respond, in terms of the effective step size.Show answer

AdaGrad accumulates every squared gradient, so the early large values stay in its denominator. Its effective step remains small for the rest of training. Adam forgets old squared gradients at rate β2\beta_2. With β2=0.999\beta_2 = 0.999 the old values decay slowly, but the old contribution falls below the new level of 0.0120.01^2 after roughly 18,000 steps (0.999n≈10−80.999^n \approx 10^{-8}), and then the step grows again. Adam's effective step recovers eventually, while AdaGrad's does not.

Sources

Read the full paper

Diederik P. Kingma, Jimmy Ba, 2014. arXiv:1412.6980