TL;DR
Shampoo is a training rule for models whose parameters form a matrix, such as the weights of a neural-network layer. It keeps two running sums, one over the rows of the gradient and one over its columns, and it rescales each gradient on both sides using inverse fourth roots of those sums. Because it sees how rows and columns move together, it can take better-sized steps than AdaGrad, at the cost of more memory and more matrix arithmetic.
Builds on: AdaGrad, Preconditioning, Matrix Square Root · Leads to: the Shampoo paper, which proves the method and tests it on neural-network training
Where it's used
Training a large dense layer. A layer that maps 2048 inputs to 512 outputs has a weight matrix of 512 by 2048, a little over one million numbers. Plain gradient descent gives all of them one shared learning rate. Shampoo treats the whole matrix as one object, so the step for each weight depends on the rows and columns it belongs to. The 2018 paper tested Shampoo against AdaGrad on neural-network training.
Large-scale distributed training. Computing the matrix roots is expensive, so practical versions split that work across many machines and recompute the roots only every so often. A 2020 follow-up by Anil and colleagues made the method practical at this scale.
Convolution filters. A convolution layer's weights form a tensor with four indices (output channel, input channel, height, width). The "tensor" in the paper's title means Shampoo keeps one running sum per index, not one per matrix. This is why the method is more general than a plain matrix rule.
How it works
Take a parameter matrix with two rows and two columns, which starts at zero. Use learning rate and ignore the tiny safety constant , so the arithmetic stays clean. At step 1 the gradient is
Coordinate 1 has a large gradient and coordinate 2 a small one.
-
Row sum. Compute and add it to a running sum . Here . Check: the first diagonal entry is , and the second is .
-
Column sum. Compute and add it to a running sum . Here . Check: for a diagonal gradient the row and column sums are the same.
-
Inverse fourth roots. Raise each diagonal entry of to the power . The first entry gives , and the second gives . The same happens for . Check: .
-
Precondition the gradient. Multiply on the left by and on the right by . The result is . Check: .
-
Move. Subtract times that matrix from : , so both diagonal entries moved by . Check: the gradients were and , yet both weights moved the same distance.
The flow of one update looks like this:
flowchart LR G["Gradient G (rows by columns)"] --> A["Add G G^T to L and G^T G to R"] A --> B["Take inverse fourth roots of L and R"] B --> C["Compute L^(-1/4) G R^(-1/4)"] C --> D["Subtract eta times that from W"]
If the gradient has off-diagonal entries, and are no longer diagonal, and the roots mix coordinates. That is where Shampoo differs from AdaGrad.
The math
Definition
Let be the parameter matrix, its gradient at step , the learning rate and a tiny positive constant. Start with and , and at each step
where is the inverse fourth root of : the matrix whose eigenvalues are those of raised to the power .
Step 1 in symbols: the two sums
The entry of is the sum over columns of . So records how rows and of the gradient move together. Likewise records how columns move together. In the walkthrough, gives .
Step 2 in symbols: the inverse fourth root
For a symmetric positive definite matrix , with diagonal and holding the eigenvalues, the inverse fourth root is . This is the same construction as the matrix square root, with exponent in place of . When is already diagonal, is the identity and each diagonal entry is raised to , which is the of step 3.
Step 3 in symbols: the preconditioned step
The product rescales the gradient on the left by the row factor and on the right by the column factor. For a square diagonal gradient , every accumulator stays diagonal. Write for the total squared gradient of coordinate . Then , and the update for coordinate is
This is exactly AdaGrad's rule. Shampoo therefore generalises AdaGrad, and the extra matrix structure appears only when the gradient is not diagonal.
Worked examples
Start with , , and the gradient . Ignore .
- Row sum: , since and .
- Column sum: .
- Inverse fourth roots: and . So , and the same for .
- Precondition: the first diagonal entry is . The second is .
- Move: .
Answer: both coordinates moved , even though their gradients were and . AdaGrad's first step gives and before the learning rate, so it agrees with this result.
Let the gradient be at every step, with . Because the gradient is constant, after steps . Find the first step () and the second ().
- Inverse roots at step : and .
- Row 1 of the preconditioned gradient: .
- Row 2: .
- Both rows therefore move by . At that is , and at it is .
- Plain gradient descent at would have moved row 1 by and row 2 by , a hundredfold difference.
Answer: the step at is for both rows, the same as AdaGrad would give. The matrix form has removed the scale difference between the rows.
A 512-by-2048 layer has one weight per entry, so AdaGrad stores one running sum for each weight: numbers. Shampoo stores , which is numbers, and , which is numbers.
- AdaGrad total: .
- Shampoo total: .
- Ratio: .
- The wide dimension dominates, because grows with the square of the number of columns.
Answer: Shampoo stores about 4.25 times as many numbers as AdaGrad for this layer.
Try it: A 1024-by-1024 layer uses how many numbers in Shampoo's and together, compared with AdaGrad's? Answer: each of and is , so Shampoo stores . AdaGrad stores . The ratio is .
Common mistakes and tradeoffs
It is tempting to think Shampoo is just AdaGrad with a matrix in place of a vector. In fact the two sums and capture how the rows and columns of the gradient vary together. They reduce to AdaGrad only when the gradients are diagonal, so on dense layers the two rules behave differently.
It is tempting to think the matrix roots are cheap because they are only a rescaling. In fact each inverse root needs an eigendecomposition, which costs on the order of or operations. That is why real implementations recompute the roots only every few steps and reuse the old ones in between.
It is tempting to think memory is the same as AdaGrad's because the rule looks like a per-weight scaling. In fact Shampoo stores numbers for an -by- layer, which can be far more than the AdaGrad needs when one dimension is large, as in Example 3.
Use Shampoo when a layer is dense, moderately sized and convergence per step matters more than memory. Avoid it for small models, where AdaGrad or Adam is simpler and enough, and for very large dimensions, such as embedding tables with millions of rows, unless the matrix is split into blocks. The alternatives are the diagonal rules, which use one sum per weight and cost little extra.
History
In 2011 Duchi, Hazan and Singer introduced AdaGrad, which gave each parameter its own step size from its own history of squared gradients. In 2018 Vineet Gupta, Tomer Koren and Yoram Singer published Shampoo at ICML. Their paper generalised the per-parameter idea to matrices and higher-order tensors, using one accumulator per index, and compared the method with AdaGrad on neural-network training. The cost of the matrix roots kept the method out of common use at first. A 2020 follow-up by Anil and colleagues made it practical at scale by distributing the root computation and recomputing it only occasionally.
Recap
- AdaGrad keeps one running sum of squared gradients for each parameter.
- For a matrix parameter, Shampoo keeps two sums: over rows and over columns.
- It takes the inverse fourth root of each sum and applies them on the left and right of the gradient.
- For diagonal gradients it reduces exactly to AdaGrad, and its extra cost is memory plus matrix roots.
- Read next: the Shampoo paper, which proves the method and gives its experiments, and Adam for the most common alternative.
Check yourself
QuestionFor a parameter matrix of shape m by n, what are the two accumulators Shampoo keeps, and what shape is each?Show answer
is the sum of , an matrix that records how rows co-vary. is the sum of , an matrix that records how columns co-vary.
QuestionAt step 1, with and (ignore ), what is the first diagonal entry of , and what is coordinate 1 of if ?Show answer
, and , so the entry is . The preconditioned entry is , so coordinate 1 of is .
QuestionWhy does Shampoo's update for a square diagonal gradient reduce to AdaGrad's?Show answer
If every gradient is diagonal, both and stay diagonal, and their -th entries are both . The update for coordinate is , which is AdaGrad's rule.
QuestionA layer has 1024 rows and 256 columns. How many numbers does Shampoo store in and together, and how many does AdaGrad store, and what is the ratio?Show answer
Shampoo: . AdaGrad: . The ratio is .