Sharpness-Aware Minimization (SAM) is an optimization algorithm that seeks parameters in neighborhoods of uniformly low loss by simultaneously minimizing the loss value and the loss sharpness. Unlike standard optimizers like SGD or Adam that only minimize training loss, SAM explicitly penalizes sharp minima—regions where loss increases rapidly—by solving a min-max optimization problem that finds a perturbation maximizing loss within a defined radius before taking a gradient step.
Glossary
Sharpness-Aware Minimization (SAM)

What is Sharpness-Aware Minimization (SAM)?
Sharpness-Aware Minimization (SAM) is an optimization algorithm that simultaneously minimizes loss value and loss sharpness to find flatter minima, improving model generalization.
The algorithm operates in two stages: first, it computes an adversarial perturbation by ascending the gradient scaled by a neighborhood radius hyperparameter ρ; second, it computes the gradient at the perturbed point and descends to update model weights. This double-step procedure steers optimization toward flatter loss landscapes, which correlate strongly with better generalization in Vision Transformers and other overparameterized models, often yielding significant accuracy improvements without architectural changes.
Key Characteristics of SAM
Sharpness-Aware Minimization (SAM) is an optimization algorithm that explicitly seeks flat minima—parameter regions where the loss landscape is uniformly low—rather than simply minimizing training loss. This section breaks down the core mechanisms that distinguish SAM from standard stochastic gradient descent.
The Min-Max Optimization Objective
SAM reformulates the standard empirical risk minimization problem into a min-max saddle-point problem. Instead of finding parameters w that minimize the loss L(w), SAM seeks parameters that minimize the maximum loss within a neighborhood of w. This is expressed as:
- Inner Maximization: For a given parameter vector w, SAM identifies the perturbation ε within a Euclidean ball of radius ρ that maximizes the loss.
- Outer Minimization: The optimizer then updates w to minimize the loss evaluated at the perturbed point w + ε.
This two-step process forces the optimizer to find solutions where the loss surface is flat, not just low at a single point.
The Sharpness-Aware Gradient
The practical implementation of SAM computes a distinct sharpness-aware gradient that differs from the standard stochastic gradient. The process involves two forward-backward passes per step:
- First Pass: Compute the standard gradient ∇L(w) at the current parameters. The perturbation ε is approximated by scaling this gradient to have a norm equal to the neighborhood radius ρ.
- Second Pass: Compute the gradient again at the perturbed point w + ε. This second gradient is the actual update direction.
This effectively penalizes steep valleys in the loss landscape, as the gradient at the perturbed point points sharply back toward the minimum if the surrounding region is high-loss.
Flat Minima and Generalization
The central hypothesis of SAM is that flat minima generalize better than sharp minima. A sharp minimum is a narrow valley in the loss landscape where a small parameter shift causes a large increase in loss. A flat minimum is a wide basin where loss remains low across a broad region.
- Connection to Generalization: Sharp minima are highly sensitive to the specific training data distribution. When the test distribution shifts slightly, a model in a sharp minimum degrades rapidly. Flat minima represent solutions that are robust to such shifts.
- Empirical Evidence: SAM consistently improves generalization across architectures—from ResNets to Vision Transformers—without requiring additional data or model capacity. On ImageNet, SAM has been shown to boost top-1 accuracy by over 1% for ViT models.
The m-Sharpness Variant
Standard SAM defines sharpness over the entire training set, which is computationally prohibitive. The practical variant, m-Sharpness, defines sharpness over mini-batches, making the algorithm tractable for large-scale training.
- Mini-batch Sharpness: The maximization step is performed on the same mini-batch used for the gradient computation, not the full dataset.
- Efficiency Trade-off: While m-Sharpness is a noisy approximation of the true sharpness, it retains the core benefit of seeking flat regions while fitting within standard distributed training pipelines.
- Implementation: This variant is the default in popular implementations and is what enables SAM to scale to large models like ViT-H trained on ImageNet-21k.
Adaptive SAM (ASAM)
A key limitation of standard SAM is that its neighborhood definition uses a fixed-radius Euclidean ball, which treats all parameters equally regardless of their scale. Adaptive SAM (ASAM) addresses this by defining the maximization neighborhood using a scale-invariant norm.
- Scale Invariance: ASAM normalizes the perturbation for each parameter by its magnitude, ensuring that the sharpness definition is not biased by parameter scaling.
- Improved Performance: ASAM consistently outperforms standard SAM, particularly when there are large discrepancies in weight norms across layers—a common scenario in deep Transformer architectures.
- Relevance to ViTs: For Vision Transformers, where attention and MLP layers have different weight statistics, ASAM provides more robust flatness-seeking behavior.
SAM in Medical Imaging Pipelines
SAM is particularly impactful in medical imaging applications where training data is scarce and distribution shifts between hospitals and scanner types are common. By explicitly optimizing for flat minima, SAM produces models that are more robust to these domain shifts.
- Limited Data Regimes: When fine-tuning a Vision Transformer on a small dataset of radiological scans, SAM prevents overfitting to spurious correlations in the training set.
- Cross-Scanner Generalization: Models trained with SAM on MRI data from one manufacturer maintain higher accuracy when deployed on scans from a different vendor.
- Integration with Self-Supervised Pre-training: SAM is often combined with self-supervised methods like MAE or DINO during fine-tuning, further boosting the robustness of the resulting diagnostic model.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Frequently Asked Questions
Clear, technically precise answers to the most common questions about the Sharpness-Aware Minimization optimization algorithm and its role in improving model generalization.
Sharpness-Aware Minimization (SAM) is an optimization algorithm that simultaneously minimizes a model's loss value and the sharpness of the loss landscape, explicitly seeking parameters located in neighborhoods where the loss is uniformly low. Instead of finding a single point with minimal training loss, SAM solves a min-max optimization problem: it first identifies the worst-case parameter perturbation within a defined radius that maximizes the loss, then minimizes the loss at that perturbed point. This process is implemented efficiently via a single additional forward and backward pass per step, where gradients are computed at the perturbed parameters w + ε(w) rather than the current parameters w. The resulting flat minima consistently generalize better than the sharp minima found by standard optimizers like SGD or Adam, as small parameter shifts caused by data distribution changes do not cause catastrophic performance degradation.
Related Terms
Sharpness-Aware Minimization is part of a broader ecosystem of techniques focused on finding flatter minima and improving model generalization. These related concepts are critical for training robust Vision Transformers in data-scarce medical imaging domains.
Stochastic Weight Averaging (SWA)
An optimization procedure that averages multiple checkpoints traversed by SGD with a cyclical or high-constant learning rate schedule. Unlike SAM, which actively seeks flat regions during training, SWA approximates the center of a wide basin by averaging points along the trajectory. Key distinction: SAM minimizes sharpness directly; SWA finds flatness through temporal averaging. Often combined with SAM for state-of-the-art results.
Label Smoothing
A regularization technique that softens hard one-hot targets by assigning a small probability mass to incorrect classes. This prevents the model from becoming overconfident and encourages representations where similar classes cluster closer. Interaction with SAM: Label smoothing reduces the sharpness of the loss landscape by discouraging extreme logit values, complementing SAM's explicit sharpness penalty. Critical for medical imaging where inter-class boundaries are often ambiguous.
Flat Minima Hypothesis
The theoretical foundation motivating SAM. This hypothesis posits that parameters residing in wide, flat basins of the loss landscape generalize better than those in narrow, sharp minima. Mechanism: Small perturbations to weights in a flat minimum cause minimal changes in loss, making the model robust to the distribution shift between training and test data. SAM operationalizes this by explicitly penalizing the largest eigenvalue of the Hessian.
Adaptive Sharpness-Aware Minimization (ASAM)
An extension of SAM that defines sharpness relative to the scale of parameters. Standard SAM's perturbation radius is uniform, which can be suboptimal for layers with vastly different weight norms. ASAM's innovation: Scales the perturbation neighborhood element-wise using the absolute value of each weight, ensuring the sharpness measure is scale-invariant. This often yields superior performance in Vision Transformers where attention and MLP layers operate at different scales.
Gradient Centralization
A simple, computation-free technique that normalizes gradient vectors to have zero mean before backpropagation. This directly constrains the loss landscape by projecting gradients onto a hyperplane, smoothing the optimization path. Synergy with SAM: Both techniques regularize the geometry of the loss surface. Gradient centralization acts on the gradient directly, while SAM acts on the parameter space. Combined, they provide complementary smoothing effects.
Hessian Eigenvalue Regularization
A family of methods that directly penalize the curvature of the loss landscape by adding the trace or spectral norm of the Hessian matrix to the training objective. Relationship to SAM: SAM implicitly minimizes the largest Hessian eigenvalue through its two-step inner maximization and outer minimization. Explicit Hessian regularization is more computationally expensive but provides a direct signal. SAM is often preferred as a practical, scalable approximation.

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us