Search

Search papers and lessons

Fast Gradient Sign Method

Level 4 · 12 min read

The fast gradient sign method changes every input value by a fixed small amount in the direction that most increases the model's loss, using one gradient computation to make an adversarial example.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Discrete math
  • Neural network basics
  • Linear algebra
  • Losses & regularization
  • ReadReady to readNot yet

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 p=σ(w⋅x+b)p = \sigma(w \cdot x + b), where σ\sigma is the Sigmoid Function. Let w=(2,−1)w = (2, -1) and b=0b = 0. For a true label y=1y = 1, the loss is the Cross-Entropy Loss, L=−ln⁡pL = -\ln p.

  1. Start with a correct input. Take x=(1,1)x = (1, 1). The score is z=2⋅1−1⋅1=1z = 2 \cdot 1 - 1 \cdot 1 = 1, so p=σ(1)≈0.731p = \sigma(1) \approx 0.731. The loss is −ln⁡0.731≈0.31-\ln 0.731 \approx 0.31. Check: pp is above one half, so the answer is correct.
  2. Set a budget. Each input value may change by at most ε=0.5\varepsilon = 0.5, in either direction. Check: the largest change to any one value is 0.5.
  3. Compute the Gradient of the loss with respect to the input. The gradient is (p−y)⋅w=(0.731−1)(2,−1)≈(−0.54,0.27)(p - y) \cdot w = (0.731 - 1)(2, -1) \approx (-0.54, 0.27). Check: the first entry is negative, so lowering the first input value raises the loss.
  4. Take the sign. The sign of each entry is (−1,+1)(-1, +1). Only the direction matters, not the size of each gradient entry.
  5. Move each value by the budget. xadv=x+ε⋅sign(∇xL)=(1−0.5,1+0.5)=(0.5,1.5)x_{\text{adv}} = x + \varepsilon \cdot \mathrm{sign}(\nabla_x L) = (1 - 0.5, 1 + 0.5) = (0.5, 1.5). Check: each value moved by exactly 0.5.
  6. Recompute. The score is z=2⋅0.5−1.5=−0.5z = 2 \cdot 0.5 - 1.5 = -0.5, so p=σ(−0.5)≈0.378p = \sigma(-0.5) \approx 0.378. 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.

00.20.40.60.8100.511.52budget εloss L
  • loss on the attacked input
For this model, the loss rises as the budget grows. At ε = 1/3 the score is zero, the probability is one half, and the loss is ln 2 ≈ 0.69.

The math

Definition: Fast gradient sign method
Given a model with parameters θ\theta, an input xx with true label yy, a loss L(θ,x,y)L(\theta, x, y), and a budget ε>0\varepsilon > 0, the FGSM adversarial example is xadv=x+ε sign(∇xL(θ,x,y))x_{\text{adv}} = x + \varepsilon \, \mathrm{sign}\big(\nabla_x L(\theta, x, y)\big), where the sign is applied to each entry of the gradient separately.

Step 1 in symbols: the gradient of the loss

For the logistic classifier, the Gradient is built from the Chain Rule. Let z=w⋅x+bz = w \cdot x + b and p=σ(z)p = \sigma(z). The loss for a label y∈{0,1}y \in \{0, 1\} is L=−[yln⁡p+(1−y)ln⁡(1−p)]L = -\big[y \ln p + (1 - y)\ln(1 - p)\big]. By the Chain Rule, since ∂L∂z=p−y\frac{\partial L}{\partial z} = p - y and ∂z∂x=w\frac{\partial z}{\partial x} = w,

∇xL=(p−y) w\nabla_x L = (p - y)\, w

Check against step 3: (0.731−1)(2,−1)=(−0.54,0.27)(0.731 - 1)(2, -1) = (-0.54, 0.27).

Step 2 in symbols: the budget

The allowed changes form a box: ∥δ∥∞≤ε\|\delta\|_\infty \leq \varepsilon, which means ∣δi∣≤ε|\delta_i| \leq \varepsilon for every entry ii (see Vector Norm).

Step 3 in symbols: why the sign is the best move

