Search

Search papers and lessons

Shampoo: Preconditioned Stochastic Tensor Optimization

Vineet Gupta, Tomer Koren, Yoram Singer · 2018

Shampoo preconditions each weight matrix with two small matrices, one per side, which approximates full-matrix AdaGrad at a cost close to SGD. It trains deep nets with little extra step time.

On this page

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

TL;DR

Shampoo is an optimizer for training neural networks. It keeps one small preconditioning matrix for each dimension of each weight tensor, rather than one number per weight or one huge matrix over all weights. Each step multiplies the gradient by these matrices on its left and right. The result approximates the full-matrix AdaGrad step at a memory and computation cost much lower than full-matrix AdaGrad, and in most of the paper's experiments each step took about as long as an SGD, Adam or AdaGrad step.

Builds on: AdaGrad, Preconditioning, Tensor Product, Shampoo Optimizer · Leads to: later optimizers that precondition whole weight matrices with structured factors

Why it mattered

The paper gave a practical method, with a proof, for getting something close to full-matrix AdaGrad. Diagonal AdaGrad and Adam are used everywhere, but they ignore correlations between parameters. Full-matrix AdaGrad captures those correlations, but its preconditioner for a layer with mnmn weights is mn×mnmn \times mn, which is far too large for real models.

Shampoo's claim is that a layer's weights already have structure. A fully connected layer is an m×nm \times n matrix, and a convolution filter is a four-dimensional tensor. Keeping one matrix per dimension captures much of the useful correlation while staying cheap. The paper's experiments trained a 32-layer residual network on CIFAR-10, a 55-layer residual network on CIFAR-100, and an attention model on the LM1B language benchmark, all with Shampoo as a drop-in optimizer.

By late 2026 the paper had about 700 citations on OpenAlex.

The problem

Stochastic gradient descent (Stochastic Gradient Descent) uses the same step size in every direction. A parameter that receives large gradients and one that receives tiny gradients both move at the same scale unless the step size is tuned by hand.

Preconditioning fixes this by transforming the gradient before the step (Preconditioning). The ideal preconditioner is the matrix that gives each direction the right scale. Newton's method uses the Hessian (Hessian Matrix), which is too costly to form for deep networks. AdaGrad (AdaGrad) accumulates the sum of squared gradients over all past steps. Its full-matrix version uses the sum of outer products of gradients. That matrix captures correlations, but it has to be stored and inverted, with memory and computation that grow with the square of the number of weights.

The problem the paper solves is: how to keep most of the benefit of full-matrix AdaGrad while storing and inverting only matrices of the size of the layer's dimensions.

How it works

Take a single weight matrix WW of size m×nm \times n, for example a fully connected layer. Trace one training step with ϵ=1\epsilon = 1 as the initial value of the accumulators.

  1. Start with two accumulators. Keep LL, an m×mm \times m matrix, and RR, an n×nn \times n matrix. Both start as ϵ\epsilon times the identity. Check: they are small, m2+n2m^2 + n^2 numbers in total, not m2n2m^2 n^2.
  2. Compute the gradient. On a mini-batch, compute the gradient GG, an m×nm \times n matrix with one entry per weight. Check: GG has the same shape as WW.
  3. Update the accumulators. Add GGTG G^{\mathsf T} to LL, an m×mm \times m matrix that records how the rows of the gradient vary together. Add GTGG^{\mathsf T} G to RR, which does the same for the columns. Check: both are sums of outer products, so they stay symmetric and positive semidefinite.
  4. Precondition from both sides. Compute L−1/4L^{-1/4} and R−1/4R^{-1/4}, the inverse fourth roots, and form L−1/4GR−1/4L^{-1/4} G R^{-1/4}. The left factor mixes the rows of GG and the right factor mixes its columns. Check: the result has the same shape as GG.
  5. Take the step. Set W←W−η L−1/4GR−1/4W \leftarrow W - \eta\, L^{-1/4} G R^{-1/4}, with learning rate η\eta. Check: a direction with a long history of large gradients is shrunk on both sides, which is the AdaGrad effect in a structured form.

