Skip to content
AI360XpertBeta
Core ML

Visual explainer

Adam Optimizer, Visually

Trace Adam's two moving averages, bias correction and coordinatewise steps, then see why retained momentum can raise the current minibatch loss.

One SGD learning rate preserves the gradient scale difference.
One SGD learning rate preserves the gradient scale difference.

The dashed projection separates the long horizontal movement from the short vertical movement in this cropped parameter-space view. The faint slanted lines are equal-loss contours for the first minibatch. Plain stochastic gradient descent (SGD) multiplies every gradient coordinate by the same learning rate, so the tenfold gradient difference remains a tenfold displacement difference.

Our synthetic example starts at weights (1, 1), with gradient (2, 0.2). At learning rate 0.1, SGD moves them by (−0.2, −0.02), ending at (0.8, 0.98). Adam uses each coordinate's history to rescale its update. Throughout this page, displacement means new weight minus old weight.

Keep Two Histories

Adam tracks signed gradients and squared gradients separately.
Adam tracks signed gradients and squared gradients separately.

The two branches keep different information. This drawing zooms into the x coordinate: the stored direction arrow is half the input arrow's length. On the right, the dashed square represents the raw squared gradient; the mint square has half its area and represents the stored squared-gradient average. Area, rather than side length, encodes the squared quantity.

Adam stores a signed-gradient average, m, and a squared-gradient average, v, per parameter. At update t, gₜ is the current gradient; the decay factors β₁ and β₂ set how much old history is kept. Squaring retains magnitude even when a gradient is negative:

mt=β1mt−1+(1−β1)gtm_t = \beta_1 m_{t-1} + (1-\beta_1)g_t vt=β2vt−1+(1−β2)gt2v_t = \beta_2 v_{t-1} + (1-\beta_2)g_t^2

The square applies separately to each coordinate. Start both averages at zero and choose decay factors β₁ = β₂ = 0.5 for readable arithmetic. After the first gradient, m = (1, 0.1) and v = (2, 0.02). These are teaching settings, not recommendations. The second moment isn't centered variance. The two histories cost two state values per weight.

Correct the Zero Start

Bias correction compensates for moments initialized at zero.
Bias correction compensates for moments initialized at zero.

Starting a moving average at zero pulls its early values toward zero. The raw/corrected comparison continues the x-coordinate detail: the direction arrow doubles in length, while the square doubles in area. Adam divides each average by its own correction factor:

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

At step one, both divisors are 0.5: corrected direction becomes (2, 0.2), and corrected second moment becomes (4, 0.04). This compensates for initialization, not sampling noise, and doesn't replace the running history with the current gradient at every later step.

Divide Each Coordinate

Adam divides each corrected direction by its own scale plus epsilon.
Adam divides each corrected direction by its own scale plus epsilon.

Read each row from its direction arrow, through the middle scale ruler, to the resulting step. The previous square encodes area, so its side length represents the square root used here. A larger remembered magnitude provides a larger divisor. Here, the large and small direction components become almost equal displacements after each is divided by its own scale. Compare x with y within each column; the output uses parameter units. Following Adam's update rule:

Δwi=−αm^iv^i+ε.\Delta w_i = -\alpha\frac{\hat m_i}{\sqrt{\hat v_i}+\varepsilon}.

The global learning rate is α = 0.1. Choose ε = 0.01 outside the square root so its effect is visible. The x calculation is −0.1 × 2 / (2 + 0.01); the y calculation is −0.1 × 0.2 / (0.2 + 0.01). The first displacements are approximately (−0.09950, −0.09524), not exactly −0.1. The resulting weights, w₁, are approximately (0.90050, 0.90476). This isn't a universal rule that flatter coordinates travel farther.

When the Gradient Reverses

A reversed gradient can leave the smoothed direction positive.
A reversed gradient can leave the smoothed direction positive.

In this magnified x-coordinate view, the old and new arrows already include their half weights. Dashed guides align the endpoints; the memory arrow is their sum. The new gradient points the other way, but the retained contribution from the previous step is stronger.

Our next gradient is (−0.5, −0.05). Blending half the old signed average with half this gradient gives m = (0.25, 0.025), still positive in both coordinates.

The squared-gradient average becomes v = (1.125, 0.01125). At step two, both correction factors are 0.75. After correction and scaling, both weights decrease again, by approximately (−0.02700, −0.02516), reaching w₂ ≈ (0.87350, 0.87960). History can outweigh the latest gradient's sign; smoothing doesn't guarantee an instant change of direction.

The Rate Still Matters

Doubling the learning rate doubles a step from the same moment state.
Doubling the learning rate doubles a step from the same moment state.

The ring and diamond mark alternative endpoints from the same starting state, using learning rates 0.1 and 0.2. They use identical corrected histories. Doubling the rate doubles the displacement vector: approximately (−0.09950, −0.09524) becomes (−0.19900, −0.19048).

This comparison holds the histories fixed. In general training objectives, future gradients can change with the resulting weights, so whole trajectories needn't double. Choose settings using validation evidence; adaptation doesn't choose the global rate for you.

A Batch Loss Can Rise

