Adam Optimizer Explained Step by Step (Free Calculator)

Watch the Adam optimizer work step by step: this free calculator shows every moment estimate, bias correction and update, and compares Adam with plain gradient descent.

mt = β1mt−1 + (1−β1)gt  ·  vt = β2vt−1 + (1−β2)gt²  ·  θ ← θ − α·m̂t/(√v̂t + ε)
Final θ (Adam)—
Final θ (plain SGD)—
Final loss (Adam)—
First Adam step—
Every Adam step: gradient, moments, bias correction and update
tgtmtvtm̂tv̂tstepθt
Example loaded. Press Run Adam.

What is the Adam optimizer?

The Adam optimizer (adaptive moment estimation) is the most widely used algorithm for training neural networks. Like gradient descent, it moves each weight against its gradient, but it adds two running averages: one of the gradients (momentum) and one of the squared gradients (a per-weight scale). Dividing the first by the square root of the second gives every weight its own adaptive step size.

The calculator above runs Adam step by step on the simple loss \( f(\theta) = a(\theta – c)^2 \), or on your own list of gradients, and shows every intermediate value: \( m_t \), \( v_t \), the bias-corrected \( \hat m_t \) and \( \hat v_t \), the step and the new \( \theta \). It also runs plain SGD with the same learning rate for comparison.

adam optimizer path compared with plain gradient descent on a quadratic loss

The Adam optimizer update rule

At step t, with gradient \( g_t \):

\[ 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 \]

\[ \hat m_t = \frac{m_t}{1-\beta_1^{\,t}} \qquad \hat v_t = \frac{v_t}{1-\beta_2^{\,t}} \qquad \theta_t = \theta_{t-1} – \alpha\,\frac{\hat m_t}{\sqrt{\hat v_t} + \epsilon} \]

  • \( m_t \), the first moment, is an exponential moving average of the gradient. It smooths out noise and builds speed in a consistent direction, like momentum.
  • \( v_t \), the second moment, averages the squared gradient. Weights with large or noisy gradients get a larger \( v_t \) and therefore smaller steps.
  • Bias correction. Both averages start at 0, so early on they are too small. Dividing by \( 1 – \beta^t \) fixes that; the correction fades as t grows.
  • Defaults from the paper: \( \alpha = 0.001 \), \( \beta_1 = 0.9 \), \( \beta_2 = 0.999 \), \( \epsilon = 10^{-8} \).

Worked example: the first Adam optimizer step

Take \( f(\theta) = (\theta – 3)^2 \), start at \( \theta_0 = 0 \), with \( \alpha = 0.1 \). The gradient is \( g_1 = 2(0 – 3) = -6 \).

  1. \( m_1 = 0.9 \cdot 0 + 0.1 \cdot (-6) = -0.6 \)
  2. \( v_1 = 0.999 \cdot 0 + 0.001 \cdot 36 = 0.036 \)
  3. \( \hat m_1 = -0.6 / (1 – 0.9) = -6 \) and \( \hat v_1 = 0.036 / (1 – 0.999) = 36 \)
  4. Step \( = -0.1 \cdot (-6) / (\sqrt{36} + 10^{-8}) \approx 0.1 \), so \( \theta_1 \approx 0.1 \)

Notice the first step equals the learning rate, whatever the size of the gradient. Change a to 100 in the calculator (a gradient 100 times larger) and the first step is still about 0.1. That scale-invariance is why Adam is easy to tune. Plain SGD with the same rate takes a step of \( 0.1 \times 6 = 0.6 \) here, and with a = 100 it overshoots and diverges.

Adam optimizer vs SGD, RMSProp and AdamW

OptimizerMomentumPer-weight step sizeNotes
SGDNoNoSimple; needs a well-chosen learning rate
SGD with momentumYesNoOften generalises well in vision models
RMSPropNoYesAdam without the first moment
AdamYesYesRobust default; fast progress early in training
AdamWYesYesAdam with decoupled weight decay; the usual choice for transformers

AdamW matters because adding L2 regularisation to the loss interacts badly with Adam’s per-weight scaling. AdamW instead shrinks the weights directly in the update, which tends to generalise better. It is the standard optimizer for training transformers.

Tips for using the Adam optimizer

  • Start at 0.001 (3e-4 is another common choice) and tune the learning rate first; the betas rarely need changing.
  • Use a schedule. Warm-up followed by decay usually beats a constant rate for large models.
  • Watch for poor generalisation. Adam can fit training data fast and still generalise slightly worse than SGD with momentum on some vision tasks. Check against overfitting.
  • Adaptive steps do not fix bad scaling. Feature scaling still helps, and very deep networks still need care with vanishing gradients.

Using the Adam optimizer in PyTorch

import torch
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3, betas=(0.9, 0.999), eps=1e-8)
# or, with decoupled weight decay:
optimizer = torch.optim.AdamW(model.parameters(), lr=1e-3, weight_decay=0.01)

for x, y in loader:
    optimizer.zero_grad()
    loss = loss_fn(model(x), y)
    loss.backward()
    optimizer.step()

Each call to optimizer.step() applies exactly the update shown in the calculator, once for every weight in the model. The Adam optimizer keeps two extra numbers per weight (\( m \) and \( v \)), so it needs about three times the memory of the weights alone.

The method was introduced in Adam: A Method for Stochastic Optimization (Kingma and Ba).

Frequently asked questions

What does Adam stand for? Adaptive moment estimation: it adapts the step size from estimates of the first and second moments of the gradient.

What are the default Adam parameters? Learning rate 0.001, \( \beta_1 = 0.9 \), \( \beta_2 = 0.999 \) and \( \epsilon = 10^{-8} \).

Why does Adam need bias correction? Because \( m \) and \( v \) start at zero, their early averages are biased towards zero. Dividing by \( 1 – \beta^t \) removes that bias in the first steps.

Is Adam always better than SGD? No. Adam converges quickly and needs little tuning, but well-tuned SGD with momentum can match or beat it on some tasks.

Scroll to Top