TL;DR
The fast gradient sign method (FGSM) makes an adversarial example in one step. It computes how the model's loss changes with each input value, then moves every value by the same small amount in the direction that increases the loss. Because it needs only one gradient, it is cheap enough to run on every test input, which makes it a standard first check of a model's robustness.
Builds on: Adversarial Examples, Backpropagation, Logistic Regression · Leads to: Adversarial Training, Explaining and Harnessing Adversarial Examples
Where it's used
Robustness testing before release. A team that ships an image classifier runs FGSM on its test set at a few budgets and reports the accuracy under attack next to the clean accuracy. A large drop is a warning that the model relies on details an attacker can change.
Generating training data. Adversarial Training adds attacked copies of training images to each batch. FGSM is the usual generator because it costs one extra backward pass per batch, while stronger attacks cost many more.
Baseline for new defences. Papers that propose a defence against adversarial inputs compare against FGSM first. A defence that fails against FGSM is not worth testing further, and one that resists FGSM still has to be tested against stronger attacks.
How it works
Take a tiny logistic classifier with two inputs. Its probability that the input is positive is , where is the Sigmoid Function. Let and . For a true label , the loss is the Cross-Entropy Loss, .
- Start with a correct input. Take . The score is , so . The loss is . Check: is above one half, so the answer is correct.
- Set a budget. Each input value may change by at most , in either direction. Check: the largest change to any one value is 0.5.
- Compute the Gradient of the loss with respect to the input. The gradient is . Check: the first entry is negative, so lowering the first input value raises the loss.
- Take the sign. The sign of each entry is . Only the direction matters, not the size of each gradient entry.
- Move each value by the budget. . Check: each value moved by exactly 0.5.
- Recompute. The score is , so . The answer is now negative, and the loss has risen from about 0.31 to about 0.97. Check: the label flipped and the loss went up.
The attack is a single gradient step: there is no search and no repetition. The whole attack is determined by the gradient at the starting point.
- loss on the attacked input
The math
Step 1 in symbols: the gradient of the loss
For the logistic classifier, the Gradient is built from the Chain Rule. Let and . The loss for a label is . By the Chain Rule, since and ,
Check against step 3: .
Step 2 in symbols: the budget
The allowed changes form a box: , which means for every entry (see Vector Norm).
Step 3 in symbols: why the sign is the best move
A small change changes the loss by about , where . To make this as large as possible inside the box, each term must be as large as possible on its own. Each term is largest when , which gives . Adding the terms gives the total gain
The sign rule is therefore not an approximation of the best move. Within this linear estimate it is the exact best move.
Step 4 in symbols: the flip condition
For the example, the attacked score is . Because , we have , so . The label flips when , which happens when . The score drops by , where is the L1 norm of the weights. This is the same factor of 3 that appears in .
Iterative variant
Repeating the step with a smaller step size and projecting back into the box gives the iterative method:
The clip keeps every value inside the budget. This costs one gradient per step, so it is many times more expensive than FGSM, and it is usually stronger.
Worked examples
Use the model , , with , and .
- Score: . Probability: . Loss: .
- Gradient: .
- Sign: .
- Adversarial input: .
- Score: . Probability: .
- Loss: .
- Check: the loss rose from 0.31 to 0.97, and the predicted label flipped from positive to negative.
Interpretation: a change of half a unit on each value, in the direction the gradient gives, was enough to flip the answer of this small model.
Keep the same model, input and label. The attacked input for a budget is , and its score is .
- Budget : the input is , the score is , and the probability is . The label is still positive.
- Budget : the input is , the score is , and the probability is . Still positive, but closer to the boundary.
- Budget : the input is , the score is , and the probability is . The label has flipped.
- Check: the flip happens at , where . Every budget above that flips the label, and every budget below it does not.
- Try it: at what budget does the loss first reach ? Answer: at , because there and .
Interpretation: for a linear model the budget that flips the answer depends only on the score and the weights. The gradient tells the attacker which way to move, and the weights set how much each unit of budget is worth.
Suppose a team tests a vision model on 10,000 images. Each image has values. Each gradient computation, one forward pass and one backward pass, takes about 0.05 seconds on the team's hardware. This timing is an assumption for the illustration, not a measurement.
- One FGSM pass over the test set needs one gradient per image: gradients.
- Time: seconds, about 8 minutes.
- A 20-step iterative attack needs 20 gradients per image: gradients.
- Time: seconds, about 2.8 hours.
- The budget for brightness values scaled to the range 0 to 1 is often written as , two brightness levels out of 255. Each value changes by less than one percent of its range.
- Check: the attack is 20 times as expensive, and the budget is the same.
Try it: how many times more gradient computations does the 20-step attack use per image than FGSM? Answer: 20 times, because .
Interpretation: FGSM makes a full robustness sweep affordable. The iterative attack is more expensive, and the cost is the reason teams often use FGSM first and the stronger attack later.
Common mistakes and tradeoffs
It is tempting to think FGSM is the strongest attack. In fact it takes one linear step from the starting point, so it can miss an adversarial example that a few smaller steps would find. The iterative method and projected gradient descent are usually stronger. A model that resists FGSM only may still fail a stronger attack, and a model that looks robust to FGSM because its gradients are flat at the starting point is a case of gradient masking.
It is tempting to think FGSM needs the score, and that it pushes the answer toward a chosen class. In fact it uses the loss with the true label, so it increases the loss on the correct answer. A targeted variant lowers the loss for a chosen wrong label instead, but the basic method is untargeted.
It is tempting to think a per-value change of a few units is always invisible. In fact that depends on what the values are. On image pixels with a budget of about 2/255, the change is hard to see. On text, tabular or sensor data, the same numerical budget can change the meaning of the input completely, so the budget must be chosen with the domain in mind.
It is tempting to think a model that passes FGSM at one budget is robust. In fact one attack at one budget is weak evidence. A robust model should hold up across budgets and against several attacks, and the strength of the attack should be reported.
Use FGSM when you need a cheap attack: a quick check after training, a baseline in a paper, or a source of attacked examples during training. Do not use it as a guarantee. When the answer must be certified, use certified robustness methods, which give a proof that no change inside the budget flips the label. When the gradient is not available, attacks based on queries or on transfer from a substitute model are needed instead.
History
In 2013 Szegedy and colleagues showed that carefully computed, tiny changes to images could make deep networks misclassify them, and that the changed images often fooled other networks as well. Their attack solved an optimisation problem for each input, which was slow.
In 2014 Goodfellow, Shlens and Szegedy published Explaining and Harnessing Adversarial Examples. They proposed the single gradient step that became FGSM, which is fast enough to generate many examples. They also argued that the attack works because many models behave almost linearly in high dimensions, so a small change on every value adds up. Their paper made it practical to study the problem and to train on attacked examples, which is the basis of Adversarial Training.
In 2016 Kurakin, Goodfellow and Bengio showed that attacks of this kind can work on images taken by a camera, and they used an iterative version of the method. In 2017 Madry and colleagues framed robust training as a minimax problem and used projected gradient descent, the iterative method with projection onto the budget, as a strong attack and as the training signal. Both results made iterative attacks the standard stronger test that FGSM is compared against.
Recap
- FGSM moves every input value by the budget , in the direction that increases the loss, using the sign of the gradient.
- For a linear score, the sign rule is the exact best move inside the box, and its gain is .
- The gradient of a logistic model's loss with respect to the input is .
- FGSM costs one gradient per input, which makes it a fast robustness check and a source of attacked training examples.
- It is a single step, so stronger iterative attacks can find examples it misses.
- Read next: Adversarial Training for the main use of FGSM in training, and Explaining and Harnessing Adversarial Examples for the original paper.