Search

Search papers and lessons

Shampoo Optimizer

Level 4 · 10 min read

Shampoo trains a weight matrix by keeping one running sum over its rows and one over its columns, then rescaling each gradient on both sides with inverse fourth roots of those sums.

On this page

  • Colour = topic mix
  • Linear algebra
  • Probability
  • Calculus
  • Discrete math
  • Optimizers
  • Losses & regularization
  • ReadReady to readNot yet

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 WW with two rows and two columns, which starts at zero. Use learning rate η=0.1\eta = 0.1 and ignore the tiny safety constant ε\varepsilon, so the arithmetic stays clean. At step 1 the gradient is

G1=(3001).G_1 = \begin{pmatrix} 3 & 0 \\ 0 & 1 \end{pmatrix}.

Coordinate 1 has a large gradient and coordinate 2 a small one.

  1. Row sum. Compute G1G1TG_1 G_1^{\mathsf T} and add it to a running sum LL. Here L1=diag(9,1)L_1 = \mathrm{diag}(9, 1). Check: the first diagonal entry is 3×3=93 \times 3 = 9, and the second is 1×1=11 \times 1 = 1.

  2. Column sum. Compute G1TG1G_1^{\mathsf T} G_1 and add it to a running sum RR. Here R1=diag(9,1)R_1 = \mathrm{diag}(9, 1). Check: for a diagonal gradient the row and column sums are the same.

  3. Inverse fourth roots. Raise each diagonal entry of L1L_1 to the power −14-\tfrac14. The first entry gives 9−1/4=1/3≈0.57749^{-1/4} = 1/\sqrt{3} \approx 0.5774, and the second gives 11. The same happens for R1R_1. Check: 0.57744≈0.1111=1/90.5774^4 \approx 0.1111 = 1/9.

  4. Precondition the gradient. Multiply on the left by L1−1/4L_1^{-1/4} and on the right by R1−1/4R_1^{-1/4}. The result is diag(0.5774×3×0.5774,  1×1×1)=diag(1,1)\mathrm{diag}(0.5774 \times 3 \times 0.5774,\; 1 \times 1 \times 1) = \mathrm{diag}(1, 1). Check: 0.5774×3×0.5774≈1.000.5774 \times 3 \times 0.5774 \approx 1.00.

  5. Move. Subtract η\eta times that matrix from WW: W1=−0.1 IW_1 = -0.1\,I, so both diagonal entries moved by 0.10.1. Check: the gradients were 33 and 11, 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, LL and RR are no longer diagonal, and the roots mix coordinates. That is where Shampoo differs from AdaGrad.

The math

Definition

Definition: Shampoo update for a matrix parameter

Let Wt∈Rm×nW_t \in \mathbb{R}^{m \times n} be the parameter matrix, GtG_t its gradient at step tt, η\eta the learning rate and ε\varepsilon a tiny positive constant. Start with L0=εImL_0 = \varepsilon I_m and R0=εInR_0 = \varepsilon I_n, and at each step

Lt=Lt−1+GtGtT,Rt=Rt−1+GtTGt,L_t = L_{t-1} + G_t G_t^{\mathsf T}, \qquad R_t = R_{t-1} + G_t^{\mathsf T} G_t,Wt+1=Wt−η Lt−1/4 Gt Rt−1/4,W_{t+1} = W_t - \eta\, L_t^{-1/4}\, G_t\, R_t^{-1/4},

where Lt−1/4L_t^{-1/4} is the inverse fourth root of LtL_t: the matrix whose eigenvalues are those of LtL_t raised to the power −14-\tfrac14.

Step 1 in symbols: the two sums

The entry (i,j)(i,j) of GtGtTG_t G_t^{\mathsf T} is the sum over columns kk of GikGjkG_{ik} G_{jk}. So LtL_t records how rows ii and jj of the gradient move together. Likewise RtR_t records how columns move together. In the walkthrough, G1=diag(3,1)G_1 = \mathrm{diag}(3, 1) gives G1G1T=diag(9,1)G_1 G_1^{\mathsf T} = \mathrm{diag}(9, 1).

Step 2 in symbols: the inverse fourth root

