Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.How attackers find the weak spot
A model’s input gradient tells you which small change to the input increases its error fastest. FGSM takes one step in that direction. PGD takes many small steps and pulls the input back inside the allowed budget after each one, like a hiker who keeps climbing uphill while staying inside a fenced field. Carlini and Wagner flipped the question round: instead of ‘what is the worst change of a given size?’, they asked ‘what is the smallest change that fools the model?’. Attacks are how robustness is measured, so a weak attack produces a falsely reassuring number.
Defensive distillation (2016) was reported to cut attack success from 96% to under 1%; Carlini and Wagner (2017) showed that a better loss function made the attacks succeed again almost every time.
- 1. FGSM takes…
- 2. PGD keeps the input…
- 3. C&W looks for…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- FGSM: x + eps·sign(∇ₓL); one step, optimal for a linear model (topic M1-1).
- Basic iterative method and PGD: repeated steps δ ← Π(δ + α·sign(∇L)) with projection Π onto the eps-ball; PGD (Madry and colleagues) adds a random start inside the ball and is the standard first-order attack.
- L2 PGD: steps along ∇L/‖∇L‖₂; projection rescales δ by min(1, eps/‖δ‖₂).
- Margin (C&W) loss: max_(j≠y) Z_j − Z_y on the logits Z; it is positive exactly when x is misclassified and does not saturate like the softmax.
- Carlini–Wagner L2 attack: minimise ‖δ‖² + c·max(Z_y − max_(j≠y) Z_j, −κ), with a search over c; κ sets how confident the mistake must be.
- Targeted and untargeted attacks; restarts: several random starts, keeping any that succeeds.
- Black-box attacks: score-based (estimate gradients from output queries: ZOO, NES, SPSA), decision-based (only the label: boundary attack) and transfer attacks.
- Gradient masking: a model whose gradients are zero, noisy or misleading, so gradient attacks fail although adversarial examples still exist.
- 1. PGD differs from the basic iterative method by…
- 2. The margin loss is positive when…
- 3. A decision-based black-box attack sees…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Building the attacks
Exercise 1.
- Three classes in 20 dimensions, each a mixture of four Gaussian blobs, so the decision boundary is curved; a one-hidden-layer ReLU network (64 units, Adam).
- Input gradients of the cross-entropy and of the margin loss, checked against finite differences.
- FGSM; PGD in L∞ and L2 with random start and restarts; the C&W L2 attack with Adam.
- A gradient-masked copy of the network: logits multiplied by 1000 and the softmax computed in 32-bit floating point, which makes the cross-entropy gradient exactly zero for almost every correctly classified point without changing any prediction.
- Checks: PGD stays inside its ball and is at least as strong as FGSM; every C&W output is misclassified.
Practice.
- Use PGD with the margin loss, many steps and several restarts as the baseline, and check that more steps or restarts no longer lower the accuracy.
- Use AutoAttack (Croce and Hein, 2020), an ensemble of parameter-free attacks, for reported numbers.
- Watch for signs of gradient masking: one-step attacks stronger than iterative ones, black-box attacks stronger than white-box, accuracy not reaching 0 as eps grows, or zero gradients.
- Design adaptive attacks that target the defence itself (Tramèr and colleagues, 2020): if a defence adds randomness, average gradients over it; if it is non-differentiable, approximate it.
- Report minimum-norm results too (C&W, DeepFool, FAB): the median smallest perturbation is a threshold-free robustness measure.
def pgd(m, X, y, eps, steps=20, alpha=None, norm="linf", loss="ce", restarts=1, rng=None):
alpha = 2.5 * eps / steps if alpha is None else alpha
best = X.copy(); fooled = np.zeros(len(X), bool)
for _ in range(restarts):
D = rng.uniform(-eps, eps, X.shape) # random start (L∞ case)
for _ in range(steps):
g = grad_x(m, X + D, y, loss)
D = np.clip(D + alpha * np.sign(g), -eps, eps) # step, then project
new = logits(m, X + D).argmax(1) != y
take = new & ~fooled; best[take] = (X + D)[take]; fooled |= new
return best- 1. Reported robustness numbers should use…
- 2. A one-step attack beating an iterative one suggests…
- 3. For a randomised defence, an adaptive attack should…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Why sign steps. Linearising the loss, L(x + δ) ≈ L(x) + ∇L·δ. Over the L∞ ball the maximum of ∇L·δ is eps·‖∇L‖₁, attained at δ = eps·sign(∇L) (Hölder’s inequality); over the L2 ball it is eps·‖∇L‖₂ at δ = eps·∇L/‖∇L‖₂. FGSM and the L2 gradient step are the steepest-ascent directions for each norm, and PGD is projected steepest ascent for the constrained problem max over ‖δ‖ ≤ eps of L(x + δ). Madry and colleagues observed that, on standard networks, PGD from many random starts finds local maxima of very similar loss, which is why it is treated as the strongest first-order attack.
Why the loss matters. The cross-entropy gradient with respect to the logits is p − e_y. When the model is confident, p is almost exactly e_y and the gradient almost vanishes; if the logits are multiplied by s, it vanishes faster as s grows, and in finite precision it becomes exactly zero. The decision, argmax Z, does not change at all. The margin loss max_(j≠y) Z_j − Z_y is simply multiplied by s, so sign-based steps on it are unchanged. This is the mechanism by which Carlini and Wagner broke defensive distillation, and Athalye, Carlini and Wagner (2018) found similar ‘obfuscated gradients’ in 7 of the 9 defences accepted at ICLR 2018 that they examined.
Minimum-norm attacks. C&W replaces the hard constraint ‘misclassified’ with a penalty: minimise ‖δ‖² + c·max(Z_y − max_(j≠y) Z_j, −κ). Small c favours small perturbations that may fail; large c favours success; searching over c finds nearly the smallest successful perturbation. DeepFool instead linearises every class score and jumps to the nearest linearised boundary, which is exact for linear models (topic M1-1) and fast but slightly larger than necessary for curved boundaries.
- 1. Over the L2 ball, the linearised loss is maximised by…
- 2. Multiplying the logits by a large factor changes…
- 3. In C&W, a large constant c favours…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.On an undefended network, how much stronger is PGD than FGSM, and how does gradient masking change what FGSM, cross-entropy PGD and margin-loss PGD report? How close are Carlini–Wagner’s smallest perturbations to those found by PGD with a radius search?
History.
- I. Goodfellow, J. Shlens and C. Szegedy (2015) introduced FGSM; A. Kurakin, I. Goodfellow and S. Bengio (2017) the basic iterative method.
- N. Papernot and colleagues (2016) introduced the saliency-map attack (JSMA) and defensive distillation; S.-M. Moosavi-Dezfooli, A. Fawzi and P. Frossard (2016) introduced DeepFool.
- N. Carlini and D. Wagner (2017) introduced the C&W attacks and broke defensive distillation.
- A. Madry, A. Makelov, L. Schmidt, D. Tsipras and A. Vladu (2018) framed robustness as a min–max problem with PGD as the inner attack.
- Black-box attacks: P.-Y. Chen and colleagues (ZOO, 2017), A. Ilyas and colleagues (NES, 2018), W. Brendel, J. Rauber and M. Bethge (boundary attack, 2018).
- A. Athalye, N. Carlini and D. Wagner (2018) exposed obfuscated gradients; F. Croce and M. Hein (2020) introduced AutoAttack; F. Tramèr and colleagues (2020) described adaptive attacks.
Exercise 2.
- The ReLU network trained on 1000 points; 500 test points; L∞ budgets eps = 0.1, 0.2, 0.3, 0.5 and 0.75.
- Attacks: FGSM; PGD with 20 steps and the cross-entropy loss; PGD with 50 steps, the margin loss and 3 random restarts. Each on the network and on its gradient-masked copy.
- Smallest L2 perturbation for 200 correctly classified test points: C&W with c ∈ 0.3, 1, 3, 10, 30 (best per point) against L2 PGD with a bisection on the radius.
Accuracy under attack, seed 0 (clean accuracy 0.986; 0.968 and 0.972 for seeds 1 and 2):
| eps | FGSM | PGD-20, CE | PGD-50, margin, 3 restarts | masked: FGSM | masked: PGD-20, CE | masked: PGD-50, margin |
|---|---|---|---|---|---|---|
| 0.1 | 0.966 | 0.966 | 0.966 | 0.986 | 0.986 | 0.966 |
| 0.2 | 0.920 | 0.922 | 0.920 | 0.986 | 0.984 | 0.920 |
| 0.3 | 0.858 | 0.852 | 0.850 | 0.986 | 0.984 | 0.854 |
| 0.5 | 0.628 | 0.604 | 0.600 | 0.986 | 0.978 | 0.602 |
| 0.75 | 0.322 | 0.266 | 0.254 | 0.986 | 0.972 | 0.252 |
Findings.
- On the undefended network FGSM was nearly as strong as PGD at small budgets (identical at eps 0.1); the gap grew to 6.8 points at eps 0.75 (6.8 and 7.0 for seeds 1 and 2), as the curved boundary makes one linear step less accurate.
- Gradient masking made the network look perfectly robust to FGSM (0.986 at every eps, exactly the clean accuracy) and nearly so to cross-entropy PGD (0.972 at eps 0.75), because the gradient was exactly zero for 98.6% of test points (96.6% and 97.2% for seeds 1 and 2). The margin-loss attack recovered the true robustness (0.252 against 0.254 for the unmasked network): no prediction had changed.
- C&W found smaller L2 perturbations than PGD with a radius search for 94% of points (98% and 96.5% for seeds 1 and 2), median 2.087 against 2.168 (1.879 against 1.934, and 1.932 against 1.992).
Attacks in practice
- Libraries: Foolbox, the Adversarial Robustness Toolbox (ART), torchattacks and CleverHans implement these attacks; AutoAttack and RobustBench provide standardised evaluation and leaderboards.
- Beyond images: word-substitution and character attacks on text, universal adversarial perturbations, adversarial patches, and jailbreak suffixes found by gradient search on language models (GCG, Zou and colleagues, 2023).
- Physical and space systems: attacks must survive printing, viewpoint and lighting changes (expectation over transformation, Athalye and colleagues 2018); sensors and onboard models face both deliberate attacks and natural perturbations.
References
- A. Madry and colleagues (2018). “Towards deep learning models resistant to adversarial attacks.” ICLR.
- N. Carlini and D. Wagner (2017). “Towards evaluating the robustness of neural networks.” IEEE Symposium on Security and Privacy.
- S.-M. Moosavi-Dezfooli, A. Fawzi and P. Frossard (2016). “DeepFool: a simple and accurate method to fool deep neural networks.” CVPR.
- A. Athalye, N. Carlini and D. Wagner (2018). “Obfuscated gradients give a false sense of security.” ICML.
- F. Croce and M. Hein (2020). “Reliable evaluation of adversarial robustness with an ensemble of diverse parameter-free attacks.” ICML.
- A. Ilyas, L. Engstrom, A. Athalye and J. Lin (2018). “Black-box adversarial attacks with limited queries and information.” ICML.