Search

Search papers and lessons

Sharpness-Aware Minimization for Efficiently Improving Generalization

Pierre Foret, Ariel Kleiner, Hossein Mobahi et al. · 2020

SAM trains a model to reach parameters whose whole neighbourhood has low loss, not just a low loss at one point, and it improves test accuracy with two gradient computations per step.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Linear algebra
  • Discrete math
  • Learning basics
  • Optimizers
  • ReadReady to readNot yet

TL;DR

Training a model usually means finding weights that give a low loss on the training set. A model can reach a low training loss in many places, and some of those places generalize much worse than others. Sharpness-Aware Minimization (SAM) changes the target: it looks for weights whose whole small neighbourhood has low loss, so the loss does not climb steeply when the weights move a little. Each SAM step needs two gradient computations, one to find the worst nearby point and one to take the step from there. On image benchmarks the paper reports lower test error than standard training with the same models.

Builds on: Stochastic Gradient Descent, Empirical Risk Minimization, L2 Regularization, Taylor Series · Leads to: later work that treats the flatness of a minimum as a training target

Why it mattered

SAM is a drop-in change to the optimizer. The paper replaces the usual update with its own and keeps the model, data and learning-rate schedule. On CIFAR-10 with a WideResNet-28-10 trained for 200 epochs and basic augmentation, the paper reports 2.7 percent test error with SAM against 3.5 percent with standard SGD. On ImageNet, a ResNet-152 trained for 200 epochs reaches 18.5 percent top-1 error with SAM against 20.3 percent without it.

By late 2026 it had about 2,200 citations on OpenAlex. Its lasting contribution is the idea that a training loss can be shaped to care about curvature, at a cost cheap enough to use on large models.

The idea of flat minima is older. Hochreiter and Schmidhuber argued in the mid-1990s that flat minima generalize better. For two decades the idea was hard to use, because the sharpness measures were expensive and awkward to differentiate. In 2016 Keskar and colleagues reported that large-batch training tended to find sharper minima and generalized worse, which made sharpness a practical concern again. The SAM paper was posted to arXiv in October 2020 and gave an update rule that works with ordinary optimizers and scales to ImageNet-sized models.

The problem

A deep network has millions of weights, and its training loss is a non-convex surface. That surface has many minima. Some sit in wide, flat valleys and some in narrow, sharp ones, and several can have nearly the same training loss. The paper says these minima can differ a lot in test performance even when their training losses match.

Standard training minimizes the training loss LSL_S and nothing else. Earlier work suggested that sharp minima generalize worse, but the methods that tried to find flat minima were hard to compute and differentiate through. The paper asks for a method that is cheap, scales to large models, and improves test accuracy on modern networks.

Two prerequisites matter here. The training loss LSL_S is the average of a per-example Loss Function, and training follows its Gradient. The method also needs a small change of the weights measured with a length, so it uses an L2 norm.

How it works

Take a tiny model with one weight ww and a training loss that is a simple parabola, LS(w)=w2L_S(w) = w^2. The gradient is 2w2w. Start at w=3w = 3.

  1. Measure the slope where you are. The gradient at w=3w = 3 is g=6g = 6. This is the step that standard Gradient Descent would take. Check: the loss here is 99, and it rises as ww moves away from 00.
  2. Find the worst nearby point. SAM asks: if I move the weight by at most a fixed distance ρ=0.5\rho = 0.5, in the direction that raises the loss the most, where do I end up? For one weight the answer is to step along the gradient by the full distance: ϵ^=ρ⋅g/∣g∣=0.5\hat\epsilon = \rho \cdot g / |g| = 0.5. The perturbed weight is w+ϵ^=3.5w + \hat\epsilon = 3.5. Check: the loss there is 12.2512.25, up from 99.
  3. Measure the slope at that worse point. The gradient at 3.53.5 is gSAM=7g_{\text{SAM}} = 7. This is the slope that matters. A sharp valley gives a steep slope here; a flat one would not.
  4. Update the original weight with that slope. With learning rate η=0.1\eta = 0.1, the SAM update is wnew=3−0.1⋅7=2.3w_{\text{new}} = 3 - 0.1 \cdot 7 = 2.3. Plain SGD would have gone to 3−0.1⋅6=2.43 - 0.1 \cdot 6 = 2.4. The tiny example is enough to see the rule: the step is taken from ww, but its size comes from the slope at the perturbed point.

The diagram shows the two passes that make up one SAM update.

flowchart LR
  A[weights w] --> B[gradient g at w]
  B --> C[perturb: w + rho times g over its length]
  C --> D[gradient at the perturbed point]
  D --> E[SGD step from w using that gradient]
  E --> A

The plot shows the same idea in one dimension: both curves have their minimum at 00, but the sharp one gains much more loss over the same step, so SAM is pushed toward the flat one.

−2−1.5−1−0.500.511.5201234weighttraining loss
  • sharp minimum
  • flat minimum
A step of size 0.5 from the minimum raises the sharp loss to 1 and the flat loss to 0.125.

The math

