Random Sharpness-Aware Minimization

Yong Liu, Siqi Mai, Minhao Cheng, Xiangning Chen, Cho-Jui Hsieh, Yang YouNeurIPS 2022

TL;DR

Sharpness-Aware Minimization (SAM) improves model generalization by seeking "flat" minima, but its internal search for these regions is often hampered by unstable gradients. R-SAM introduces a simple yet effective random smoothing step that stabilizes these gradients, leading to better optimization and superior performance on ResNets and Vision Transformers without increasing computational overhead.

The Challenge of Finding Flat Minima

In deep learning, the ultimate goal is not just to perform well on training data, but to generalize effectively to unseen data. Research suggests that models residing in "flat" regions of the loss landscape—where small changes to weights don't cause massive spikes in error—generalize much better than those trapped in "sharp" regions.

To find these flat regions, researchers developed Sharpness-Aware Minimization (SAM). SAM works by solving a minimax problem: it tries to minimize the loss at a specific point while simultaneously ensuring that the maximum loss in a small neighborhood around that point remains low.

The catch? Finding that "maximum loss" point (the inner maximization step) is computationally expensive. To keep training fast, standard SAM uses a single step of gradient ascent to approximate it. However, the loss landscape is often so jagged and unstable that a single gradient step points in the wrong direction, leading to a poor approximation and sub-optimal generalization.

Introducing R-SAM: Smoothing the Landscape

The authors propose Random Sharpness-Aware Minimization (R-SAM) to address this instability. The core idea is to "smooth" the jagged loss landscape before taking that crucial gradient ascent step.

Instead of calculating the gradient directly at the current weights ($w$), R-SAM follows a two-step process for the inner maximization:

  1. Random Smoothing: It adds a small amount of Gaussian noise to the weights to get a "smoothed" weight.
  2. Stabilized Ascent: It calculates the gradient at this noisy point. Because the noise effectively "blurs" the local jaggies, this gradient is more stable and provides a better direction toward the true local maximum.

Crucially, the authors use kernel-wise Gaussian noise, meaning the noise is scaled based on the norm of the weights in each specific kernel or layer. This accounts for the fact that different parts of a neural network have vastly different weight distributions. Once the "adversarial" direction is found using this smoothed gradient, the model performs a standard update.

Key Insights and Ideas

  • The Stability Gap: The foundational insight of R-SAM is that the vanilla gradient in a sharp region acts as a "bad compass." By visualizing the cosine similarity of gradients, the authors demonstrate that as you move further from the starting point (increasing perturbation distance $\rho$), the gradient direction in standard SAM becomes increasingly erratic. R-SAM effectively bridges this stability gap.
Cosine similarity of gradients for ResNet-18 on CIFAR-100, showing R-SAM maintains higher similarity than vanilla SAM.
  • Smoothing as a Regularizer: The paper mathematically proves that adding Gaussian noise makes the loss function more "Lipschitz continuous." Essentially, it mathematically enforces a smoother landscape, making the one-step approximation much more reliable.
  • Efficiency is King: While multi-step gradient ascent would also solve the stability problem, it would double or triple training time. R-SAM achieves similar (or better) stability benefits with virtually zero additional computational cost.
  • Synergy with Data Augmentation: R-SAM doesn't just replace techniques like Mixup or RandAugment; it stacks with them. The flatter minima found by R-SAM provide a better foundation for these augmentations to improve performance further.

Experimental Results

The researchers evaluated R-SAM across several standard computer vision benchmarks, including CIFAR-10/100, ImageNet-1k, and robustness datasets (ImageNet-Real, V2, R, and C). R-SAM consistently outperformed vanilla SAM and other state-of-the-art optimizers:

  • CIFAR-100: On a WideResNet-28-10, R-SAM achieved 85.2% accuracy, compared to 84.6% for SAM and 81.7% for standard SGD with momentum.
  • ImageNet (ViT): Training a Vision Transformer (ViT-B-16) from scratch, R-SAM reached 81.6% accuracy (with augmentations), a significant jump over the 80.8% achieved by vanilla SAM.
  • Robustness: R-SAM showed a marked improvement in handling corrupted data. On ImageNet-C, R-SAM improved accuracy by 0.3% to 1.4% over SAM across different architectures, proving that flatter minima truly translate to more robust real-world performance.

Limitations and Future Work

While R-SAM is highly effective, the authors note a few areas for further exploration:

  • Noise Design: Designing the optimal noise distribution is tricky. While kernel-wise Gaussian noise works well, the weights in neural networks aren't perfectly independent or identically distributed, suggesting there might be even better ways to "blur" the landscape.
  • Hyperparameter Sensitivity: Like SAM, R-SAM introduces new hyperparameters (such as the noise scale $\gamma^2$). While the paper shows R-SAM is relatively stable across different values, finding the absolute "sweet spot" still requires some tuning.
  • Broader Applications: The current study focuses on computer vision. Future work could investigate if R-SAM provides similar benefits in Natural Language Processing (NLP) or Reinforcement Learning.

Summary updated

BibTeX
@inproceedings{liu2022random,
  author = {Yong Liu and Siqi Mai and Minhao Cheng and Xiangning Chen and Cho-Jui Hsieh and Yang You},
  title = {{Random Sharpness-Aware Minimization}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2022},
  url = {https://openreview.net/forum?id=htUvh7xPoa},
}
Figure