A small change δ\delta changes the loss by about ∇xL⋅δ=∑igiδi\nabla_x L \cdot \delta = \sum_i g_i \delta_i, where g=∇xLg = \nabla_x L. To make this as large as possible inside the box, each term must be as large as possible on its own. Each term giδig_i \delta_i is largest when δi=ε sign(gi)\delta_i = \varepsilon \,\mathrm{sign}(g_i), which gives ε∣gi∣\varepsilon |g_i|. Adding the terms gives the total gain

∑iε∣gi∣=ε∥g∥1\sum_i \varepsilon |g_i| = \varepsilon \|g\|_1

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 z′=w⋅xadv+bz' = w \cdot x_{\text{adv}} + b. Because sign(∇xL)=(−1,+1)\mathrm{sign}(\nabla_x L) = (-1, +1), we have xadv=(1−ε,1+ε)x_{\text{adv}} = (1 - \varepsilon, 1 + \varepsilon), so z′=2(1−ε)−(1+ε)=1−3εz' = 2(1 - \varepsilon) - (1 + \varepsilon) = 1 - 3\varepsilon. The label flips when z′<0z' < 0, which happens when ε>13\varepsilon > \frac13. The score drops by ε∥w∥1=3ε\varepsilon \|w\|_1 = 3\varepsilon, where ∥w∥1=∣2∣+∣−1∣=3\|w\|_1 = |2| + |{-1}| = 3 is the L1 norm of the weights. This is the same factor of 3 that appears in z′=1−3εz' = 1 - 3\varepsilon.

Iterative variant

Repeating the step with a smaller step size α\alpha and projecting back into the box gives the iterative method:

x(k+1)=clip[x−ε, x+ε](x(k)+α sign(∇xL(θ,x(k),y)))x^{(k+1)} = \mathrm{clip}_{[x - \varepsilon,\, x + \varepsilon]}\Big(x^{(k)} + \alpha\, \mathrm{sign}\big(\nabla_x L(\theta, x^{(k)}, y)\big)\Big)

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

Example
Example 1: one step, traced in full

Use the model w=(2,−1)w = (2, -1), b=0b = 0, with x=(1,1)x = (1, 1), y=1y = 1 and ε=0.5\varepsilon = 0.5.

  1. Score: z=2−1=1z = 2 - 1 = 1. Probability: p=σ(1)=11+e−1≈0.7311p = \sigma(1) = \frac{1}{1 + e^{-1}} \approx 0.7311. Loss: −ln⁡(0.7311)≈0.3133-\ln(0.7311) \approx 0.3133.
  2. Gradient: (p−1)(2,−1)=(−0.2689)(2,−1)=(−0.5378,0.2689)(p - 1)(2, -1) = (-0.2689)(2, -1) = (-0.5378, 0.2689).
  3. Sign: (−1,+1)(-1, +1).
  4. Adversarial input: xadv=(1,1)+0.5⋅(−1,+1)=(0.5,1.5)x_{\text{adv}} = (1, 1) + 0.5 \cdot (-1, +1) = (0.5, 1.5).
  5. Score: z′=2⋅0.5−1.5=−0.5z' = 2 \cdot 0.5 - 1.5 = -0.5. Probability: σ(−0.5)=11+e0.5≈0.3775\sigma(-0.5) = \frac{1}{1 + e^{0.5}} \approx 0.3775.
  6. Loss: −ln⁡(0.3775)≈0.9741-\ln(0.3775) \approx 0.9741.
  7. 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.

Example
Example 2: how the budget decides the flip