The paper writes the training objective as a sum over the training set, LS(w)=1n∑i=1nℓ(w,xi,yi)L_S(\mathbf{w}) = \frac{1}{n}\sum_{i=1}^{n} \ell(\mathbf{w}, \mathbf{x}_i, y_i), where w\mathbf{w} is the vector of weights and ℓ\ell is the loss on one example.

Step 2 in symbols: the worst point in a ball

SAM replaces LS(w)L_S(\mathbf{w}) with the worst training loss inside a ball of radius ρ\rho around w\mathbf{w}:

LSSAM(w)=max⁡∥ϵ∥2≤ρLS(w+ϵ),L^{\text{SAM}}_S(\mathbf{w}) = \max_{\|\boldsymbol{\epsilon}\|_2 \le \rho} L_S(\mathbf{w} + \boldsymbol{\epsilon}),

and the paper minimizes LSSAM(w)+λ∥w∥22L^{\text{SAM}}_S(\mathbf{w}) + \lambda \|\mathbf{w}\|_2^2. The second term is ordinary L2 Regularization with strength λ\lambda.

To find the inner maximum, the paper takes a first-order Taylor expansion of LSL_S around w\mathbf{w}: LS(w+ϵ)≈LS(w)+ϵT∇LS(w)L_S(\mathbf{w} + \boldsymbol{\epsilon}) \approx L_S(\mathbf{w}) + \boldsymbol{\epsilon}^T \nabla L_S(\mathbf{w}). The maximum over the ball is then attained by the vector of length ρ\rho that points along the gradient:

ϵ^(w)=ρ ∇LS(w)∥∇LS(w)∥2.\hat{\boldsymbol{\epsilon}}(\mathbf{w}) = \rho \, \frac{\nabla L_S(\mathbf{w})}{\|\nabla L_S(\mathbf{w})\|_2}.

This is the step 2 formula in vector form. Each component of the gradient is scaled by the same factor, so the direction is kept and the length becomes ρ\rho.

Step 3 in symbols: the gradient used for the update

Differentiating the objective through ϵ^\hat{\boldsymbol{\epsilon}} would require second derivatives. The paper drops those terms and keeps only the gradient at the perturbed point:

∇wLSSAM(w)≈∇LS(w+ϵ^(w)).\nabla_{\mathbf{w}} L^{\text{SAM}}_S(\mathbf{w}) \approx \nabla L_S(\mathbf{w} + \hat{\boldsymbol{\epsilon}}(\mathbf{w})).

The update then uses this vector as the gradient in ordinary SGD: w←w−η ∇LS(w+ϵ^(w))\mathbf{w} \leftarrow \mathbf{w} - \eta \, \nabla L_S(\mathbf{w} + \hat{\boldsymbol{\epsilon}}(\mathbf{w})).

In practice the paper uses minibatches. For each batch it computes ϵ^\hat{\boldsymbol{\epsilon}} on that batch, then the gradient at the perturbed weights on the same batch. Each update therefore costs two forward and backward passes. Let tt be the cost of one such pass: SGD costs tt per step and SAM costs 2t2t. The paper compares the two methods at equal epoch counts, so its reported gains do not net out this extra cost.

Step by step with two weights

Take w=(1,2)\mathbf{w} = (1, 2), ρ=0.5\rho = 0.5, η=0.1\eta = 0.1. Suppose the gradient at w\mathbf{w} is g=(3,4)\mathbf{g} = (3, 4) and the gradient at the perturbed point is (3.6,5.2)(3.6, 5.2).

  1. The length of g\mathbf{g} is 32+42=5\sqrt{3^2 + 4^2} = 5.
  2. The perturbation is ϵ^=0.5⋅(3,4)/5=(0.3,0.4)\hat{\boldsymbol{\epsilon}} = 0.5 \cdot (3, 4) / 5 = (0.3, 0.4).
  3. Its length is 0.32+0.42=0.5\sqrt{0.3^2 + 0.4^2} = 0.5, which is ρ\rho, as required.
  4. The perturbed weights are (1.3,2.4)(1.3, 2.4).
  5. The SAM update is (1,2)−0.1⋅(3.6,5.2)=(0.64,1.48)(1, 2) - 0.1 \cdot (3.6, 5.2) = (0.64, 1.48).
  6. Interpretation: the second component moved by 0.520.52 instead of 0.40.4, because the slope at the worse point was steeper. The step responds to the neighbourhood, not only to the current point.

Try it: with ρ=0.5\rho = 0.5 and g=(6,8)\mathbf{g} = (6, 8), what is ϵ^\hat{\boldsymbol{\epsilon}}? Answer: the length of g\mathbf{g} is 10, so ϵ^=0.5⋅(6,8)/10=(0.3,0.4)\hat{\boldsymbol{\epsilon}} = 0.5 \cdot (6, 8) / 10 = (0.3, 0.4).

Results

