Exercises — Adversarial examples and robustness
This page is a self-test ladder. Each rung is harder than the last. Cover the solution, try it, then reveal. Every symbol used here was built in the parent note — if a piece feels unfamiliar, re-read that first.
A quick vocabulary refresher, all defined in the parent, so nothing here is a surprise:
- — the perturbation, the little vector we add to an input.
- — the biggest single change across all coordinates.
- — the straight-line length of the perturbation.
- — the gradient of the loss with respect to the input: a vector telling us, coordinate by coordinate, how fast the loss climbs if we nudge that input coordinate up.
- — returns , , or for each entry depending on whether it is positive, negative, or exactly zero.
Level 1 — Recognition
Exercise 1.1 (L1)
An attacker changes an image so that each pixel value moves by at most , and some pixels move by exactly that much. Which norm is being bounded, and what is ?
Recall Solution
"At most, per pixel" is the definition of the == norm==: . The bound is . Why not ? would bound the total length across all pixels, letting one pixel change hugely if others stay still. Here the cap is on each pixel individually, which is exactly what measures.
Exercise 1.2 (L1)
For a gradient component , what value does FGSM place in that coordinate of the perturbation when ?
Recall Solution
FGSM sets . Since the gradient is negative, , so Notice: the magnitude is thrown away. Under , all that matters is the direction (sign) — every coordinate gets pushed the full , never more, never less.
Level 2 — Application
Exercise 2.1 (L2)
A 1-D toy input has input-gradient . With , compute the FGSM adversarial input (untargeted).
Recall Solution
Step 1 — signs: . Step 2 — perturbation: . Step 3 — add: . Check the budget: . ✓
Exercise 2.2 (L2)
Continue Exercise 2.1, but now it is a targeted attack toward class , and the gradient of the target-class loss is . Compute .
Recall Solution
Targeted FGSM subtracts: . Signs: . Note the exact zero: . Perturbation: . . Why subtract? For untargeted we climb the loss of the true label. For targeted we want the model to prefer class , i.e. lower the loss of — descending, hence the minus sign.
Exercise 2.3 (L2)
Take one PGD step on the 1-D input with , step size , and gradient sign at the current point. Then take a second step with gradient sign . Give after projection. (Assume pixel-range clipping to is not binding.)
The figure below traces the two steps geometrically: start point in white, first landing point in blue, final point in green, all inside the yellow box. Follow the two arrows and confirm they never leave the box.

Recall Solution
Step 1 (gradient): . Project to the -ball around : clip each coord to . Both are inside → (blue point in the figure). Step 2 (gradient): . Project: both inside → (green point). Budget check: . ✓ — the green point sits comfortably inside the yellow box.
Level 3 — Analysis
Exercise 3.1 (L3)
Show algebraically that among all with , the dot product (where ) is maximised by , and find that maximum value.
Recall Solution
Write the dot product coordinate-by-coordinate: . The constraint means each independently satisfies — the coordinates don't trade off against each other (unlike ). So we maximise each term separately. For a fixed , the product over is largest when has the same sign as and is as big as allowed: . Then . Summing: So the best-case first-order loss increase is — the norm of the gradient shows up because it is the "dual" of the ball.
Exercise 3.2 (L3)
Using the toy gradient and , compute the predicted first-order loss increase from the FGSM step, and compare it to the loss increase you'd get from an -bounded step of the same length .
Recall Solution
FGSM (): increase . Best step of length : the optimal direction points along itself, giving increase . Here , so increase . Reading it: (FGSM) achieves a larger first-order increase () for the same per-coordinate budget, because it independently maxes out every coordinate. But note the step has a longer Euclidean length (), so it isn't a free lunch — it just uses a bigger box.
Exercise 3.3 (L3)
Fix the Euclidean length of the perturbation at (an threat model, not FGSM). Assume every gradient coordinate has the same magnitude and the same sign, in dimensions. Find the -optimal perturbation, show the predicted loss increase grows like , and explain what this says about the curse of dimensionality.
Recall Solution
Why here, not ? The point of this exercise is to hold the perceptibility measure — Euclidean length — constant and watch how effectiveness scales with dimension. FGSM's step would instead fix the per-coordinate size and let the Euclidean length grow, which mixes two effects. To isolate "does dimension alone help the attacker?", we bound . Optimal direction. Under a fixed Euclidean length, is maximised by pointing along (Cauchy–Schwarz): . With all and equal sign, this puts an equal share into every coordinate: . That is why we "spread evenly" — it is not an arbitrary choice, it is the forced consequence of the gradient being uniform. Check the length: . ✓ Loss increase. So with a fixed, imperceptible Euclidean nudge, the loss increase scales as . For a image, — a factor-of-hundreds amplification, purely from dimension count. This is the geometric root of vulnerability: many tiny aligned nudges sum to a large effect. High dimensions help the attacker, not the defender.
The figure plots this growth: the blue curve is against dimension on a log axis; the coloured dots mark a toy problem, MNIST, and a full image — watch the amplification climb from ~2 to ~388.

