Skip to content

Sharpness-aware minimisation

How SAM probes a weight neighbourhood before updating, what its first-order approximation omits, and why flatness alone does not establish generalisation.

Sharpness-aware minimisation (SAM) is a training method that seeks model weights with low loss throughout a small surrounding neighbourhood. It probes an uphill direction in weight space, then uses the gradient at that perturbed point for the learning update. For a quant fitting a neural return or volatility model, this changes the training objective and computational budget; an improvement still has to survive chronological validation.

The neighbourhood is part of the objective

For empirical loss L(w)L(w) and radius ρ\rho, the core objective is
min⁡wFρ(w),Fρ(w)=max⁡∥ϵ∥2≤ρL(w+ϵ).\min_w F_\rho(w), \qquad F_\rho(w)=\max_{\lVert\epsilon\rVert_2\le\rho}L(w+\epsilon).
The Euclidean ball constrains the joint length of the weight perturbation. It does not give every weight an independent allowance of ρ\rho. The increase Fρ(w)−L(w)F_\rho(w)-L(w) measures local sharpness under this chosen norm and radius. Minimising FρF_\rho balances low loss with insensitivity to nearby weight changes; minimising sharpness alone could favour a flat region with high loss. A weight penalty may be added separately, as in Foret et al.'s formulation.

The practical step uses two gradients

For one mini-batch loss LBL_B, the usual first-order, Euclidean SAM step is

g=∇LB(w),ϵ^=ρg∥g∥2,g~=∇uLB(u)∣u=w+ϵ^,wnext=w−ηg~.\begin{aligned} g &= \nabla L_B(w), \\ \hat\epsilon &= \rho\frac{g}{\lVert g\rVert_2}, \\ \tilde g &= \left.\nabla_u L_B(u)\right|_{u=w+\hat\epsilon}, \\ w_{\mathrm{next}} &= w-\eta\tilde g. \end{aligned}
Here g≠0g\ne0 and η\eta is the learning rate; the last line uses plain gradient descent. Another base optimiser, such as Adam, can consume g~\tilde g instead.
Two approximations matter. The normalised gradient maximises a linearised loss, not the general nonlinear inner problem. The second gradient treats ϵ^\hat\epsilon as fixed: it does not differentiate through the calculation of the perturbation. This is the stop-gradient, first-order procedure in the paper's Equation 3 and Algorithm 1. Differentiating through the approximate perturbation gives a different procedure with second-order terms.
The basic version uses the same mini-batch twice. Restore the original weights before applying the optimiser step. Its two forward/backward evaluations cost more than an ordinary update; elapsed time need not be exactly doubled. The authors' research repository provides the original experimental setting and code, rather than evidence about financial prediction.

A probe is not a permanent uphill update

Take the synthetic scalar loss L(w)=w2/2L(w)=w^2/2, with current weight w=2w=2, radius ρ=0.1\rho=0.1, learning rate η=0.1\eta=0.1 and no weight decay.

OperationResult
Gradient at the current weightg=2g=2
Normalised uphill perturbationϵ^=0.1\hat\epsilon=0.1
Probe weight and loss2.12.1 and 2.2052.205
Gradient measured at the probeg~=2.1\tilde g=2.1
Restore, then update2−0.1(2.1)=1.792-0.1(2.1)=1.79
Ordinary SGD from the same weight2−0.1(2)=1.802-0.1(2)=1.80
Synthetic quadratic loss: from w = 2, probe the radius-0.1 neighbourhood at w = 2.1, where loss is 2.205 and gradient is 2.1. Restore w = 2 before the update to 1.79; ordinary SGD would give 1.80.
Synthetic quadratic loss: from w = 2, probe the radius-0.1 neighbourhood at w = 2.1, where loss is 2.205 and gradient is 2.1. Restore w = 2 before the update to 1.79; ordinary SGD would give 1.80.
The uphill probe chooses where to measure the gradient. The learning update starts from the original weight, not the perturbed weight. Synthetic scalar example; no weight decay.

Starting the final update from 2.12.1 would instead give 1.891.89, which is not this SAM step. The probe temporarily increases the loss so the algorithm can measure a gradient there.

For this quadratic at positive ww, the exact neighbourhood maximum is at w+ρw+\rho, so the approximate probe happens to find it. That coincidence is not a general property of neural-network losses. Even this quadratic exposes a limit at w=0w=0: the current gradient vanishes, yet the radius-0.10.1 neighbourhood contains loss 0.0050.005. The normalisation formula is then undefined. Choosing a zero perturbation handles the division but misses that increase; it does not solve the inner maximisation.

Flatness needs a coordinate system—and a test

A flat-looking minimum is not a certificate of generalisation. Dinh et al. show that rescaling layers in rectified networks can preserve the prediction function while changing common sharpness measures. A fixed Euclidean radius therefore has meaning only relative to the parameterisation and scale. SAM's weight-space neighbourhood is also different from perturbing input features or market scenarios.

When comparing a financial model trained with SAM against its base optimiser, hold the data construction and chronological splits fixed, tune on validation, and record the radius, learning rate, weight-decay convention, seeds and compute budget. Report both predictive performance and training cost. Lower validation error alone does not establish that flatness caused the improvement; matching training losses does not establish that mechanism either. Weight robustness cannot repair future-data leakage or guarantee stability under a market-regime change.

Reading Loss Curves develops experiment logging, validation-based selection and training diagnosis. Its preview is public; the full lesson requires membership.

Sources

Last updated
  • machine learning
  • optimisation
  • regularisation
  • sam
← Back to the glossaryBack to top ↑