Search

Search papers and lessons

Explaining and Harnessing Adversarial Examples

Ian J. Goodfellow, Jonathon Shlens, Christian Szegedy · 2014

Goodfellow and colleagues argue that adversarial examples come from models being too linear, and they give the fast gradient sign method, a one-step attack that also makes adversarial training practical.

On this page

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

TL;DR

Neural networks, and many other classifiers, can be fooled by an input changed by an amount too small for a person to notice. Earlier work blamed strong nonlinearity or overfitting. This paper argues the main cause is that the models are too linear: a sum over many small input changes, all pushed in the harmful direction, can move the output a long way. The authors also give the fast gradient sign method (FGSM), a single-step way to build such inputs, and show that training on them regularizes a maxout network on MNIST.

Builds on: FGSM, Logistic Regression · Leads to: Adversarial Training

Why it mattered

The paper turned adversarial examples from a curiosity into a standard research problem. FGSM became the baseline attack that new defences are tested against. Its one-step recipe needs only one gradient, so it runs in the time of one backward pass, which made it cheap enough to generate new examples during training. The paper has been cited more than 20,000 times according to Semantic Scholar. Its idea of adversarial training, mixing clean and perturbed inputs in each batch, is still a common baseline for making image classifiers robust, and its linear explanation is still the starting point for many follow-up analyses.

The problem

Before this paper, the observations from Szegedy and colleagues were clear. A trained network can be pushed to a wrong answer with a change to the input that a person cannot see. The same perturbed input often fools several different networks. Shallow softmax models were fooled too. The explanations on offer were nonlinearity, overfitting and insufficient regularization.

Those explanations are hard to test and do not account for why the same wrong answer appears across models. The paper's question is simple: if a model is built from weighted sums, how much can a tiny change to the input move the score? Answering that needs only the ideas of dot products and norms (the size of a perturbation), and the claim is that this is already enough.

How it works

The paper's argument has four steps. Trace them on one tiny linear model first.

  1. Take a linear score a=w⊤xa = \mathbf{w}^\top \mathbf{x}, where w\mathbf{w} is the weight vector and x\mathbf{x} the input. Use w=(1, −2, 3)\mathbf{w} = (1,\ -2,\ 3) and x=(0.5, 0.25, 1)\mathbf{x} = (0.5,\ 0.25,\ 1). Then a=0.5−0.5+3=3.0a = 0.5 - 0.5 + 3 = 3.0. Check: each product is 1×0.51 \times 0.5, −2×0.25-2 \times 0.25 and 3×13 \times 1.
  2. Allow a change η\boldsymbol{\eta} to each input value, but keep every change within a small budget ϵ\epsilon: ∣ηi∣≤ϵ|\eta_i| \le \epsilon. The best choice for growing the score is to push each input in the direction of its weight's sign: η=ϵ sign⁡(w)\boldsymbol{\eta} = \epsilon\,\operatorname{sign}(\mathbf{w}). With ϵ=0.1\epsilon = 0.1, η=(0.1, −0.1, 0.1)\boldsymbol{\eta} = (0.1,\ -0.1,\ 0.1). Check: the signs of w\mathbf{w} are (+,−,+)(+,-,+).
  3. Compute the new score: w⊤(x+η)=3.0+0.6=3.6\mathbf{w}^\top(\mathbf{x} + \boldsymbol{\eta}) = 3.0 + 0.6 = 3.6. The change, 0.60.6, equals ϵ∥w∥1=0.1×(1+2+3)\epsilon \|\mathbf{w}\|_1 = 0.1 \times (1 + 2 + 3). Check: the three contributions are 0.10.1, 0.20.2 and 0.30.3.
  4. Now scale up. The paper's point is that the change is ϵ\epsilon times the average weight size mm, times the number of inputs nn. Here m=2m = 2 and n=3n = 3, giving 0.1×2×3=0.60.1 \times 2 \times 3 = 0.6. Each input moved by only 0.10.1, but if the average weight size stays 2 across a thousand inputs, the same budget moves the score by 0.1×2×1000=2000.1 \times 2 \times 1000 = 200.

The growth in step 4 is linear in nn, as the plot shows. A budget that looks harmless per input adds up across many inputs.

02004006008001000050100150200number of inputs nchange in score
  • score change, eps = 0.1, mean weight 2
Each input moves by at most 0.1, yet the score change grows in proportion to n. The two marked points are the three-input example and the thousand-input case.

The paper then makes this a method. Let J(θ,x,y)J(\boldsymbol{\theta}, \mathbf{x}, y) be the training cost. Approximate it near the input by a linear function and choose the perturbation that increases it most within the budget. That perturbation is the fast gradient sign method, and it is one step using one gradient, which backpropagation computes.

