← All Posts
Deep Learning · Regularization in Deep LearningChapter 09 / 9

Adversarial Training and SAM: Optimize a Neighborhood

Derive worst-case $L_\infty$ (FGSM) and $L_2$ perturbations from a single inner dot-product maximization, then contrast input-space adversarial training with weight-space SAM.

Whereas random noise minimizes the average loss in a neighborhood, min–max methods minimize the worst-case loss inside a ball. Adversarial training perturbs the input $x+\delta$ to resist adversarial examples; Sharpness-Aware Minimization (SAM) perturbs the weights $w+e$ to seek flat minima.

1. Adversarial training: worst-case input perturbations

Adversarial training (Madry et al., 2018) solves a saddle-point problem over an allowed perturbation ball $\|\delta\|_p\le\varepsilon$:

$$\min_\theta\;\mathbb E_{(x,y)}\!\left[\max_{\|\delta\|_p\le\varepsilon}\ell\bigl(f_\theta(x+\delta),y\bigr)\right].$$

To solve the inner maximization to first order, linearize $\ell(f_\theta(x+\delta),y)\approx\ell(f_\theta(x),y)+g_x^\top\delta$, where $g_x=\nabla_x\ell(f_\theta(x),y)$. Choosing $\delta$ to maximize $g_x^\top\delta=\sum_j g_{x,j}\delta_j$ depends on the norm ball:

For $g_x=(3,-4)$ (so $\|g_x\|_2=5$) and $\varepsilon=0.1$, the $L_\infty$ step is $(0.1,-0.1)$ while the $L_2$ step is $(0.06,-0.08)$. PGD (Projected Gradient Descent) refines this single-step linearization by taking $K$ smaller ascent steps and projecting back onto the ball after each step: $\delta^{(k+1)}=\Pi_{\|\delta\|_p\le\varepsilon}\bigl(\delta^{(k)}+\alpha\operatorname{sign}(\nabla_\delta\ell)\bigr)$.

2. SAM: worst-case weight perturbations for flat minima

Sharpness-Aware Minimization (Foret et al., 2021) applies the exact same $L_2$ inner maximization in parameter space to penalize sharp valleys where nearby weights have much higher loss:

$$\min_w\;\max_{\|e\|_2\le\rho}L(w+e).$$

Using the first-order $L_2$ solution $e(w)=\rho\frac{\nabla_w L(w)}{\|\nabla_w L(w)\|_2}$, each SAM step runs two forward–backward passes:

$$\begin{aligned}1.\;\text{Climb to the perturbed point:}\quad w_{\rm adv}&=w_t+\rho\frac{\nabla_w L(w_t)}{\|\nabla_w L(w_t)\|_2},\\2.\;\text{Update original }w_t\text{ using gradient at }w_{\rm adv}:\quad w_{t+1}&=w_t-\eta\nabla_w L(w_{\rm adv}).\end{aligned}$$

Trace $L(w)=\frac12w^2$ from $w_0=1$ with $\rho=0.1$ and $\eta=0.2$: plain SGD measures $g(1)=1$ and steps to $1-0.2(1)=0.8$. SAM climbs to $w_{\rm adv}=1+0.1(1)=1.1$, measures $g(1.1)=1.1$, restores $w_0=1$, and steps to $w_1=1-0.2(1.1)=0.78$.

Two minima have equal zero pointwise loss, but a fixed-radius neighborhood around the narrower minimum reaches a higher loss.
Scroll the figure horizontally on small screens.
Both valleys achieve $L(w)=0$ at their bottom ($w=-1$ and $w=1$), but within radius $\rho$ the sharp valley at $w=1$ suffers a much higher worst-case loss $\max_{|e|\le\rho}L(w+e)$.

3. Lab: compare pointwise loss and SAM’s neighborhood probe

Probe a broader and a narrower minimum

The exact plotted loss is $L(w)=(w^2-1)^2[0.3+(w+1)^2]$. The orange curve approximates the neighborhood maximum by evaluating 101 evenly spaced perturbations in $[-\rho,\rho]$ at each location. The vertical axis shows $\log_{10}(1+L)$ so both valleys remain visible; readouts show the original loss. Compare the sampled maximum with SAM’s single gradient-directed probe. Step buttons use learning rate $0.01$.

4. Two-pass SAM implementation and scale invariance

# Two-pass SAM update on a single mini-batch
loss = criterion(model(x), y)
loss.backward()  # Pass 1: compute g = grad L(w)
grad_norm = torch.norm(torch.stack([p.grad.norm(2) for p in model.parameters()]), 2)
with torch.no_grad():
    e_list = [rho * p.grad / (grad_norm + 1e-12) for p in model.parameters()]
    for p, e in zip(model.parameters(), e_list):
        p.add_(e)  # climb to w + e

optimizer.zero_grad(set_to_none=True)
criterion(model(x), y).backward()  # Pass 2: compute grad L(w + e) on the SAME batch
with torch.no_grad():
    for p, e in zip(model.parameters(), e_list):
        p.sub_(e)  # restore original w before stepping
optimizer.step()

Because rescaling two adjacent ReLU layers by $(c,1/c)$ leaves the network function unchanged while altering Euclidean sharpness, ASAM (Adaptive SAM) scales each parameter’s perturbation radius proportionally to $|w|$ so the neighborhood is invariant to layer rescaling. Similarly, Virtual Adversarial Training (VAT) applies worst-case input perturbations $\delta$ to maximize $\operatorname{KL}(p(y\mid x)\|p(y\mid x+\delta))$ on unlabeled examples.

5. Questions worth answering

Why must SAM subtract $e$ to restore $w_t$ before calling `optimizer.step()`?

Because $w_{\rm adv}=w_t+e$ is only a probe location used to evaluate the gradient $\nabla L(w_t+e)$. Leaving $+e$ in place would turn the update into $w_t+e-\eta\nabla L(w_t+e)$, adding an uphill step to every iteration.

Why does FGSM use $\operatorname{sign}(g_x)$ while SAM uses $g_w/\|g_w\|_2$?

FGSM maximizes $g_x^\top\delta$ over an $L_\infty$ box ($|\delta_j|\le\varepsilon$), whose maximum occurs at the box corners $\pm\varepsilon$. SAM maximizes $g_w^\top e$ over a Euclidean $L_2$ sphere ($\|e\|_2\le\rho$), whose maximum aligns with the unit vector $g_w/\|g_w\|_2$.

Sources

Goodfellow, Shlens, and Szegedy, Explaining and Harnessing Adversarial Examples; Madry et al., Towards Deep Learning Models Resistant to Adversarial Attacks; Foret et al., Sharpness-Aware Minimization for Efficiently Improving Generalization; Kwon et al., ASAM: Adaptive Sharpness-Aware Minimization; Miyato et al., Virtual Adversarial Training.