TExcelTAD
Home › Math for Deep Learning & LLMs › AdaGrad and RMSProp

Adaptive Learning Rates: AdaGrad and RMSProp

A single learning rate for all parameters is a compromise: some parameters see large, frequent gradients (common words' embeddings), others small or rare ones (rare words). Adaptive methods give every parameter its own step size, scaled by the history of its gradients. AdaGrad accumulates all past squared gradients; RMSProp uses a moving average instead. Together with momentum, they lead directly to Adam.

AdaGrad

Keep a per-coordinate sum of squared gradients and divide by its square root:

st=st−1+gt⊙gt,θt+1=θt−ηst+ϵ⊙gt\mathbf{s}_t = \mathbf{s}_{t-1} + \mathbf{g}_t \odot \mathbf{g}_t, \qquad \boldsymbol{\theta}_{t+1} = \boldsymbol{\theta}_t - \frac{\eta}{\sqrt{\mathbf{s}_t} + \epsilon} \odot \mathbf{g}_t

Coordinates with large past gradients get small steps; rarely updated coordinates keep large steps. This is ideal for sparse features. Its weakness: st\mathbf{s}_t only grows, so the effective learning rate decays like 1/t1/\sqrt{t} and can stall long training runs.

RMSProp

Replace the sum by an exponential moving average, so old gradients are forgotten:

st=ρ st−1+(1−ρ) gt⊙gt,θt+1=θt−ηst+ϵ⊙gt\mathbf{s}_t = \rho\,\mathbf{s}_{t-1} + (1 - \rho)\,\mathbf{g}_t \odot \mathbf{g}_t, \qquad \boldsymbol{\theta}_{t+1} = \boldsymbol{\theta}_t - \frac{\eta}{\sqrt{\mathbf{s}_t} + \epsilon} \odot \mathbf{g}_t

with ρ≈0.9\rho \approx 0.9 to 0.990.99. st\sqrt{\mathbf{s}_t} estimates the recent root-mean-square gradient of each coordinate, so the step g/s\mathbf{g}/\sqrt{\mathbf{s}} is roughly scale-free: about ±η\pm\eta per coordinate regardless of the gradient's magnitude.

python · try it yourself
import numpy as np

H = np.diag([1.0, 100.0])                    # a badly scaled quadratic
grad = lambda th: H @ th
def run(method, lr, steps=200):
    th, s = np.array([1.0, 1.0]), np.zeros(2)
    for t in range(steps):
        g = grad(th)
        if method == "sgd":
            th = th - lr * g
        elif method == "adagrad":
            s += g * g; th = th - lr * g / (np.sqrt(s) + 1e-8)
        else:
            s = 0.9 * s + 0.1 * g * g; th = th - lr * g / (np.sqrt(s) + 1e-8)
    return th
print("SGD     lr 0.0199:", np.round(run("sgd", 0.0199), 5))
print("AdaGrad lr 0.5   :", np.round(run("adagrad", 0.5), 5))
print("RMSProp lr 0.01  :", np.round(run("rmsprop", 0.01), 5))
SGD     lr 0.0199: [0.01795 0.13398]
AdaGrad lr 0.5   : [0. 0.]
RMSProp lr 0.01  : [3.e-05 3.e-05]

SGD needs a step below 2/1002/100 for the steep coordinate. At 0.01990.0199 that coordinate oscillates and shrinks only by a factor 0.990.99 per step, while the flat one shrinks by 0.980.98 per step, so after 200 steps neither is close to 0. The adaptive methods rescale each coordinate by its own gradient size and drive both to the optimum. (On noisy problems RMSProp with a constant rate keeps jittering at a scale set by η\eta, which is why it is combined with a decaying schedule.)

Diagonal preconditioning

Both methods are diagonal preconditioners: they multiply the gradient by a diagonal matrix D−1D^{-1} that approximates the inverse curvature per coordinate. When the Hessian is close to diagonal (in the parameter axes), this undoes much of the ill conditioning. When it is not, a diagonal rescaling cannot fix rotated valleys; full-matrix methods (Shampoo, K-FAC) go further at higher cost.

python · try it yourself
import numpy as np

rng = np.random.default_rng(0)
g_small = rng.normal(0, 0.001, 50); g_large = rng.normal(0, 100.0, 50)
for name, gs in (("tiny gradients", g_small), ("huge gradients", g_large)):
    s = 0.0
    for g in gs:
        s = 0.9 * s + 0.1 * g * g
    print(f"{name}: raw last gradient {gs[-1]:+.2e}, normalized step {gs[-1] / np.sqrt(s):+.3f}")
tiny gradients: raw last gradient +1.32e-03, normalized step +1.104
huge gradients: raw last gradient -1.40e+02, normalized step -1.389

In software

  • torch.optim.Adagrad, torch.optim.RMSprop(alpha=0.99) (PyTorch calls ρ\rho alpha).
  • ϵ\epsilon (around 10−810^{-8}) prevents division by zero; for bf16 training a larger value may be needed.
  • AdaGrad remains popular for sparse embedding tables in recommendation models.

Exercises

Exercise 1: AdaGrad's decay

A coordinate receives a gradient of exactly 2 every step. What is AdaGrad's step size (ignoring ϵ\epsilon) at step tt?

Answer

η/t\eta/\sqrt{t}.

Solution

st=4ts_t = 4t, so the step is η⋅2/4t=η/t\eta \cdot 2/\sqrt{4t} = \eta/\sqrt{t}: it decays even though nothing changed.

Exercise 2: RMSProp steady state

A coordinate receives a constant gradient gg. What step does RMSProp take after many steps?

Answer

ηsign⁡(g)\eta\operatorname{sign}(g).

Solution

s→g2s \to g^2, so the step is ηg/∣g∣\eta g/\lvert g \rvert: the size of the gradient no longer matters, only its sign.

Exercise 3: Sparse features (code)

A parameter gets gradient 1 only every 10th step (0 otherwise), another gets 1 every step. After 100 steps, compare their AdaGrad step sizes for the next nonzero gradient.

Answer

The rare one's step is about 3.16 times larger.

Solution

ss is 10 for the rare parameter and 100 for the frequent one, so the steps are η/10\eta/\sqrt{10} and η/10\eta/10.

python · try it yourself
import numpy as np
s_rare = sum(1.0 for t in range(100) if t % 10 == 0); s_freq = 100.0
print(round((1 / np.sqrt(s_rare)) / (1 / np.sqrt(s_freq)), 3))
3.162

Test yourself

What does AdaGrad divide each coordinate's gradient by?

  1. The learning rate
  2. The square root of the sum of its past squared gradients
  3. The number of steps
  4. The loss

RMSProp fixes AdaGrad's problem of…

  1. too large steps
  2. an ever-shrinking effective learning rate
  3. needing gradients
  4. high memory use

Adaptive methods are a form of…

  1. diagonal preconditioning
  2. second-order Newton steps with the full Hessian
  3. regularization only
  4. data augmentation