Keep the same model, input and label. The attacked input for a budget ε\varepsilon is (1−ε,1+ε)(1 - \varepsilon, 1 + \varepsilon), and its score is z′=1−3εz' = 1 - 3\varepsilon.

  1. Budget ε=0.1\varepsilon = 0.1: the input is (0.9,1.1)(0.9, 1.1), the score is z′=0.7z' = 0.7, and the probability is σ(0.7)≈0.668\sigma(0.7) \approx 0.668. The label is still positive.
  2. Budget ε=0.25\varepsilon = 0.25: the input is (0.75,1.25)(0.75, 1.25), the score is z′=0.25z' = 0.25, and the probability is σ(0.25)≈0.562\sigma(0.25) \approx 0.562. Still positive, but closer to the boundary.
  3. Budget ε=0.5\varepsilon = 0.5: the input is (0.5,1.5)(0.5, 1.5), the score is z′=−0.5z' = -0.5, and the probability is σ(−0.5)≈0.378\sigma(-0.5) \approx 0.378. The label has flipped.
  4. Check: the flip happens at ε=1/3\varepsilon = 1/3, where z′=0z' = 0. Every budget above that flips the label, and every budget below it does not.
  5. Try it: at what budget does the loss first reach ln⁡2≈0.693\ln 2 \approx 0.693? Answer: at ε=1/3\varepsilon = 1/3, because there p=0.5p = 0.5 and −ln⁡0.5=ln⁡2-\ln 0.5 = \ln 2.

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.

Example
Example 3: the cost of the attack on a real test set

Suppose a team tests a vision model on 10,000 images. Each image has 224×224×3=150 528224 \times 224 \times 3 = 150\,528 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.

  1. One FGSM pass over the test set needs one gradient per image: 10,00010{,}000 gradients.
  2. Time: 10,000×0.05=50010{,}000 \times 0.05 = 500 seconds, about 8 minutes.
  3. A 20-step iterative attack needs 20 gradients per image: 200,000200{,}000 gradients.
  4. Time: 200,000×0.05=10,000200{,}000 \times 0.05 = 10{,}000 seconds, about 2.8 hours.
  5. The budget for brightness values scaled to the range 0 to 1 is often written as ε=2/255≈0.0078\varepsilon = 2/255 \approx 0.0078, two brightness levels out of 255. Each value changes by less than one percent of its range.
  6. 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 20/1=2020 / 1 = 20.

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 ε\varepsilon, 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 ε∥g∥1\varepsilon \|g\|_1.
  • The gradient of a logistic model's loss with respect to the input is (p−y) w(p - y)\,w.
  • 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.

Check yourself

QuestionWhy does FGSM use only the sign of each gradient entry rather than the entry itself?Show answer
Inside the budget box, the best change to each value is the full budget in the direction that increases the loss, whatever the size of the gradient entry. The size of the entry does not change where the value should move to, so only the sign is needed. For a linear model, the total gain is ε∥g∥1\varepsilon \|g\|_1, which is attained exactly by the sign rule.
QuestionA logistic model has w=(2,−1)w = (2, -1), b=0b = 0, and input x=(1,1)x = (1, 1) with label y=1y = 1. With budget ε=0.2\varepsilon = 0.2, what is the adversarial input and its probability?Show answer
The gradient is (p−1)(2,−1)(p - 1)(2, -1) with p=σ(1)p = \sigma(1), so its sign is (−1,+1)(-1, +1). The adversarial input is (0.8,1.2)(0.8, 1.2). Its score is 1.6−1.2=0.41.6 - 1.2 = 0.4, so the probability is σ(0.4)=11+e−0.4≈0.599\sigma(0.4) = \frac{1}{1 + e^{-0.4}} \approx 0.599. The label stays positive at this budget.
QuestionA team reports that its model keeps 95 percent accuracy under FGSM at one small budget, and concludes it is robust. What is wrong with this conclusion?Show answer
One step at one budget is weak evidence. A stronger iterative attack, or the same attack at larger budgets, may find examples that FGSM misses. The model may also be resisting FGSM because its gradient at the starting point is uninformative, which is gradient masking. A fair test uses several budgets and at least one stronger attack.
QuestionIn an application, each test input needs an attack, and the team can afford only a fixed number of backward passes. Why might FGSM be preferred even though iterative attacks are stronger?Show answer
FGSM needs one backward pass per input, while a kk-step iterative attack needs kk. Under a fixed compute budget, FGSM can cover far more inputs or budgets, which can give a more complete picture of the model's weak points than a few very strong attacks on a small sample.

Sources