For a tensor with kk dimensions, there is one accumulator per dimension, and the exponent is −1/(2k)-1/(2k) instead of −1/4-1/4. For a convolution filter with four dimensions, each mode gets its own ni×nin_i \times n_i matrix raised to the power −1/8-1/8.

Two practical details from the paper. The inverse roots are only recomputed every 20 to 100 steps, which the authors report had almost no effect on accuracy and saved time. Momentum (Momentum), an average of past gradients with factor 0.9, is added to the step as in many other first-order methods.

The math

Definition: Shampoo update for a matrix

Given gradients G1,…,GTG_1, \ldots, G_T of an m×nm \times n weight matrix, the accumulators are

Lt=ϵIm+∑s=1tGsGsT,Rt=ϵIn+∑s=1tGsTGs,L_t = \epsilon I_m + \sum_{s=1}^{t} G_s G_s^{\mathsf T}, \qquad R_t = \epsilon I_n + \sum_{s=1}^{t} G_s^{\mathsf T} G_s,

and the update is

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}.

Here AαA^{\alpha} for a symmetric positive semidefinite matrix AA means the matrix with the same eigenvectors and each eigenvalue raised to the power α\alpha. ImI_m is the m×mm \times m identity matrix.

Step 3 in symbols: the accumulators

The term GsGsTG_s G_s^{\mathsf T} is an m×mm \times m matrix whose entry (i,j)(i, j) is the dot product of rows ii and jj of GsG_s. So LtL_t records how the rows of the gradient have moved together over time, and RtR_t does the same for the columns.

Step 4 in symbols: the two-sided preconditioner

The flattened gradient vec‾(G)\overline{\mathrm{vec}}(G) is an mnmn-vector. Using the Kronecker product identity (L⊗RT) vec‾(G)=vec‾(LGR)(L \otimes R^{\mathsf T})\,\overline{\mathrm{vec}}(G) = \overline{\mathrm{vec}}(LGR) from the paper's Lemma 4, the two-sided step is exactly a single preconditioned step on the flattened gradient with preconditioner

Ht=Lt1/4⊗Rt1/4.H_t = L_t^{1/4} \otimes R_t^{1/4}.

This is why the method is "full-matrix AdaGrad-like": the full preconditioner is never formed, but its action is computed through the two small matrices.

Step 5 in symbols: why the exponent is one quarter

The paper's Lemma 8 shows that the Kronecker product Lt1/2⊗Rt1/2L_t^{1/2} \otimes R_t^{1/2} is an upper bound on the full-matrix AdaGrad accumulator, up to the rank factor rr (for gradients of rank at most rr). The full AdaGrad step uses the square root of that accumulator, so HtH_t uses the fourth roots of LtL_t and RtR_t to match it. The paper also notes that the fourth root gives an overall step-size decay of order 1/t1/\sqrt{t}, a decay rate common in stochastic optimization methods.

The regret bound

Under the online convex optimization framework, Theorem 7 bounds the regret after TT rounds, for gradients of rank at most rr and a bounded distance DD from the comparison point W⋆W^\star:

∑t=1Tft(Wt)−∑t=1Tft(W⋆)≤2r D tr(LT1/4) tr(RT1/4).\sum_{t=1}^{T} f_t(W_t) - \sum_{t=1}^{T} f_t(W^\star) \le \sqrt{2r}\,D\,\mathrm{tr}\bigl(L_T^{1/4}\bigr)\,\mathrm{tr}\bigl(R_T^{1/4}\bigr).

Each trace grows like T1/4T^{1/4} under standard assumptions on the gradients, so the bound grows like T\sqrt{T}, the rate that the paper's bound achieves for convex online problems.

Worked examples

Example
One step on a 2 by 2 matrix