flowchart LR
  A["Input x and true label y"] --> B["Compute gradient of cost with respect to x"]
  B --> C["Take sign of each entry"]
  C --> D["Multiply by budget epsilon"]
  D --> E["Add to x to get adversarial input"]
  E --> F["Model output changes"]

The math

The linear argument

For a model with weight vector w\mathbf{w} and an input x~=x+η\tilde{\mathbf{x}} = \mathbf{x} + \boldsymbol{\eta}, the score is

w⊤x~=w⊤x+w⊤η.\mathbf{w}^\top \tilde{\mathbf{x}} = \mathbf{w}^\top \mathbf{x} + \mathbf{w}^\top \boldsymbol{\eta}.

The perturbation adds w⊤η\mathbf{w}^\top \boldsymbol{\eta} to the score. Under the constraint ∥η∥∞≤ϵ\|\boldsymbol{\eta}\|_\infty \le \epsilon, the largest possible increase is reached at η=ϵ sign⁡(w)\boldsymbol{\eta} = \epsilon\,\operatorname{sign}(\mathbf{w}), and it equals ϵ∥w∥1\epsilon \|\mathbf{w}\|_1. If each weight has average magnitude mm and there are nn of them, this is ϵmn\epsilon m n. This is step 4 in symbols, and it is the source of the paper's high-dimension argument.

Step 2 in symbols: the sign rule

Step 2 used the sign of the weights. The max-norm budget ∥η∥∞≤ϵ\|\boldsymbol{\eta}\|_\infty \le \epsilon says each entry is limited to ±ϵ\pm \epsilon, so the best entry is always at the edge of that box, with the sign of whatever direction helps. The sum ∑iwiηi\sum_i w_i \eta_i is largest when each ηi\eta_i has the sign of wiw_i. That gives η=ϵ sign⁡(w)\boldsymbol{\eta} = \epsilon\,\operatorname{sign}(\mathbf{w}).

The general method in symbols

For a nonlinear model, the same idea works after a linear approximation of the cost. The gradient ∇xJ\nabla_{\mathbf{x}} J plays the role of w\mathbf{w}, and the paper's rule is

η=ϵ sign⁡ ⁣(∇xJ(θ,x,y)).\boldsymbol{\eta} = \epsilon\,\operatorname{sign}\!\left(\nabla_{\mathbf{x}} J(\boldsymbol{\theta}, \mathbf{x}, y)\right).

Each entry of the gradient says how the cost changes when that input value rises. The sign turns each one into a step of size ϵ\epsilon in the uphill direction. Only the sign is kept, so the size of the gradient does not matter. The step is one backward pass, which is why the paper calls it fast.

The adversarial training objective

The paper trains with a mix of clean and perturbed costs:

J~(θ,x,y)=α J(θ,x,y)+(1−α) J ⁣(θ,x+ϵ sign⁡(∇xJ(θ,x,y)),y),\tilde{J}(\boldsymbol{\theta}, \mathbf{x}, y) = \alpha\, J(\boldsymbol{\theta}, \mathbf{x}, y) + (1 - \alpha)\, J\!\left(\boldsymbol{\theta}, \mathbf{x} + \epsilon\,\operatorname{sign}(\nabla_{\mathbf{x}} J(\boldsymbol{\theta}, \mathbf{x}, y)), y\right),

with α=0.5\alpha = 0.5 in all of the paper's experiments. Because the perturbation is recomputed from the current model at every step, the supply of adversarial examples keeps pace with the model as it learns.

Logistic regression, where the method is exact

For logistic regression with labels y∈{−1,1}y \in \{-1, 1\}, the worst-case perturbed cost has a closed form. Take a positive example, y=1y = 1: the sign of the gradient is −sign⁡(w)-\operatorname{sign}(\mathbf{w}), and w⊤sign⁡(w)=∥w∥1\mathbf{w}^\top \operatorname{sign}(\mathbf{w}) = \|\mathbf{w}\|_1, so the adversarial cost is

E ζ ⁣(ϵ∥w∥1−w⊤x−b),\mathbb{E}\, \zeta\!\left(\epsilon \|\mathbf{w}\|_1 - \mathbf{w}^\top \mathbf{x} - b\right),

where ζ\zeta is the softplus function. The paper writes the general case with yy multiplying the whole bracket, which gives the same result for y=1y = 1. The term ϵ∥w∥1\epsilon \|\mathbf{w}\|_1 looks like an L1 penalty, but the paper points out that it is subtracted from the activation rather than added to the cost, so it can fade once the model is confident. That makes it a different form of regularization from weight decay.

Results

The paper's numbers come from three kinds of experiment.

