Turn local derivatives into feasible test candidates
Derive one-step norm-constrained loss ascent and validate the candidate through the complete prediction path.
On this page
The question for today
What you’ll understand
- Derive sign-gradient and Euclidean-gradient directions.
- Account for clipping at the input domain boundary.
- Distinguish a surrogate-loss increase from actual attack success.
An input gradient says how a chosen differentiable loss changes locally. It does not directly identify a valid adversarial example. A testing procedure must choose a direction compatible with its norm, keep the candidate inside the input domain, and run the resulting candidate through the real classifier. The fast gradient sign method makes these distinctions inspectable in a single step. Its simplicity is useful for checking a model wrapper and gradient flow, while its local approximation also explains why surviving it is weak evidence about a nonlinear classifier’s worst case.
Solve the local linear problem
Approximate the loss near the clean input by its first-order expansion. Under an infinity-norm constraint, each coordinate can move independently by at most epsilon. Choosing the sign of each gradient component maximizes the linearized increase, with any value in the allowed interval equally good for a zero component. Under a Euclidean constraint, the best nonzero-gradient direction is the normalized gradient. The two rules solve different constrained problems even when they share a numeric epsilon.
Untargeted testing typically increases a loss against the true label. A targeted attack instead tries to induce a specified target, often by decreasing the target-label loss or increasing a target margin. Record the sign convention explicitly; accidentally minimizing the true-label loss can make an attack appear ineffective because it improves the original classification. Use logits and numerically stable losses, and inspect whether gradients are finite and connected to the actual input tensor.
Differentiate the same pipeline you evaluate
A classifier wrapper may resize, normalize, reorder channels, or aggregate outputs. The gradient should describe that same computation whenever exact differentiation is available. If normalization is performed outside the wrapper, explicitly transform both derivatives and perturbation constraints rather than mixing raw and normalized spaces. Keep model evaluation mode fixed so dropout or batch statistics do not accidentally change the defended classifier between clean and candidate evaluations.
Clip or project the generated candidate into the legal domain, then recompute the actual displacement from the original input. A step near a box boundary can have a smaller norm and a different linearized loss increase than the nominal sign vector. Save both proposed and realized perturbation summaries. A visually plausible sample is insufficient evidence of feasibility: check every relevant constraint numerically, including encoding round trips if the service accepts bytes rather than arbitrary real tensors.
Use one step as a diagnostic, not a guarantee
The linearized objective can increase while the predicted class stays correct. A decision boundary depends on competing outputs, not just the numeric value of one surrogate loss. Conversely, a model can be misclassified with only a modest loss change. Check the declared success predicate on the final candidate and retain the actual outputs. Avoid reporting loss thresholds as attack success unless the application’s failure definition is itself a threshold on that exact score.
Run stronger iterative and diversified evaluations after the one-step sanity check. Large curvature, saturation, and piecewise operations can make the initial gradient a poor guide over the whole allowed ball. A one-step attack also has no mechanism for comparing multiple local directions after crossing a nonlinear region. When its results look unexpectedly strong or weak, inspect the wrapper and gradients before inventing an explanation about model robustness. The method is a useful probe, while the evidence remains limited to the feasible candidates actually tested.
Work it through
A boundary clips the nominal sign step
Use gradient g=(3,−4), infinity budget ε=0.1, and clean input x=(0.95,0.05) in [0,1]². Compute the nominal sign perturbation, its linearized loss increase, and the actual clipped candidate. Compare with the unconstrained Euclidean-budget direction for the same epsilon.
The infinity sign direction is δ=(0.1,−0.1). Its nominal first-order increase is g·δ=3×0.1+(−4)×(−0.1)=0.7 in loss units.
The proposed input is (1.05,−0.05), so clipping yields (1,0). Actual displacement is (0.05,−0.05), with infinity norm 0.05 and linearized increase 0.35.
The Euclidean gradient norm is 5, so its unconstrained-ball optimizer is 0.1×(3,−4)/5=(0.06,−0.08), giving increase 0.5 before any box projection. It is a different geometry and is also clipped if applied to this boundary-near input.
The nominal budget and direction do not replace validation of the realized input. None of these surrogate calculations alone establishes a wrong predicted label.
The living notebook
Explore the model
Interactive lab · network
Inspect an output response to an input change
Reset, set Input x to 1 and Weight w to 1. Predict product 1, logit 0.5, and sigmoid approximately 0.6225 before Step. Reset, set x=1.2 and w=1, then Step: predict logit 0.7 and sigmoid approximately 0.6682. Playback traces a fixed single-unit computation; no training or attack optimization occurs. This toy response is not a robustness score, and a higher output is not automatically an attacker success.
- Product · wx
- 1.2
- Logit · wx − 0.5
- 0.7
- Sigmoid output
- 0.6682
One inspectable sigmoid unit, σ(wx − 0.5). Playback traces the computation graph. There is no training, attention mechanism or hidden deep network. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forA changed score must be interpreted through the actual task and success predicate.
Pause & reason
Check your understanding
Your turn
Try it yourself
For g=(−2,0,5), use an infinity budget 0.02 and choose sign(0)=0. Assume the input is far from all box boundaries. Find the sign perturbation and first-order loss increase.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Each nonzero coordinate moves by epsilon in its gradient’s sign; compute the dot product afterward.
Compare with the solution
The perturbation is (−0.02,0,0.02). Its infinity norm is 0.02 and the linearized increase is (−2)(−0.02)+5(0.02)=0.14 loss units. A real candidate must still be evaluated against the declared output failure condition; 0.14 is not an attack-success probability.
Keep the idea
Take it with you
- The attack direction depends on the norm and loss sign convention.
- Projection changes the realized candidate and sometimes its effective budget.
- Validate output failure separately from surrogate-loss movement.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.