What two running averages does Adam maintain, and how does its update rule combine them?
answer
- two exponential moving averages, not one
- one of the gradient, one of its square
- the second moment is uncentered
- numerator over square root of denominator
- a steady gradient gives a ratio near one
basics
~20 sAdam keeps two exponential moving averages per parameter: one of the gradient, one of the squared gradient. The update is the first divided by the square root of the second, so each parameter gets its own step size.
solid answer
~50 sAdam stores two numbers per parameter. The first moment `m` is an exponential moving average of the gradient, with a decay rate near 0.9; the second moment `v` is an exponential moving average of the *squared* gradient, with a decay rate near 0.999. After bias correction the step is `w <- w - lr * m_hat / (sqrt(v_hat) + eps)`. The numerator is the smoothed direction, the denominator is roughly the recent root-mean-square magnitude of that parameter's gradient, so the ratio is close to plus or minus one whenever the gradient is consistent — a parameter with a steady but tiny gradient still moves about a full learning rate, and a parameter whose gradient keeps flipping sign has `m` near zero while the denominator stays large, so it barely moves. That normalization is why Adam drops the loss fast in the first few hundred steps with no per-layer tuning.
go deeper
Be ready to say what Adam stores beyond the weights: a moving average of the gradient and a moving average of the squared gradient, and that dividing one by the square root of the other gives each parameter its own step size.
Explain the two update equations and the meaning of each decay rate, and be precise that the second moment is an average of squared gradients rather than a variance. Show what the ratio becomes for a steady gradient versus an alternating one.
Demonstrate that you use the ratio as a diagnostic: a parameter with a consistent tiny gradient still moves a full learning rate, which is why Adam finds progress in layers that plain gradient descent leaves stranded, and why a badly scaled layer can go unnoticed for a long time.
Own the tradeoff that the normalization buys robustness to per-layer gradient scale at the cost of hiding it. Be ready to argue when a team should keep the global learning rate as the single tuned knob and when scale problems should be fixed in the model rather than absorbed by the optimizer.
## The state Adam carries Plain gradient descent stores nothing between steps: it moves each weight by `lr * g`, where `g` is the current gradient of the loss with respect to that weight. Adam stores two extra numbers **per parameter**, updated every step from that same gradient: ``` m_t = b1 * m_(t-1) + (1 - b1) * g_t # first moment v_t = b2 * v_(t-1) + (1 - b2) * g_t * g_t # second moment ``` Both are exponential moving averages (EMAs). `b1` and `b2` are decay rates in [0, 1); the widely used values are `b1 = 0.9` and `b2 = 0.999`. An EMA with decay `b` has an effective memory of roughly `1 / (1 - b)` steps: about 10 steps for the first moment, about 1000 for the second. Both are initialised to zero, which is why the bias-correction terms `m_hat = m_t / (1 - b1^t)` and `v_hat = v_t / (1 - b2^t)` exist. The update itself is: ``` w <- w - lr * m_hat / (sqrt(v_hat) + eps) ``` ## What each moment means The **first moment** is a smoothed estimate of the gradient direction — momentum, written as an average rather than a sum. Averaging over roughly ten steps cancels a good part of the minibatch noise, so the direction the optimizer actually follows is steadier than any single batch's gradient. The **second moment** is the average of the *squared* gradient. Two precision points matter here. First, it is an **uncentered** second moment: it estimates `E[g^2]`, the mean square, not `Var[g]`. It equals the variance only in the special case where the mean gradient is zero. Calling it "the variance" is the single most common slip in interviews. Second, `sqrt(v)` therefore has the units and scale of a gradient magnitude — it is roughly the recent root-mean-square size of that parameter's gradient. ## Why the ratio is the whole idea Because the numerator has the scale of a gradient and the denominator has the scale of a gradient magnitude, the ratio `m_hat / sqrt(v_hat)` is close to dimensionless, and its size is bounded near one in the common cases. Consider two parameters: - **A consistent gradient.** Suppose one parameter's gradient is `+0.0001` on nearly every batch. Then `m_hat` settles at about `0.0001` and `sqrt(v_hat)` at about `0.0001`, so the ratio is about `+1` and the parameter moves by about the full learning rate — even though its gradient is a hundred times smaller than some other layer's. Plain gradient descent would have crawled here. - **An alternating gradient.** Suppose another parameter's gradient is `+0.5, -0.5, +0.5, ...`. The first moment averages toward zero, while the second moment averages `0.25` and its square root stays at `0.5`. The ratio collapses toward zero and the parameter barely moves. Adam damps itself exactly where the gradient signal is inconsistent. So the update behaves like a smoothed sign of the gradient scaled by the learning rate, modulated by how consistent that gradient has been. This is what people mean by "per-parameter learning rates": Adam does not tune anything, it just divides by a running measure of each parameter's own gradient scale. ## Consequences worth naming **Scale invariance.** Multiply the loss by 1000 and every gradient is multiplied by 1000; `m` scales by 1000, `sqrt(v)` scales by 1000, and the ratio is unchanged. The update is invariant to a rescaling of the objective (until `eps` becomes comparable to `sqrt(v_hat)`, which breaks the invariance at the small end). Plain gradient descent has no such property — rescaling the loss is equivalent to rescaling the learning rate. **Still one learning rate to tune.** The normalization removes the need to pick a different step size per layer; it does not remove the need to pick `lr`. "Adam has no learning rate" is wrong, and the global rate remains the highest-leverage hyperparameter. **Two extra tensors of optimizer state.** Every parameter now carries `m` and `v` alongside the weight and its gradient. **The decay rates are separate on purpose.** The first moment must react quickly to a genuine change of direction, so its memory is short. The second moment estimates a magnitude and wants a low-variance estimate, so its memory is long. Setting them equal is not a simplification — it changes both the smoothing behaviour and the way the bias corrections interact. A good one-line summary for an interviewer: Adam takes a momentum step, then divides it by the recent root-mean-square size of that parameter's gradient, so every parameter travels at a comparable pace regardless of how large its gradients happen to be.
- Does Adam's second moment estimate the variance of the gradient?No. It is an uncentered second moment: an average of `g^2`, which estimates `E[g^2]`, the mean square. Variance is `E[g^2] - E[g]^2`, so the two coincide only when the mean gradient is zero. That distinction matters because it means `sqrt(v)` tracks the typical magnitude of the gradient, including any persistent bias in it, rather than only its fluctuation.
- Why is Adam's update almost unchanged if you multiply the loss by 100?Every gradient scales by 100, so the first moment scales by 100 and the square root of the second moment scales by 100 as well. The ratio, and therefore the step, is unchanged. The one place it breaks is the epsilon in the denominator, which is a fixed constant and does not scale — once the root second moment is comparable to epsilon, the invariance is gone.
- What does Adam do to a parameter whose gradient keeps flipping sign?The first moment averages toward zero because positive and negative contributions cancel, while the second moment keeps averaging the squares and stays at the gradient's root-mean-square magnitude. The ratio collapses toward zero, so that parameter's steps shrink. Adam self-damps in directions where the batch-to-batch gradient signal is inconsistent, without any explicit detection of oscillation.
It is like pacing runners by their own recent stride: the momentum average says which way you are heading, and dividing by the recent typical stride length makes a short-strided runner and a long-strided one cover the same ground per step.
saying these in an interview costs you the question
- Says the second moment estimates the gradient's variance
- Claims Adam removes the need to tune a learning rate
- Describes the second moment as a running sum that never decays
- Says the two decay rates should be equal or that only one exists
- Explains Adam as momentum alone, ignoring the denominator