For a symmetric positive definite matrix L=QΛQTL = Q \Lambda Q^{\mathsf T}, with Λ\Lambda diagonal and holding the eigenvalues, the inverse fourth root is L−1/4=QΛ−1/4QTL^{-1/4} = Q \Lambda^{-1/4} Q^{\mathsf T}. This is the same construction as the matrix square root, with exponent −14-\tfrac14 in place of 12\tfrac12. When LL is already diagonal, QQ is the identity and each diagonal entry is raised to −14-\tfrac14, which is the 9−1/4=1/39^{-1/4} = 1/\sqrt{3} of step 3.

Step 3 in symbols: the preconditioned step

The product Lt−1/4GtRt−1/4L_t^{-1/4} G_t R_t^{-1/4} rescales the gradient on the left by the row factor and on the right by the column factor. For a square diagonal gradient Gt=diag(g1,t,… )G_t = \mathrm{diag}(g_{1,t}, \dots), every accumulator stays diagonal. Write si=∑tgi,t2s_i = \sum_{t} g_{i,t}^2 for the total squared gradient of coordinate ii. Then Lii=Rii=siL_{ii} = R_{ii} = s_i, and the update for coordinate ii is

η si−1/4 gi,t si−1/4=η gi,tsi.\eta\, s_i^{-1/4}\, g_{i,t}\, s_i^{-1/4} = \frac{\eta\, g_{i,t}}{\sqrt{s_i}}.

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

Example
1. A diagonal gradient, one step (tiny case)

Start with W0=0W_0 = 0, η=0.1\eta = 0.1, and the gradient G1=diag(2,8)G_1 = \mathrm{diag}(2, 8). Ignore ε\varepsilon.

  1. Row sum: L1=G1G1T=diag(4,64)L_1 = G_1 G_1^{\mathsf T} = \mathrm{diag}(4, 64), since 22=42^2 = 4 and 82=648^2 = 64.
  2. Column sum: R1=G1TG1=diag(4,64)R_1 = G_1^{\mathsf T} G_1 = \mathrm{diag}(4, 64).
  3. Inverse fourth roots: 4−1/4=1/2≈0.70714^{-1/4} = 1/\sqrt{2} \approx 0.7071 and 64−1/4=1/(22)≈0.353664^{-1/4} = 1/(2\sqrt{2}) \approx 0.3536. So L1−1/4=diag(0.7071,0.3536)L_1^{-1/4} = \mathrm{diag}(0.7071, 0.3536), and the same for R1−1/4R_1^{-1/4}.
  4. Precondition: the first diagonal entry is 0.7071×2×0.7071=1.00.7071 \times 2 \times 0.7071 = 1.0. The second is 0.3536×8×0.3536=1.00.3536 \times 8 \times 0.3536 = 1.0.
  5. Move: W1=0−0.1×diag(1,1)=diag(−0.1,−0.1)W_1 = 0 - 0.1 \times \mathrm{diag}(1, 1) = \mathrm{diag}(-0.1, -0.1).

Answer: both coordinates moved −0.1-0.1, even though their gradients were 22 and 88. AdaGrad's first step gives 2/4=12/\sqrt{4} = 1 and 8/64=18/\sqrt{64} = 1 before the learning rate, so it agrees with this result.

Example
2. Two rows with very different scales (standard case)

Let the gradient be G=diag(10,0.1)G = \mathrm{diag}(10, 0.1) at every step, with η=0.1\eta = 0.1. Because the gradient is constant, after tt steps Lt=Rt=t diag(100,0.01)L_t = R_t = t\,\mathrm{diag}(100, 0.01). Find the first step (t=1t = 1) and the second (t=2t = 2).

  1. Inverse roots at step tt: 100−1/4⋅t−1/4=t−1/4/10100^{-1/4} \cdot t^{-1/4} = t^{-1/4}/\sqrt{10} and 0.01−1/4⋅t−1/4=10 t−1/40.01^{-1/4} \cdot t^{-1/4} = \sqrt{10}\, t^{-1/4}.
  2. Row 1 of the preconditioned gradient: t−1/410×10×t−1/410=t−1/2\dfrac{t^{-1/4}}{\sqrt{10}} \times 10 \times \dfrac{t^{-1/4}}{\sqrt{10}} = t^{-1/2}.
  3. Row 2: 10 t−1/4×0.1×10 t−1/4=t−1/2\sqrt{10}\, t^{-1/4} \times 0.1 \times \sqrt{10}\, t^{-1/4} = t^{-1/2}.
  4. Both rows therefore move by η t−1/2\eta\, t^{-1/2}. At t=1t = 1 that is 0.10.1, and at t=2t = 2 it is 0.1/2≈0.07070.1/\sqrt{2} \approx 0.0707.
  5. Plain gradient descent at t=1t = 1 would have moved row 1 by 1.01.0 and row 2 by 0.010.01, a hundredfold difference.