Retained momentum can increase the current minibatch loss.
Retained momentum can increase the current minibatch loss.

The picture plots the current minibatch's loss along the straight parameter-space segment from w₁ to w₂. The horizontal axis follows progress along this one update; height shows loss. The uphill movement shows the cost of retaining the old direction.

Our illustrative minibatches have linear losses: first 2wₓ + 0.2wᵧ, then −0.5wₓ − 0.05wᵧ. On the second, decreasing both weights increases loss. The plotted slice is straight because this loss is linear.

Before the second update, that batch's loss is approximately −0.49549; afterward it is −0.48073, an increase of 0.01476. A less negative loss is larger. This is a concrete limit on per-step descent, not proof of eventual divergence or poor generalization. These linear losses are local teaching examples, not a bounded training objective.

Epsilon Changes The Scale

Epsilon can materially shrink a coordinate update from the same moment state.
Epsilon can materially shrink a coordinate update from the same moment state.

Return to the first update's y coordinate, with corrected direction 0.2 and corrected second moment 0.04. Hold both histories and learning rate 0.1 fixed. With ε = 0.01, the displacement is −0.1(0.2)/(0.2+0.01)≈−0.095238-0.1(0.2)/(0.2+0.01)\approx-0.095238. With ε = 0.2, it becomes −0.1(0.2)/(0.2+0.2)=−0.05-0.1(0.2)/(0.2+0.2)=-0.05.

The two lanes share one starting weight and the same parameter scale as the earlier step rulers. Labels beside the endpoints give displacements, not absolute weights. Epsilon prevents division by zero when the denominator's square-root term vanishes, but can also materially affect an update when it is comparable to that term. These deliberately large teaching values are not recommended defaults.

Zero Gradient Still Leaves History

A zero current gradient need not stop Adam when moment history remains.
A zero current gradient need not stop Adam when moment history remains.

Continue from the second update at w2≈(0.873501,0.879600)w_2\approx(0.873501,0.879600). Let the third minibatch have a constant loss, so its fresh gradient is exactly (0, 0). Adam still halves its stored moments: m3=(0.125,0.0125)m_3=(0.125,0.0125) and v3=(0.5625,0.005625)v_3=(0.5625,0.005625). The correction divisors are now 1−0.53=0.8751-0.5^3=0.875.

After bias correction and scaling with ε = 0.01, the displacement is approximately (−0.017598, −0.015842), giving w3≈(0.855903,0.863758)w_3\approx(0.855903,0.863758). The drawing magnifies parameter distances to 10,000 SVG units per weight unit, compared with 1,400 in the earlier parameter-space views. Dashed projections expose the two components. The constant batch loss stays constant during this move; movement alone does not imply either loss improvement or deterioration. This concerns an explicitly supplied zero gradient, not an implementation that skips a parameter whose gradient is absent.

Where It Breaks

Adam can take different physical steps when the coordinate basis is rotated.
Adam can take different physical steps when the coordinate basis is rotated.

Reset both moments to zero and return to the first gradient (2, 0.2), with rate 0.1 and ε = 0.01. The white ring marks the original-coordinate displacement (−0.099502, −0.095238). The small u/v axes sketch a coordinate basis rotated 45 degrees relative to x/y. Express the same physical weights and objective in that rotated basis before applying Adam.

Let c=1/2c=1/\sqrt2, u=(c,c)u=(c,c) and v=(−c,c)v=(-c,c). The transformed gradient is (2.2c,−1.8c)≈(1.555635,−1.272792)(2.2c,-1.8c)\approx(1.555635,-1.272792). On the first update, the bias-corrected moments are that gradient and its elementwise square. Applying Adam in u/v gives a displacement approximately (−0.099361, +0.099220). Transforming this displacement back into x/y gives:

Δwx=c(Δu−Δv)≈−0.140418,Δwy=c(Δu+Δv)≈−0.000100.\Delta w_x=c(\Delta u-\Delta v)\approx-0.140418,\qquad \Delta w_y=c(\Delta u+\Delta v)\approx-0.000100.

The amber diamond marks that alternative endpoint from the same start. Both endpoints use the original equal x/y scale of 1,400 SVG units per weight unit. The rotated step is almost horizontal, with a real y component too small to resolve at mobile size. These are alternatives, not successive updates.

Elementwise squaring and scaling depend on the selected coordinates: rotating coordinates and rotating an already computed step are different operations. This computed counterexample limits the idea that Adam supplies a basis-independent direction. It does not prove one basis is always better or that either step causes divergence. Adaptation still needs a suitable parameterization, global learning rate and evaluation on the actual objective.

The Quick Version

  • SGD preserves gradient scale differences in its displacements.
  • Adam keeps signed-gradient and squared-gradient histories per parameter.
  • Bias correction compensates for moments initialized at zero.
  • Each coordinate uses its own remembered scale.
  • Momentum can retain its sign after gradients reverse.
  • Adam still needs a global learning rate.
  • An Adam step can increase the current minibatch loss.
  • Epsilon can materially change a normalized step.
  • A zero current gradient can leave continuing motion.
  • Coordinatewise adaptation depends on the chosen basis.