TL;DR
AdaGrad is a training rule for models that learn by gradient steps. It keeps a running total of the squared gradient for each parameter, and it divides that parameter's learning rate by the square root of the total. A parameter that has received large gradients takes small steps, and a parameter that has received few gradients keeps a large step.
Builds on: Stochastic Gradient Descent, Learning Rate · Leads to: Adam Optimizer, which replaces the running total with a decaying average
Where it's used
Sparse text models. A linear classifier that reads bag-of-words features has a weight for every word. Common words get a gradient in almost every example, and rare words get one only occasionally. AdaGrad gives the rare words larger steps when they do appear. Duchi, Hazan and Singer proposed the method in 2011 with sparse, high-dimensional problems like this in mind.
Large sparse linear models. Logistic-regression models with very many features, most of them inactive in any single example, have the same problem. Each feature's weight is updated only when the feature is present, so per-parameter step sizes matter.
Word-embedding training. The word vectors in a model get gradients only when their word appears. Per-parameter step sizes let those vectors learn from few examples without a single global step that is either too small for them or too large for common words.
How it works
Take one parameter whose gradient is at step . Start with , learning rate , and ignore the tiny safety constant that AdaGrad normally adds, so the arithmetic stays clean. Feed in the gradient at each of the first two steps.
-
First gradient. Add the square of the gradient to the running total: . Take the square root, . Check: the total is the sum of all squared gradients seen so far, which here is one term.
-
First step. Move by the learning rate times the gradient, divided by the square root of the total: . The parameter moves from to . Check: the step is , which is the learning rate because the gradient equals its own square root here.
-
Second gradient. The same gradient arrives again. The total grows to , and . Check: the total only grows; it never shrinks.
-
Second step. The step is now . The parameter moves from to . Check: the same gradient produced a smaller step than in step 2, because the total grew.
The plot shows what happens when the gradient is held at and the learning rate is . The step at time is , and it shrinks as the total grows.
- step size 1/sqrt(t)
The math
Definition
Let be the parameters, the gradient of the Loss Function at step , the Learning Rate and a tiny positive constant. All operations on vectors act coordinate by coordinate. The update is
with . Here is the element-wise product, so holds one running total per parameter.
Step 1 in symbols: the running total
Step 1 of the walkthrough is , and in general . The total is a sum, so it can only grow. Each parameter has its own total, because the square is taken element by element.
Step 2 in symbols: the scaled step
The step is . Its size depends on the gradient only through the ratio . If every gradient for a parameter has the same size , the ratio is at step , which is the curve in the plot. The step size of each parameter is therefore set relative to its own gradient history, not the global scale of all gradients.
Step 3 in symbols: the second step
With at steps 1 and 2, the total is , and the second step is , which is the value in the walkthrough. This is the general rule applied to the sequence of gradients .
Step 4 in symbols: the rare feature
Suppose a parameter receives a gradient only at a few steps and zero otherwise. Its total stays small, so when it does receive a gradient its step is with a small denominator. Compare this with a parameter that has received a gradient at every step, which has a large denominator. Example 3 uses this comparison.
Worked examples
Start with , learning rate , and gradients and .
- Step 1 total: , so .
- Step 1 move: . The gradient is positive, so the parameter moves against it: .
- Step 2 total: , so .
- Step 2 move: . Moving against a negative gradient means adding , so .
Answer: . The sign of the gradient flipped, so the second step turned the parameter around, but its size was still set by the total of both squared gradients.
Parameter gets gradient at every step. Parameter gets gradient at every step. Both use . Compare their steps after two updates.
- Step 1, parameter : , , step .
- Step 1, parameter : , , step .
- Both parameters moved by the same at step 1, even though their gradients differed by a factor of 100.
- Step 2, parameter : , , step .
- Step 2, parameter : , , step .
- Both parameters again moved the same amount.
Answer: each step is for both, so the two parameters move at the same rate despite a 100-fold difference in gradient size. This is the point of the per-parameter scaling.
A classifier with two features: the common word "the" has a gradient at every step, and the rare word "cassowary" has a gradient only at step 100. Use .
- At step 100 the common word has received 100 gradients of size 1, so its total is and .
- The common word's step at step 100 is .
- The rare word has received only one gradient before (its first, at step 100), so its total is and .
- The rare word's step is .
- The rare word moves ten times as far as the common word, in the step where both had a gradient of 1.
Answer: the rare word moves , the common word moves . Plain gradient descent with one shared learning rate would have moved both by . That is too large for the common word, and it gives the rare word no extra size.
Try it: Suppose the common word has received 400 gradients of size 1 by step 400, and the rare word receives its first gradient of size 1 at step 400, with . How many times larger is the rare word's step than the common word's step? Answer: The common word's total is , so and its step is . The rare word's total is , so its step is . The ratio is .
Common mistakes and tradeoffs
It is tempting to think AdaGrad's steps are always large enough to keep training fast. In fact the running total only grows, so every parameter's effective learning rate shrinks toward zero over time. In long runs of deep networks the steps can become too small to make progress. This is why Adam Optimizer replaces the sum with a decaying average, so that old gradients are forgotten.
It is tempting to think the size of a parameter's step is set by its gradient. In fact the step is normalised by the history of that parameter's gradients. Example 2 shows a gradient 100 times larger producing the same first step, because the step is divided by the root of its own total.
It is tempting to think the learning rate matters less once the steps are adaptive. In fact still sets the scale of every step. Example 1 uses throughout, and changing it changes every move.
When to use it: sparse features, where most parameters receive gradients rarely, and convex-like problems where a steadily shrinking step is acceptable. It was designed for online learning, where each example is seen once.
When not to use it: long training runs of deep networks, where the steps may shrink before the model converges. Use Adam Optimizer or RMSProp, which forget old gradients, or a plain Stochastic Gradient Descent schedule.
Alternatives: RMSProp, which uses a decaying average of squared gradients; Adam, which adds Momentum; and Preconditioning methods in general, of which AdaGrad is one example.
History
Stochastic approximation, the idea behind gradient steps computed from noisy data, goes back to 1951. Per-parameter step sizes became a serious research topic in the 2010s, when models with very large sparse inputs became common. Duchi, Hazan and Singer published AdaGrad in 2011 in the Journal of Machine Learning Research, with analysis for online convex problems. It caught on because it removed the need to tune a separate learning rate for every feature by hand. Its shrinking steps then became the problem that RMSProp and Adam were designed to fix.
Recap
- Each parameter keeps a running total of its squared gradients.
- Its step is the learning rate divided by the square root of that total, times its gradient.
- Parameters with rare gradients keep large steps, and parameters with frequent gradients take small ones.
- The total only grows, so the steps shrink over long training, which is why deep-learning practice moved to Adam Optimizer.
Read next: Adam Optimizer for the decaying-average fix, and Learning Rate for the global scale that AdaGrad still multiplies every step by.
Check yourself
QuestionWhy does AdaGrad divide by the square root of the sum of squared gradients rather than by the sum itself?Show answer
The square root puts the denominator on the same scale as the gradient. If every gradient has size , the sum of squares over steps is , and its square root is , so the step scales as with the gradient size cancelled. Dividing by the raw sum would make the step shrink much faster than the gradient size warrants.
QuestionA parameter gets a gradient of 3 at step 1 and a gradient of 4 at step 2. With and no safety constant, what is its total , and what is its step at step 2?Show answer
The total is , so . The step at step 2 is .
QuestionWhy can AdaGrad stop making progress in a long deep-network training run, and what does Adam change to prevent it?Show answer
The running total only grows, so each parameter's step shrinks toward zero as training continues, even if the gradients are still useful. Adam replaces the total with an exponentially decaying average, so old gradients are gradually forgotten and the steps do not shrink to nothing.
QuestionTwo parameters have constant gradients of 1 and 100. After one step each, with , how do their steps compare?Show answer
Both steps are . For the first parameter , so the step is . For the second, , so and the step is . The per-parameter normalisation makes the first step the same for both.