Answer: the step at t=2t = 2 is 0.07070.0707 for both rows, the same as AdaGrad would give. The matrix form has removed the scale difference between the rows.

Example
3. A realistic layer and its memory cost (realistic case)

A 512-by-2048 layer has one weight per entry, so AdaGrad stores one running sum for each weight: 512×2048=1,048,576512 \times 2048 = 1{,}048{,}576 numbers. Shampoo stores LL, which is 512×512=262,144512 \times 512 = 262{,}144 numbers, and RR, which is 2048×2048=4,194,3042048 \times 2048 = 4{,}194{,}304 numbers.

  1. AdaGrad total: 1,048,5761{,}048{,}576.
  2. Shampoo total: 262,144+4,194,304=4,456,448262{,}144 + 4{,}194{,}304 = 4{,}456{,}448.
  3. Ratio: 4,456,448/1,048,576=4.254{,}456{,}448 / 1{,}048{,}576 = 4.25.
  4. The wide dimension dominates, because RR 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 LL and RR together, compared with AdaGrad's? Answer: each of LL and RR is 1024×1024=1,048,5761024 \times 1024 = 1{,}048{,}576, so Shampoo stores 2,097,1522{,}097{,}152. AdaGrad stores 1,048,5761{,}048{,}576. The ratio is 22.

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 LL and RR 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 m3m^3 or n3n^3 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 m2+n2m^2 + n^2 numbers for an mm-by-nn layer, which can be far more than the mnmn 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: GGTG G^{\mathsf T} over rows and GTGG^{\mathsf T} G 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

LL is the sum of GGTG G^{\mathsf T}, an m×mm \times m matrix that records how rows co-vary. RR is the sum of GTGG^{\mathsf T} G, an n×nn \times n matrix that records how columns co-vary.

QuestionAt step 1, with G1=diag(4,1)G_1 = \mathrm{diag}(4, 1) and η=0.1\eta = 0.1 (ignore ε\varepsilon), what is the first diagonal entry of L1−1/4L_1^{-1/4}, and what is coordinate 1 of W1W_1 if W0=0W_0 = 0?Show answer

L1=diag(16,1)L_1 = \mathrm{diag}(16, 1), and 16−1/4=1/216^{-1/4} = 1/2, so the entry is 0.50.5. The preconditioned entry is 0.5×4×0.5=10.5 \times 4 \times 0.5 = 1, so coordinate 1 of W1W_1 is −0.1-0.1.

QuestionWhy does Shampoo's update for a square diagonal gradient reduce to AdaGrad's?Show answer

If every gradient is diagonal, both LL and RR stay diagonal, and their ii-th entries are both si=∑gi,t2s_i = \sum g_{i,t}^2. The update for coordinate ii is η si−1/4gi,tsi−1/4=η gi,t/si\eta\, s_i^{-1/4} g_{i,t} s_i^{-1/4} = \eta\, g_{i,t}/\sqrt{s_i}, which is AdaGrad's rule.

QuestionA layer has 1024 rows and 256 columns. How many numbers does Shampoo store in LL and RR together, and how many does AdaGrad store, and what is the ratio?Show answer

Shampoo: 10242+2562=1,048,576+65,536=1,114,1121024^2 + 256^2 = 1{,}048{,}576 + 65{,}536 = 1{,}114{,}112. AdaGrad: 1024×256=262,1441024 \times 256 = 262{,}144. The ratio is 1,114,112/262,144=4.251{,}114{,}112 / 262{,}144 = 4.25.

Sources