Take m=n=2m = n = 2, ϵ=1\epsilon = 1, η=1\eta = 1, and a gradient G=(1000)G = \begin{pmatrix} 1 & 0 \\ 0 & 0 \end{pmatrix}.

  1. Update the accumulators. L1=I+GGT=(2001)L_1 = I + GG^{\mathsf T} = \begin{pmatrix} 2 & 0 \\ 0 & 1 \end{pmatrix}, and R1=I+GTGR_1 = I + G^{\mathsf T}G equals the same matrix.
  2. Take the inverse fourth roots. The entry 2−1/4≈0.84092^{-1/4} \approx 0.8409 for the first diagonal entry of each, and 11 for the second.
  3. Multiply L1−1/4GR1−1/4L_1^{-1/4} G R_1^{-1/4}. The only nonzero entry is 0.8409×1×0.8409=0.70710.8409 \times 1 \times 0.8409 = 0.7071, which is 2−1/22^{-1/2}.
  4. Update. W2=W1−(0.7071000)W_2 = W_1 - \begin{pmatrix} 0.7071 & 0 \\ 0 & 0 \end{pmatrix}.

Interpretation: the gradient entry of 11 was shrunk to 0.7070.707 on its way into the weights. A step on a weight that had seen no gradient yet would have been larger.

Example
The same gradient twice

Continue the previous example with a second identical gradient, ϵ=1\epsilon = 1, and η=1\eta = 1.

  1. Update the accumulators. L2=I+2GGT=(3001)L_2 = I + 2GG^{\mathsf T} = \begin{pmatrix} 3 & 0 \\ 0 & 1 \end{pmatrix}, and R2R_2 is the same.
  2. Take the roots. 3−1/4≈0.75983^{-1/4} \approx 0.7598.
  3. Multiply. The first entry is 0.7598×1×0.7598=0.57740.7598 \times 1 \times 0.7598 = 0.5774, which is 3−1/23^{-1/2}.
  4. Update. The second step is 0.57740.5774 instead of 0.70710.7071.

Interpretation: as the same direction keeps receiving gradient, its step shrinks, following 1/count1/\sqrt{\text{count}}. This is the AdaGrad behaviour, carried out with two small matrices.

Example
Memory at the size of a real layer

Take a fully connected layer with m=n=2048m = n = 2048, so it has mn=4,194,304mn = 4{,}194{,}304 weights.

  1. Full-matrix AdaGrad needs a preconditioner of size mn×mnmn \times mn, which has (mn)2=(4,194,304)2≈1.76×1013(mn)^2 = (4{,}194{,}304)^2 \approx 1.76 \times 10^{13} entries.
  2. Shampoo needs LL and RR, with m2+n2=2×20482=8,388,608m^2 + n^2 = 2 \times 2048^2 = 8{,}388{,}608 entries in total.
  3. The ratio is about 2×1062 \times 10^{6} to one in favour of Shampoo.

Try it: a layer with m=4096m = 4096 and n=512n = 512 has mn=2,097,152mn = 2{,}097{,}152 weights. Compute m2+n2m^2 + n^2 and state whether it is smaller than the mnmn weights themselves. Answer: m2+n2=16,777,216+262,144=17,039,360m^2 + n^2 = 16{,}777{,}216 + 262{,}144 = 17{,}039{,}360, which is about eight times the 2,097,1522{,}097{,}152 weights. For this shape Shampoo's accumulators are larger than the layer, so the paper's diagonal fallback for oversized dimensions matters in practice.

Results

The paper trained with mini-batches of 128, and for CIFAR it tuned each optimizer over 10 learning rates between 0.01 and 10.0 (scaled for Adam). Reported results:

  • CIFAR-10, 32-layer residual network (2.4M parameters): error 5 percent on the test set. Figure 2 shows the training loss.
  • CIFAR-10, 20-layer small Inception network (1.65M parameters): error 7.5 percent.
  • CIFAR-100, 55-layer residual network without batch normalization (13.5M parameters): error 24 percent. Its largest layer is a (256,3,3,256)(256, 3, 3, 256) tensor, which is four-dimensional.
  • LM1B, Attention model (9.8M parameters): the paper reports test log-perplexity curves in Figure 4. Shampoo used its default learning rate of 1.0, and the other optimizers were run at several learning rates.

Speed was measured in steps per second on a Tesla K40 GPU (Table 1):

DatasetSGDAdamAdaGradShampoo
CIFAR-10 (ResNet-32)2.1842.1842.1972.151
CIFAR-10 (Inception)3.6383.6673.6823.506
CIFAR-100 (ResNet-55)1.2101.2031.2101.249
LM1B (Attention)4.9194.8714.9083.509