The main results use the paper's own tables and numbers.

  • CIFAR-10, WideResNet-28-10, 200 epochs, basic augmentation: SAM 2.7 percent error, SGD 3.5 percent. The absolute gain is 0.80.8 points, a relative reduction of 0.8/3.5≈0.230.8 / 3.5 \approx 0.23, about 23 percent.
  • CIFAR-100, the same model and setting: SAM 16.5 percent error, SGD 18.8 percent, a gain of 2.32.3 points.
  • CIFAR-10, WideResNet-28-10, 1800 epochs, AutoAugment: SAM 1.6 percent, SGD 2.2 percent.
  • ImageNet, ResNet-152: top-1 error at 200 epochs is 18.5 percent with SAM and 20.3 percent without; at 400 epochs it is 18.4 and 20.9.- Finetuning: SAM improves finetuned EfficientNet models on the paper's downstream tasks, and reports 0.30 percent error on CIFAR-10 and 11.39 percent on ImageNet in its finetuning table (Table 3).
  • Label noise, CIFAR-10 with 80 percent of labels flipped: test accuracy is 77.9 percent for SAM and 26.2 percent for SGD. At 20 percent noise the values are 95.1 and 84.8.
  • Hessian spectra (the paper's Figure 3): the largest eigenvalue at convergence is about 24 without SAM and about 1.0 with SAM. The ratio λmax⁡/λ5\lambda_{\max} / \lambda_5 is up to 11.4 without SAM and 2.6 with it, so the ratio is about 11.4/2.6≈4.411.4 / 2.6 \approx 4.4 times smaller.

The limits are as important as the gains. The results are for image classification and finetuning on the benchmarks above, and the paper's own comparisons use its choice of models and augmentations. SAM costs two gradient computations per update, so it is not free. Its radius ρ\rho is a hyperparameter; the paper tunes it on a grid from 0.01 to 0.5 using a validation split, and reports 0.05 as a solid default. The paper also finds that the sharpness measure correlates better with generalization when computed over small groups of examples (mm-sharpness) than over the whole training set, which shows that flatness is useful but not a law. It is tempting to think SAM simply finds a minimum of the training loss faster; in fact it descends the worst loss in a small ball, so each update needs a second gradient at a point that the step does not start from.

Reading guide

  • Section 1, Introduction. Needs Overfitting and the idea that flatness was linked to generalization. Read Figure 1 first.
  • Section 2, the method. Needs Gradient, Taylor Series and Vector Norm. The derivation of ϵ^\hat{\boldsymbol{\epsilon}} is the step 2 walkthrough above. The second-order terms that the paper drops are discussed in its appendix.
  • Section 3, empirical evaluation. Needs Data Augmentation and Label Smoothing. The tables are the results above; the label-noise comparison uses Mixup as a baseline.
  • Section 4, sharpness and generalization. Needs Hessian Matrix and Eigenvalues and Eigenvectors for the spectra, and the mm-sharpness definition, which is a per-group version of the same quantity.
  • Section 5, related work. Needs Dropout and Adversarial Training, which the paper compares against as other ways to regularize.
  • Appendix A, PAC-Bayes bound. Optional. It needs the worst-case idea from Minimax Game and a formula for the KL divergence between Gaussians; skip it on a first read.

Check yourself

QuestionIn one sentence, what does SAM minimize instead of the plain training loss at the current weights?Show answer
It minimizes the worst training loss within a ball of radius rho around the current weights, the maximum of the loss over a small neighbourhood rather than its value at one point.
QuestionFor the one-weight example L(w) = w^2 at w = 3 with rho = 0.5, what perturbed weight does SAM use, and why is the gradient there larger than 6?Show answer
The perturbed weight is 3.5, found by stepping along the gradient by rho. The gradient there is 2 times 3.5 = 7, which is larger than 6 because the loss is steeper farther from zero.
QuestionA method needs two gradient computations per update. If an SGD run takes 100 epochs, roughly how many epochs of SAM would spend about the same compute, and how does the paper compare the two?Show answer
About 50 epochs of SAM would match the compute of 100 SGD epochs. The paper compares the two at equal epoch counts (for example 200 epochs each), so its gains are not measured at equal compute.
QuestionWhy might a model trained with SAM have a lower top Hessian eigenvalue than one trained with SGD, and what does the paper report?Show answer
SAM steers the weights toward regions where the loss stays low in a neighbourhood, which are flatter with smaller curvature. The paper reports the largest eigenvalue at convergence as about 24 without SAM and about 1.0 with SAM.
QuestionA team sees that SAM improved test accuracy on its dataset but its training is now twice as slow. What trade are they making, and when is it worth it?Show answer
They trade extra compute per update, since SAM computes two gradients, for better generalization. It is worth it when the model overfits and extra compute is cheap relative to the accuracy gain, and when the team can tune rho. If the baseline is already well regularized and compute is tight, the gain may not justify it.

Sources

  • Foret, Kleiner, Mobahi, Neyshabur, "Sharpness-Aware Minimization for Efficiently Improving Generalization," arXiv:2010.01412 (2020). https://arxiv.org/abs/2010.01412

Read the full paper

Pierre Foret, Ariel Kleiner, Hossein Mobahi et al., 2020. arXiv:2010.01412