Adam's state, shape by shape, and what its division buys
shape▲▲△Exact shapes; algebra exact.
STATEMENT
Give the shape of every tensor Adam holds for a concrete model, then establish the property those shapes exist to provide: that the step taken in each coordinate is unchanged when the loss is multiplied by a constant.
GIVEN
The network of I.5.B03 — widths , four affine layers, parameters. Adam as in (I.7.6), with , , .
FIND
The shape of and for each parameter tensor and the totals; then, for a loss rescaled to , the effect on , , and the update.
STRATEGY
Every moment estimate is an average of things with the shape of the gradient, and a gradient has the shape of its parameter — so the shapes follow without any new argument. The scale question is answered by tracking a factor of through (I.7.6) and seeing where it cancels.
SOLUTION
Step 1 — the shapes. and are elementwise running averages of and , so each has exactly the shape of the parameter it belongs to:
| tensor | shape | entries | ||
|---|---|---|---|---|
| same | same | |||
| same | same | |||
| same | same | |||
| same | same | |||
| same | same | |||
| same | same | |||
| same | same | |||
| same | same | |||
| total |
Two extra copies, values, MB in fp32 against the model’s own MB — the of I.7.B04, on a concrete model.
Nothing is per-layer or per-tensor: the state is per scalar parameter. That is what “adaptive” means here, and it is the whole reason the state is this large.
Step 2 — rescale the loss. Replace by for a constant . Gradients are linear, so every becomes . Both moments are linear in their inputs:
The first moment picks up and the second picks up , because one averages the gradient and the other its square.
Step 3 — the cancellation. Bias correction divides both by constants, so the factors survive it: and . The step is
For negligible against the cancels and the update is unchanged. Adam’s step is invariant to rescaling the loss, and by the same argument to rescaling any individual coordinate’s gradient.
Step 4 — what plain descent does instead. Its update is , which scales with directly. Doubling the loss doubles every step, so a rate tuned for one loss scale is wrong for another — and since a sum over a batch and a mean over a batch differ by a factor of , that is not a hypothetical.
Step 5 — where the invariance fails. is an additive constant and does not scale, so the cancellation is approximate. When is small enough that , the denominator is dominated by and the step becomes — linear in again, like plain descent. So the invariance holds over a wide range and breaks at the bottom of it, which is one reason is a number worth knowing rather than a formality: it sets where the adaptive behaviour stops.
Answer
and each have the shape of the parameters, tensor for tensor: entries apiece, values of state, MB in fp32 against MB of weights.
Under : , , , and the update is unchanged while . Plain descent’s update scales with .
Check — sanity
The state has no shape of its own. Every entry of and corresponds to exactly one scalar parameter, so a memory estimate that treats optimiser state as a per-layer overhead is wrong by orders of magnitude.
The exponents on are 1 and 2. They must be: averages and averages . Getting and would mean the square had been dropped, and the cancellation would then leave a stray factor rather than none.
The step’s magnitude is about . With for a steady gradient, the ratio is near 1 and each coordinate moves by roughly per step regardless of its gradient’s size. That is the property that makes a single serviceable across parameters of very different scales, and it is also why Adam’s is typically where SGD’s might be — they are measured in different things.
Where this breaks
Scale invariance is a property of the step, not of training. Rescaling the loss leaves Adam’s trajectory unchanged but not anything coupled to the loss value: gradient clipping by norm triggers at different points, a fixed convergence threshold on the loss means something different, and in mixed precision the gradient scaler exists precisely because the raw magnitude decides whether fp16 overflows or flushes to zero. “Adam does not care about the loss scale” is true of one equation and false of the system around it.
The invariance is also per-coordinate, which is a double-edged property. A parameter whose gradient is genuinely tiny because it barely matters is stepped just as far as one whose gradient is large, since only the ratio survives. That is the intended behaviour and it is also why Adam can keep moving parameters that plain descent would leave alone — sometimes usefully, sometimes as an argument for decoupled weight decay, which I.7.X04 takes up.
Finally, sits outside the square root in (I.7.6) and some implementations place it inside. The two differ, and the difference shows up exactly in the small-gradient regime where the invariance is already breaking down. It is not a detail worth arguing about often, but it is a real difference between two things both called Adam.
Variation
Work out what happens under a rescaling of the parameters rather than the loss — replace by with the model reparameterised to compensate. Is Adam invariant to that too? The answer is different from Step 3’s, and it is the reason normalisation layers still matter when Adam is in use.