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:
Coordinates with large past gradients get small steps; rarely updated coordinates keep large steps. This is ideal for sparse features. Its weakness: only grows, so the effective learning rate decays like and can stall long training runs.
RMSProp
Replace the sum by an exponential moving average, so old gradients are forgotten:
with to . estimates the recent root-mean-square gradient of each coordinate, so the step is roughly scale-free: about per coordinate regardless of the gradient's magnitude.
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 for the steep coordinate. At that coordinate oscillates and shrinks only by a factor per step, while the flat one shrinks by 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 , 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 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.
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 callsalpha).- (around ) 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 ) at step ?
Answer
.
Solution
, so the step is : it decays even though nothing changed.
Exercise 2: RMSProp steady state
A coordinate receives a constant gradient . What step does RMSProp take after many steps?
Answer
.
Solution
, so the step is : 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
is 10 for the rare parameter and 100 for the frequent one, so the steps are and .
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?
- The learning rate
- The square root of the sum of its past squared gradients
- The number of steps
- The loss
RMSProp fixes AdaGrad's problem of…
- too large steps
- an ever-shrinking effective learning rate
- needing gradients
- high memory use
Adaptive methods are a form of…
- diagonal preconditioning
- second-order Newton steps with the full Hessian
- regularization only
- data augmentation