SAM as an Optimal Relaxation of Bayes

Thomas Möllenhoff, Mohammad Emtiyaz Khan

TL;DR

This paper establishes a formal mathematical bridge between adversarial deep learning and Bayesian inference by showing that Sharpness-Aware Minimization (SAM) is an optimal convex relaxation of the classic Bayes objective. Leveraging this connection, the authors introduce Bayesian-SAM (bSAM), an Adam-like algorithm that inherits SAM's superior generalization capabilities while automatically providing high-quality uncertainty estimates.

What Problem Does This Paper Address?

Deep learning models often suffer from overfitting or overconfidence. To combat this, two prominent but historically separate paradigms have emerged:

  1. Sharpness-Aware Minimization (SAM): An adversarial training method that searches for "flat minima" by minimizing the maximum loss within a small neighborhood around the model parameters. SAM drastically improves generalization but operates purely as a point-estimate method, offering no inherent way to quantify prediction uncertainty.
  2. Bayesian Deep Learning: A probabilistic approach that computes an expected loss over a posterior distribution of parameters. While excellent for uncertainty estimation, Bayesian methods can be notoriously difficult to optimize effectively for large-scale deep networks.

The fundamental disconnect lies in their core objectives: SAM uses a max-loss strategy, whereas Bayes uses an expected-loss strategy. This paper addresses this gap by asking: Is there a deeper mathematical relationship between maximizing loss in a neighborhood (SAM) and taking expectations over a distribution (Bayes)?

Methods

The authors bridge the gap using convex duality, specifically the Fenchel biconjugate (which finds the tightest convex lower bound of a function).

They prove that if you take the standard Bayes objective (using an isotropic Gaussian posterior) and replace the expected negative loss with its optimal convex relaxation via the Fenchel biconjugate, the adversarial "max-loss" mathematical structure of SAM naturally emerges.

Figure 1: Panel (a) highlights the main difference between SAM and Bayes. SAM uses max-loss (the red dot), while Bayes uses expected-loss (the blue dot shows ℓ(θ + ϵ) for an ϵ ∼N(ϵ | 0, σ2)). Our main result in Theorem 2 connects the two by using the optimal convex relaxation (Fenchel biconjugate) of the negative expected loss. It shows that the role of ρ and σ are exactly the same, and a SAM minimizer obtained for a fixed ρ can always be recovered from the relaxation for some σ. An example is shown in Panel (b) for a loss (gray line) with 3 local minima indicated by A, B, and C. The expected loss is smoother (blue line) but the relaxation, which upper bounds it, is even more smooth (red line). Higher σ give smoother objectives where sharp minima A and C slowly disappear. The SAM minimizer is shown with a red star which matches the minimizer of the relaxation.

Through this framework, the authors show that:

  • Standard SAM is mathematically equivalent to optimizing the mean of a Gaussian posterior while keeping its variance fixed.
  • The perturbation radius $\rho$ in SAM plays the exact same role as the variance $\sigma^2$ in Bayesian methods.

Using the Bayesian Learning Rule (BLR), the authors extend this relaxed objective to dynamically learn the variance alongside the mean. This results in Bayesian-SAM (bSAM), a practical, Adam-like algorithm where the hyperparameter $\rho$ is automatically adjusted for each parameter dimension via a cheaply maintained variance vector.

Datasets

The authors evaluated their approach across several standard image classification benchmarks:

  • MNIST and FashionMNIST (using MLP and LeNet-5 architectures)
  • CIFAR-10 and CIFAR-100 (using ResNet-20-FRN and ResNet-18 architectures)
  • TinyImageNet (using ResNet-20-FRN)

Key Insights and Ideas

  • Max-Loss as a Relaxed Expectation: The "aha moment" of the paper is that SAM's adversarial max-loss isn't just a heuristic; it is the mathematically optimal convex relaxation of a Bayesian expectation.
  • Variance as a Smoother: Increasing the variance $\sigma^2$ in the relaxed Bayes objective acts as a smoothing mechanism. As variance increases, sharp local minima disappear, naturally biasing the optimizer toward flatter, more robust regions of the loss landscape.
  • Hyperparameter Autotuning: In standard SAM, choosing the perturbation radius $\rho$ requires tedious grid searching. In bSAM, because $\rho$ maps directly to variance, the algorithm can dynamically learn parameter-wise perturbation strengths during training.

Key Results

The authors compared bSAM against standard deep learning baselines (SGD, Adam), Bayesian methods (SWAG, VOGN), and standard SAM variants (SAM-SGD, SAM-Adam).

  • Uncertainty Quantification: On a 2D toy logistic regression problem, bSAM generated predictive uncertainty boundaries remarkably close to the exact, mathematically intractable Bayesian posterior. Standard SAM point estimates were highly overconfident, while Laplace approximations over-corrected into underconfidence.
Predictive Uncertainties on Toy Example
  • Performance on Real Datasets: In large-scale experiments (both with and without data augmentation), bSAM consistently achieved either comparable or superior test accuracy alongside significantly lower Expected Calibration Error (ECE) and Negative Log-Likelihood (NLL) than standard SAM and Bayesian baselines. For example, on CIFAR-100 without data augmentation, bSAM achieved 62.64% accuracy compared to SAM-SGD's 58.58%.
  • Robustness to Hyperparameters: bSAM proved significantly less sensitive to the initial choice of the neighborhood size $\rho$ compared to standard SAM variants, thanks to its ability to adapt variances dynamically.

Limitations and Future Work

  • The "m-sharpness" Gap: Empirically, splitting minibatches across multiple compute nodes and calculating independent perturbations (a technique called m-sharpness) boosts performance for both SAM and bSAM. However, the authors note that this phenomenon actually loosens the theoretical lower bound derived in their paper, leaving an interesting gap between current theory and practice for future work to resolve.
  • One-Way Street: While this work successfully uses Bayesian principles to improve SAM, the authors note that borrowing algorithmic tricks from SAM to improve traditional, non-relaxed Bayesian deep learning remains an open and promising direction.

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