Level 4 — Synthesis
Exercise 4.1 (L4)
You are asked to build the adversarial-training inner loop. Given a clean batch and the Madry objective write the exact per-batch pseudocode and justify two design choices: (a) why we detach/stop-gradient through the PGD attack, and (b) why we still backprop through the model afterward.
Recall Solution
For each batch (X, y):
X_adv = X.detach() + random_start in [-eps, eps] # inner max via PGD
repeat T times:
g = grad_x L(theta, X_adv, y) # attack step
X_adv = X_adv + alpha * sign(g)
X_adv = clip(X_adv, X - eps, X + eps) # project to L_inf ball
X_adv = clip(X_adv, 0, 1) # valid pixel range
X_adv = X_adv.detach() # freeze the attack result
loss = CrossEntropy(model(X_adv), y) # outer min
loss.backward(); optimizer.step() # update theta
(a) Detach the attack: The inner is solved approximately by PGD and treated as a fixed data-augmentation. If we differentiated through the whole PGD chain, gradients would explode in cost and instability; Madry et al. use Danskin's theorem, which says that to first order it suffices to evaluate the outer gradient at the worst-case and treat that as constant. Hence stop-gradient. (b) Still backprop the model: The outer is what actually learns robustness — it updates so that even the worst-case perturbed input is classified correctly. Without this backward pass on , no learning happens at all.
Exercise 4.2 (L4)
Estimate the compute cost multiplier of PGD- adversarial training versus standard training, counting forward+backward passes. For , give the number.
Recall Solution
Let one training-pass mean exactly one forward pass through the network plus one backward pass — the two operations every gradient computation needs. (Standard training does exactly one such pass per batch.) Standard training: 1 forward + 1 backward per batch = training-pass. PGD- attack: each of the steps needs a forward pass and a backward pass to the input to get → training-passes. Then the outer update needs 1 more forward + 1 backward on → 1 training-pass. Total training-passes. Multiplier over standard . For : the compute. (This is the well-known reason robust training is expensive.)
Level 5 — Mastery
Exercise 5.1 (L5)
A defender reports "100% PGD accuracy at ." The attack uses and step, starting from the clean image with no random restart. Argue why this number is almost certainly a false sense of security, and name the phenomenon.
Recall Solution
With and and no random start, "PGD" collapses into a single FGSM step — the weakest attack in the family. A model can then exploit gradient masking (a.k.a. obfuscated gradients): it arranges its loss surface so that the local input gradient near clean images is uninformative — for example by saturating activations so or points in a useless direction — so a single-step attack finds nothing to climb. Yet the true decision boundary is still close: a stronger attacker (many small steps with , multiple random restarts, or a transfer/black-box attack that never uses the defender's own gradient) still crosses it easily. The phenomenon: gradient masking / obfuscated gradients. The reported "100%" measures resistance to a crippled one-step attacker, not genuine robustness. Fix / diagnosis: evaluate with strong PGD (, , several random restarts) and cross-check with a gradient-free or transfer attack. Telltale signs of masking: accuracy that stays pinned at 100% then collapses abruptly as grows (instead of degrading smoothly), or single-step attacks beating iterative ones. See Out-of-distribution detection for the complementary problem of inputs far from the data manifold.
Exercise 5.2 (L5)
Consider a model that is certifiably robust: for a specific input , no perturbation with changes the top class. Suppose the correct logit exceeds every other logit by margin , and the total logit function is -Lipschitz in the input norm. Derive the largest you can certify and evaluate it for .
Recall Solution
Lipschitz means any input change of size moves any logit by at most . The gap between the top logit and a runner-up can shrink at most twice as fast (top can fall by , runner-up can rise by ), so the gap stays positive as long as Certified radius Reading it: larger margin → more room; smaller Lipschitz constant → flatter, more predictable model → bigger certified radius. This is why robustness research pushes for large margins and Lipschitz control, and it links back to Interpretability and explainability: a certifiably smooth model is also easier to reason about. For the broader safety picture see AI Safety fundamentals and note the contrast with Reward hacking, where the failure is in the objective, not the input.
Recall Self-test checklist (reveal to grade yourself)
Could you… state which norm a bound describes? ::: Ex 1.1 Compute an FGSM and a targeted-FGSM step by hand? ::: Ex 2.1, 2.2 Run PGD with projection and verify the budget? ::: Ex 2.3 Prove is optimal and get ? ::: Ex 3.1 Explain the dimensional amplification under a fixed length? ::: Ex 3.3 Write the Madry inner/outer loop and justify stop-gradient? ::: Ex 4.1 Give the compute multiplier? ::: Ex 4.2 Diagnose gradient masking from a suspicious "100%"? ::: Ex 5.1 Derive a certified radius ? ::: Ex 5.2