SAM as an Optimal Relaxation of Bayes

Thomas Möllenhoff, Mohammad Emtiyaz Khan

This paper, presented at ICLR 2023, provides a rigorous mathematical bridge between two popular but historically separate approaches to training robust deep learning models: Sharpness-Aware Minimization (SAM) and Bayesian Deep Learning.

The Challenge of Overfitting and Flat Minima

Deep learning models are notorious for "overfitting"—performing perfectly on training data but failing on new examples. To combat this, researchers often look for flat minima: regions in the weight space where the loss function doesn't change much if you wiggle the parameters.

Two main camps have emerged to find these regions:

  1. Adversarial (SAM): Minimizes the maximum loss within a small neighborhood (worst-case scenario).
  2. Bayesian: Minimizes the expected loss over a distribution (average-case scenario).

While both aim for "flatness," they use different mathematical machinery. SAM is prized for its state-of-the-art accuracy, while Bayesian methods are valued for "knowing what they don't know" (uncertainty estimation). The gap between "max-loss" and "expected-loss" has made it difficult to combine their strengths—until now.

Bridging the Gap with Fenchel Duality

The authors bridge this gap using Fenchel biconjugates. In convex analysis, a biconjugate provides the "tightest possible convex lower bound" for a function.

The Key Theoretical Move: The authors show that if you take the Bayesian "expected negative-loss" and replace it with its Fenchel biconjugate, the "max-loss" used in SAM naturally emerges. This proves that SAM is essentially a simplified, "relaxed" version of a Bayesian objective where the variance is kept fixed.

SAM vs Bayes and the smoothing effect of variance

The Algorithm: bSAM (Bayesian SAM) Building on this connection, they develop bSAM. Unlike standard SAM, which uses a fixed perturbation radius ($\rho$), bSAM treats this radius as a learnable variance parameter. It uses the Bayesian Learning Rule (BLR) to update both the model weights and the "precision" (inverse variance) of those weights.

  • Standard SAM: Uses a scalar $\rho$ to perturb all weights equally.
  • bSAM: Uses a vector $s$ to adapt the perturbation for every single parameter, similar to how Adam scales learning rates.

Experimental Setup

The authors evaluated bSAM against baselines like SGD, Adam, SWAG, and standard SAM variants across several benchmarks:

  • Toy Problems: 2D Logistic Regression and "Two Moons" (for visualizing uncertainty).
  • Image Classification: MNIST, FashionMNIST, CIFAR-10, CIFAR-100, and TinyImageNet.
  • Architectures: Multi-Layer Perceptrons (MLP), LeNet-5, ResNet-20, and ResNet-18.

Key Insights and Results

  • Duality is the Bridge: The "max-loss" of adversarial training and the "expected-loss" of Bayesian training are two sides of the same coin, linked by convex duality.
  • Automatic Hyperparameter Tuning: One of the biggest headaches in SAM is choosing the right $\rho$. Because bSAM is Bayesian, it can "learn" the optimal neighborhood size during training.
  • Smoothing Effect: Higher variance (larger neighborhoods) "fills in" sharp, narrow minima, forcing the optimizer to stay in broad, flat valleys that generalize better.
  • Uncertainty for Free: By treating the optimizer as a posterior distribution sampler, bSAM provides reliable uncertainty estimates (calibration) without the massive computational overhead usually associated with Bayesian methods.
  • Superior Generalization: On CIFAR-100 (without data augmentation), bSAM achieved 62.64% accuracy, significantly beating SAM-Adam (53.25%) and VOGN (59.83%).
  • Better Calibration: bSAM consistently produced the lowest Expected Calibration Error (ECE). In toy examples, it matched the predictive uncertainty of "Exact Bayes," whereas standard SAM was often overconfident.
  • Robustness to Noise: The authors found that adding a small amount of Gaussian noise to the weights during the SAM perturbation step (a "noisy linearization") consistently improved performance across all metrics.

Limitations and Future Work

  • Diagonal Assumption: To keep the algorithm fast (Adam-like speed), the authors assumed a diagonal covariance matrix. While efficient, this ignores correlations between different parameters.
  • Theoretical Constraints: The proof relies on the loss function being "majorized by a quadratic," a common assumption in optimization theory that might not strictly hold for every point in a complex neural network landscape.
  • The "Reverse" Path: The authors suggest that while they used Bayes to improve SAM, future work could go the other way—using SAM's adversarial tricks to make Bayesian deep learning more robust and easier to scale.

Summary updated

BibTeX
@inproceedings{mollenhoff2023sam,
  author = {Thomas Möllenhoff and Mohammad Emtiyaz Khan},
  title = {{SAM as an Optimal Relaxation of Bayes}},
  booktitle = {International Conference on Learning Representations},
  year = {2023},
  url = {https://openreview.net/forum?id=k4fevFqSQcX},
}
Figure