Skip to content
BIASED

Adversarial examples

The $L_\infty$ threat model, the linear nature of the vulnerability, FGSM and PGD, attack transfer and adversarial training as a min–max at a cost in clean accuracy.

Accuracy and robustness

Vision Transformers closed the representations module: CNNs and ViTs that reach high accuracy on held-out images. Every accuracy in the course so far was measured the same way — on test images drawn like the training images. This topic measures something else: accuracy on inputs chosen by someone who wants the network to fail.

A digit MLP can score 0.9650.965 clean, stay at 0.9560.956 under random ±0.1\pm 0.1 noise, and fall to 0.4300.430 when the same budget uses attack signs. Size is the same; direction differs. An adversarial example for xx with label yy is an x′x' with

∥x′−x∥∞≤ε,x′∈[0,1]d,fθ(x′)≠y.\|x' - x\|_\infty \le \varepsilon, \qquad x' \in [0, 1]^d, \qquad f_\theta(x') \ne y.

The allowed set is the threat model. Robust accuracy at budget ε\varepsilon is the fraction of tests for which no such x′x' exists; an attack only upper-bounds it. Standard budgets: ε=0.3\varepsilon = 0.3 on MNIST, ε=8/255\varepsilon = 8/255 on CIFAR-10.

Why even a linear model is vulnerable

Key idea. In high dimension many tiny changes add up. For a linear logit,

max⁡∥δ∥∞≤εw⊤δ=ε∥w∥1,\max_{\|\delta\|_\infty \le \varepsilon} w^\top \delta = \varepsilon \|w\|_1,

attained at δ=εsign⁡(w)\delta = \varepsilon \operatorname{sign}(w). If weights average magnitude mm, the shift is εmd\varepsilon m d. A 224×224224 \times 224 colour image has d=150 528d = 150\,528; with ε=1/255\varepsilon = 1/255 and m=0.01m = 0.01 the logit moves by about 5.95.9 — enough to flip most predictions — from the smallest change an 8-bit file can store.

Networks are built from nearly linear pieces. To first order L(θ,x+δ,y)≈L+δ⊤∇xLL(\theta, x+\delta, y) \approx L + \delta^\top \nabla_x L, so the same construction with w=∇xLw = \nabla_x L maximizes the local linear model. Goodfellow et al. argued models are “too linear, rather than too nonlinear”; on digits one linear step is almost as damaging as twenty careful ones.

FGSM: one step to a corner

The fast gradient sign method (FGSM) takes that maximizer as the attack:

x′=clip⁡[0,1](x+εsign⁡(∇xL(θ,x,y))).x' = \operatorname{clip}_{[0,1]}\big(x + \varepsilon \operatorname{sign}(\nabla_x L(\theta, x, y))\big).

Cost: one forward and one backward pass, in evaluation mode. Every pixel moves by the full ε\varepsilon, so x′x' is a corner of the box.

Worked example: logistic unit w=(2,−1,0.5)w = (2, -1, 0.5), x=(0.5,0.2,0.4)x = (0.5, 0.2, 0.4), label 11, ε=0.3\varepsilon = 0.3. Logit z=1z = 1, probability 0.7310.731, loss 0.3130.313. Input gradient (σ−1)w(\sigma - 1)w has sign (−1,1,−1)(-1, 1, -1), so x′=(0.2,0.5,0.1)x' = (0.2, 0.5, 0.1) and z′=−0.05=1−ε∥w∥1z' = -0.05 = 1 - \varepsilon \|w\|_1. The prediction flips. On MNIST, FGSM with ε=0.25\varepsilon = 0.25 made a shallow softmax wrong on 99.9%99.9\% of the test set — wrong and confident.

PGD: many steps with projection

FGSM trusts the linear model over the whole box. Projected gradient descent (PGD) trusts it only for small steps. Repeated sign steps of size α<ε\alpha < \varepsilon, each projected back onto the box and the pixel range:

xk+1=Π(xk+αsign⁡(∇xL(θ,xk,y))),x^{k+1} = \Pi\big(x^k + \alpha \operatorname{sign}(\nabla_x L(\theta, x^k, y))\big),

with Π\Pi clipping each coordinate to [xi−ε,xi+ε]∩[0,1][x_i - \varepsilon, x_i + \varepsilon] \cap [0, 1]. Madry et al. start from a random point inside the box. On digits, random noise at ε=0.3\varepsilon = 0.3 costs at most 1616 points; FGSM and 20-step PGD drive accuracy near zero by ε=0.2\varepsilon = 0.2, with PGD only a few points stronger — the network is close to linear inside the box.

Targeted attacks descend the loss of a chosen class tt (minus sign). Hitting one region is harder than leaving the correct one: at ε=0.1\varepsilon = 0.1, untargeted PGD already breaks 57%57\% of digit predictions while targeted y+1y+1 succeeds on 15.2%15.2\%.

Transfer and adversarial training

White-box attacks know the weights. Transfer attacks craft examples on a surrogate and submit them to the target with no queries. They work because models of the same task learn similar functions, so input gradients align. On digits, FGSM from a wider surrogate drops the target to 0.5500.550 at ε=0.1\varepsilon = 0.1 against 0.4610.461 white-box. Untargeted examples transfer easily; targeted ones rarely keep their target.

If the network fails on adversarial inputs, train on them. Adversarial training solves

min⁡θ E(x,y)[max⁡∥δ∥∞≤εL(θ,x+δ,y)].\min_\theta\ \mathbb E_{(x,y)}\Big[\max_{\|\delta\|_\infty \le \varepsilon} L(\theta, x+\delta, y)\Big].

Inner step: PGD for KK steps to δ∗\delta^*. Outer step: gradient on θ\theta at x+δ∗x+\delta^*, treating δ∗\delta^* as constant. Danskin’s theorem says the chain-rule term through δ∗(θ)\delta^*(\theta) vanishes at a maximizer. Cost about K+1K+1 passes per batch. One-step (FGSM) training can catastrophically overfit the one-step direction while staying open to PGD; a random start before FGSM largely prevents that.

The trade-off, and what comes next

On digits, PGD training at ε=0.2\varepsilon = 0.2 raises PGD accuracy from 0.0020.002 to 0.5390.539 and drops clean accuracy from 0.9650.965 to 0.9440.944. On CIFAR-10 the clean drop is larger (95.2%95.2\% → 87.3%87.3\%) and robust accuracy stays far below clean (45.8%45.8\% under PGD). Tsipras et al. show the trade-off can be unavoidable: non-robust features buy clean accuracy and an L∞L_\infty adversary can flip them.

A threat model is not the world — rotations, stickers and lighting are other attacks. Accuracy under an attack is only an upper bound; defences that obfuscate gradients can look robust until a stronger attack is used. Certified methods exist but cost more for smaller radii.

Robustness is one deployment budget. The next topic is another: model compression — pruning, distillation, factorization and fewer bits — so a trained net fits memory, latency and energy.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Go deeper — optional

Code and labs do not count toward the main percentage or the certificate.

  • FGSM: one step on a logistic unitoptional

    Reproduces the topic worked example: gradient sign, ε=0.3, logit sign flip. Self-check in the notebook.

    ~15 min · easier on a computer · Adversarial examples topic read.

    Self-check in the notebook — not part of mastery or the certificate.

Full notes

Answers are saved on the server and tied to this browser until you sign in.