What this means: Shampoo does more work per step, yet on the two CIFAR-10 models it is only about 2 to 5 percent slower in steps per second than the others. On CIFAR-100 it is the fastest of the four. On the language model it completes about 30 percent fewer steps per second than the others, a bigger gap than the other settings.

The limits matter. The convergence guarantee is for convex problems, while the neural network experiments are not convex. The speed table counts steps per second on one GPU type, not wall-clock time to a target accuracy, so it does not show which optimizer reaches a given accuracy first. The method also changes its behaviour for very large dimensions, where it falls back to a diagonal preconditioner.

Reading guide

Read the paper in this order:

  • Abstract and Section 1. Needs AdaGrad and Preconditioning. Figure 1 shows the three-dimensional tensor case.
  • Section 1.1, the matrix case. Needs Tensor Product for the Kronecker product, and the update in Algorithm 1 matches the walkthrough above.
  • Section 2. Needs Singular Value Decomposition (SVD) for the matrix powers, Matrix Square Root for the square-root identity, and the online convex optimization framework. The regret definition is in Section 2.1.
  • Section 3, the matrix analysis. Uses the Kronecker identities of Lemmas 3 and 4 from Section 2.3. Lemma 8 is the key step; skim its proof on first reading.
  • Section 4, the tensor algorithm. Needs the same ideas applied to each mode.
  • Section 5, implementation. Plain engineering: the diagonal fallback for dimensions above about 1200.
  • Section 6, experiments. Needs Convolutional Neural Network for the CIFAR models and Learning Rate for the tuning protocol.

Check yourself

QuestionShampoo keeps an m×mm \times m matrix LL and an n×nn \times n matrix RR. For a weight matrix with m=10m = 10 and n=20n = 20, how many accumulator entries does it store, and how many would full-matrix AdaGrad need for the same layer?Show answer

Shampoo stores m2+n2=100+400=500m^2 + n^2 = 100 + 400 = 500 entries. Full-matrix AdaGrad flattens the 200200 weights, so its preconditioner has 2002=40,000200^2 = 40{,}000 entries.

QuestionWith ϵ=1\epsilon = 1, a gradient G=(2000)G = \begin{pmatrix} 2 & 0 \\ 0 & 0 \end{pmatrix} and η=1\eta = 1, compute the first entry of the step L1−1/4GR1−1/4L_1^{-1/4} G R_1^{-1/4}, to three decimals.Show answer

L1=I+GGT=diag(5,1)L_1 = I + GG^{\mathsf T} = \mathrm{diag}(5, 1), and R1R_1 is the same. The first entry of L1−1/4L_1^{-1/4} is 5−1/4≈0.66875^{-1/4} \approx 0.6687. The step's first entry is 0.6687×2×0.6687=0.89440.6687 \times 2 \times 0.6687 = 0.8944. This equals 2/52 / \sqrt{5}, to three decimals 0.8940.894.

QuestionWhy does Shampoo use the fourth root of each accumulator instead of the square root, as AdaGrad does?Show answer

The product L1/4⊗R1/4L^{1/4} \otimes R^{1/4} is the preconditioner, and the Kronecker product of the two square roots, L1/2⊗R1/2L^{1/2} \otimes R^{1/2}, is what matches the full-matrix AdaGrad accumulator in the paper's Lemma 8. Splitting the power equally between the two sides gives 1/41/4 each. It also gives the overall step-size decay of order 1/t1/\sqrt{t} needed for the T\sqrt{T} regret bound.

QuestionA team wants to use Shampoo on a 4-dimensional convolution tensor of size 3×3×64×1283 \times 3 \times 64 \times 128. Which exponent does each mode's accumulator use, and what does the paper's diagonal fallback do?Show answer

For an order-kk tensor with k=4k = 4, the exponent is −1/(2k)=−1/8-1/(2k) = -1/8 on each mode's matrix. The diagonal fallback applies when one dimension is too large for its matrix to be stored or for its SVD to be computed, as set by a threshold of about 1200 in the paper. That dimension then uses a diagonal preconditioner, and the other dimensions still use full matrices.

Sources

Read the full paper

Vineet Gupta, Tomer Koren, Yoram Singer, 2018. arXiv:1802.09568