Attacks on shallow and deep models. With ϵ=0.25\epsilon = 0.25 on MNIST pixel values in [0,1][0, 1], FGSM made a shallow softmax classifier wrong on 99.9% of test images, with an average confidence of 79.3% in its wrong answers. On ImageNet with GoogLeNet, a change of ϵ=0.007\epsilon = 0.007, the size of the smallest bit of an 8-bit image, changed the predicted class of the image shown in Figure 1 of the paper. The attack needs no search, so it works on any differentiable model.

Adversarial training as regularization. A maxout network with dropout had a test error of 0.94% on MNIST. Training with the adversarial objective brought it to 0.84%. A larger version, with 1600 units per layer, had 1.14% test error without adversarial training and overfit slightly. The authors enlarged the model because it could not reach zero error on the adversarial examples in its training set. On adversarial examples, the same kind of model had an error rate of 89.4% without adversarial training and 17.9% with it. The paper's control experiments with L1 weight decay and with added noise did not produce the same effect.

Limits of the explanation. A shallow RBF network had an error rate of 55.4% on FGSM examples at ϵ=0.25\epsilon = 0.25, but its confidence on those mistakes was only 1.2%, against 60.6% on clean examples. The authors read this as a sign that nonlinear units with local support fail more gracefully. An ensemble of twelve maxout networks still had a 91.1% error rate on adversarial examples built against the whole ensemble at ϵ=0.25\epsilon = 0.25. The main experiments use MNIST, CIFAR-10 and one ImageNet example, and the regularization result is shown on one network, so the claim is supported but not tested widely in this paper.

Reading guide

The paper has nine short sections after the introduction. Read them with these lessons nearby.

  • Section 1, Introduction, and Section 2, Related work. You need Adversarial Examples for the phenomenon, and Dot Product to follow why a sum over inputs can grow.
  • Section 3, The linear explanation. This is the core argument. You need Dot Product, Vector Norm for the max-norm budget, and the step-by-step example above.
  • Section 4, Linear perturbation of nonlinear models. The fast method itself. You need Gradient and Fast Gradient Sign Method, since the paper's rule is the one defined there.
  • Section 5, Adversarial training of linear models versus weight decay. Read it with Logistic Regression, where the method is exact.
  • Section 6, Adversarial training of deep networks. You need Universal Approximation Theorem for the claim that a hidden layer can represent resistant functions, and Dropout for the baseline the paper beats.
  • Sections 7 to 10. These compare model capacity, the transfer of adversarial examples between models, and the alternative hypotheses. You need Radial Basis Function and Ensemble Learning to follow the RBF and ensemble tests.

Check yourself

QuestionIn one sentence, what does the paper claim causes adversarial examples, and what is its evidence for the claim?Show answer

The paper claims that the models are too linear. A linear score can be pushed far by many small input changes that all point the harmful way, and the amount grows with the number of inputs. The evidence is the analysis of linear models, the exact result for logistic regression, and the fact that a simple linear-style attack fools many trained models.

QuestionA linear model has weights w=(2, −1, 0.5)\mathbf{w} = (2,\ -1,\ 0.5) and an input budget ϵ=0.1\epsilon = 0.1. By how much can the score change, at most, under the max-norm budget?Show answer

The largest change is ϵ∥w∥1=0.1×(2+1+0.5)=0.1×3.5=0.35\epsilon \|\mathbf{w}\|_1 = 0.1 \times (2 + 1 + 0.5) = 0.1 \times 3.5 = 0.35. The best perturbation is η=0.1 sign⁡(w)=(0.1, −0.1, 0.1)\boldsymbol{\eta} = 0.1\,\operatorname{sign}(\mathbf{w}) = (0.1,\ -0.1,\ 0.1), and its effect on the score is 0.2+0.1+0.05=0.350.2 + 0.1 + 0.05 = 0.35.

QuestionWhy does the paper keep only the sign of the gradient instead of the gradient itself?Show answer

The max-norm budget limits each input entry to plus or minus ϵ\epsilon, so the best move for each entry is to the edge of the box in the direction that increases the cost. Only the direction matters for that choice, so the sign is enough and the size of each gradient entry is discarded. This keeps the perturbation at a known size, which makes it easy to compare across inputs.

QuestionA team adds FGSM adversarial training to a classifier and sees its clean test error rise slightly while its FGSM error falls sharply. Using the paper's setup, what could explain this, and what would you check next?Show answer

The paper mixed clean and adversarial costs with a fixed weight of 0.5, and it did not tune that weight. A model with limited capacity may spend its effort on the perturbed inputs at the expense of clean ones, which is what the paper's enlargement of its network hints at. Check the mixing weight, check whether the model has enough units, and test on attacks other than FGSM, because robustness to one attack does not imply robustness to another.

Sources

Read the full paper

Ian J. Goodfellow, Jonathon Shlens, Christian Szegedy, 2014. arXiv:1412.6572