Search

Search papers and lessons

Adversarial Examples

Level 4 · 12 min read

An adversarial example is an input changed by a small, deliberately chosen amount that flips a model's answer, even though the change is too small to matter to a person.

On this page

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

TL;DR

An adversarial example is an input that has been changed on purpose, by an amount small enough that a person would see the same thing, so that a trained model gives a different and wrong answer. The change is not random: it is chosen by following the model's own gradient, the direction in which its score moves fastest. The result shows that a model can be accurate on ordinary inputs and still be fragile in a narrow, exploitable way.

Builds on: Gradient, Feedforward Neural Network · Leads to: Fast Gradient Sign Method, Adversarial Training

Where it's used

Attacks on image classifiers. In 2013 Szegedy and colleagues showed that a small change to a photo, too faint for a person to notice, could switch a well-trained image network, such as a Convolutional Neural Network, to a different label. The finding began the study of adversarial examples and is the reason many image models are now tested against such changes.

Spam and malware filters. A spam filter scores messages by their words. A sender who knows the scoring can change a few words or add invisible characters that the filter weighs wrongly, while the message reads the same to the person it is sent to. The same idea applies to malware detectors that look at the features of a file.

Road-sign and vision systems. Stickers or paint on a traffic sign, placed where a camera's model is sensitive, can cause a vision system to read the sign wrongly. Researchers have shown this in controlled experiments. The risk matters most where a wrong label leads to a physical action, such as braking or steering.

How it works

Take a tiny Linear Classifier with two inputs. It gives a score s(x)=w⋅x+bs(x) = w \cdot x + b, the Dot Product of the weights ww and the input xx plus a bias bb, and it predicts the positive class when s(x)>0s(x) > 0. Let w=(1,−2)w = (1, -2) and b=0b = 0.

  1. Start with a correct input. Take x=(3,1)x = (3, 1). The score is 1×3+(−2)×1=11 \times 3 + (-2) \times 1 = 1, which is positive, so the classifier says positive, which is correct. Check: the score is above zero.
  2. Set a budget. Allow each input value to change by at most ε=0.5\varepsilon = 0.5 in either direction. A change this size is what the attacker is allowed to make. Check: the largest single change is 0.5.
  3. Find the direction that hurts most. The Gradient of the score with respect to the input is w=(1,−2)w = (1, -2). Moving against it decreases the score fastest. For each input value, the best move is the opposite of the sign of its weight: the first value goes down (weight 1 is positive), the second goes up (weight −2 is negative, so moving against it means increasing the value).
  4. Make the change. New input: x′=(3−0.5,1+0.5)=(2.5,1.5)x' = (3 - 0.5, 1 + 0.5) = (2.5, 1.5). Check: each value moved by exactly 0.5, within the budget.
  5. Recompute the score. s(x′)=2.5−2×1.5=2.5−3=−0.5s(x') = 2.5 - 2 \times 1.5 = 2.5 - 3 = -0.5, which is negative. The classifier now says negative. Check: the answer has flipped.
  6. Read the result. The same input, changed by at most half a unit per value, has a new answer. In a real image each value is one brightness level, and the same logic works with changes of about one level out of 255, which a person cannot see.

The picture to keep: the classifier's boundary is a line, and the attacker moves the input across it by the shortest route that the budget allows.

00.20.40.60.81−1−0.500.511.5budget εscore s(x')
  • score after a change of size x
The score falls by 3 for each unit of budget, because the weights have total size 1 + 2 = 3. It crosses zero at ε = 1/3, so the answer flips at that budget.

The math

Definition: Adversarial example
Let ff be a trained classifier, xx an input with correct label yy, and ε>0\varepsilon > 0 a budget. An adversarial example is an input x′=x+δx' = x + \delta with ∥δ∥∞≤ε\|\delta\|_\infty \leq \varepsilon and f(x′)≠yf(x') \neq y. The ∞\infty-norm ∥δ∥∞=max⁡i∣δi∣\|\delta\|_\infty = \max_i |\delta_i| is the largest change to any single value (see Vector Norm).

Step 2 in symbols: the budget

The set of allowed changes is the box ∥δ∥∞≤ε\|\delta\|_\infty \leq \varepsilon. Every value may move by up to ε\varepsilon, all in one step. In the example, ε=0.5\varepsilon = 0.5 allows any δ\delta with each ∣δi∣≤0.5|\delta_i| \leq 0.5.

Step 3 in symbols: the gradient

For the linear score, ∇xs(x)=w\nabla_x s(x) = w. For a Feedforward Neural Network, the same quantity ∇xs(x)\nabla_x s(x) is computed by backpropagation: the Chain Rule carried back from the output to the input. It tells the attacker which input values most change the score, and in which direction.

Step 4 in symbols: the change

Using a first-order approximation, a small change δ\delta alters the score by about ∇xs(x)⋅δ\nabla_x s(x) \cdot \delta. To lower the score as much as possible within the box, choose

δ=−ε sign(∇xs(x))\delta = -\varepsilon \,\mathrm{sign}\big(\nabla_x s(x)\big)

where sign\mathrm{sign} takes the sign of each entry. For the linear example, sign(w)=(1,−1)\mathrm{sign}(w) = (1, -1), so δ=(−0.5,0.5)\delta = (-0.5, 0.5), which matches step 4. The score changes by w⋅δ=(1)(−0.5)+(−2)(0.5)=−1.5w \cdot \delta = (1)(-0.5) + (-2)(0.5) = -1.5, which equals −ε∥w∥1=−0.5×3-\varepsilon \|w\|_1 = -0.5 \times 3. Check: the baseline is s(x)=1s(x) = 1, so the new score is 1−1.5=−0.51 - 1.5 = -0.5, the same as step 5.

Step 6 in symbols: why many small changes add up

Suppose an input has nn values and the model's weights have typical size ∣wi∣=a|w_i| = a. The attacker moves every value by ε\varepsilon in the best direction, so the score changes by about εna\varepsilon n a. The effect grows with nn, even when ε\varepsilon is tiny.

For example, take n=1000n = 1000, a=0.1a = 0.1 and ε=0.01\varepsilon = 0.01. The score changes by 0.01×1000×0.1=10.01 \times 1000 \times 0.1 = 1. A baseline score of 0.5 becomes 0.5−1=−0.50.5 - 1 = -0.5. Each input value moved by one hundredth, but together the moves flipped the answer.

Worked examples

Example
Example 1: one value, one weight
  1. A classifier scores a single input value as s(x)=2xs(x) = 2x, and predicts positive when s>0s > 0. The input is x=0.4x = 0.4, so s=0.8s = 0.8, which is correct.
  2. Try a budget ε=0.1\varepsilon = 0.1. The gradient is 22, its sign is +1+1, so the change is δ=−0.1\delta = -0.1.
  3. New input: x′=0.4−0.1=0.3x' = 0.4 - 0.1 = 0.3. Score: 2×0.3=0.62 \times 0.3 = 0.6, still positive. The budget was too small.
  4. Try ε=0.5\varepsilon = 0.5. The change is δ=−0.5\delta = -0.5, and x′=−0.1x' = -0.1.
  5. Score: 2×(−0.1)=−0.22 \times (-0.1) = -0.2, which is negative. The answer has flipped.
  6. Check: the change in score is ε×∣2∣=0.5×2=1.0\varepsilon \times |2| = 0.5 \times 2 = 1.0, and 0.8−1.0=−0.20.8 - 1.0 = -0.2.

Interpretation: a budget that is too small leaves the answer alone. A large enough budget flips it, and the weight sets how much each unit of budget is worth.

Example
Example 2: three values with mixed weights
  1. The classifier has weights w=(2,−1,0.5)w = (2, -1, 0.5) and bias b=−1b = -1, so s(x)=2x1−x2+0.5x3−1s(x) = 2x_1 - x_2 + 0.5x_3 - 1.
  2. The input is x=(2,1,1)x = (2, 1, 1). Score: 4−1+0.5−1=2.54 - 1 + 0.5 - 1 = 2.5, which is positive and correct.
  3. Use budget ε=0.5\varepsilon = 0.5. The sign of ww is (+1,−1,+1)(+1, -1, +1), so δ=−0.5×(+1,−1,+1)=(−0.5,0.5,−0.5)\delta = -0.5 \times (+1, -1, +1) = (-0.5, 0.5, -0.5).
  4. New input: x′=(1.5,1.5,0.5)x' = (1.5, 1.5, 0.5). Score: 3−1.5+0.25−1=0.753 - 1.5 + 0.25 - 1 = 0.75, still positive.
  5. The change in score is ε∥w∥1=0.5×(2+1+0.5)=1.75\varepsilon \|w\|_1 = 0.5 \times (2 + 1 + 0.5) = 1.75, and 2.5−1.75=0.752.5 - 1.75 = 0.75. This matches step 4.
  6. Increase the budget to ε=1.0\varepsilon = 1.0. Then δ=(−1,1,−1)\delta = (-1, 1, -1) and x′=(1,2,0)x' = (1, 2, 0).
  7. Score: 2−2+0−1=−12 - 2 + 0 - 1 = -1, which is negative. The answer has flipped.
  8. Try it: is x′=(1,2,0)x' = (1, 2, 0) a fair change? Each value moved by at most 1, which is the budget. Answer: yes, it is within the budget; whether a person would notice depends on what the values mean.

Interpretation: the attacker gains the most where the weights are largest, and the flipping budget depends on the total size of the weights, ∥w∥1\|w\|_1, not on any single one.

Example
Example 3: an image-sized input
  1. Take a colour image of 224×224×3224 \times 224 \times 3 values, so n=150 528n = 150\,528 values, each stored as a brightness from 0 to 255 and scaled to the range 0 to 1.
  2. Suppose a linear model over the pixels has typical weight size a=0.001a = 0.001, and the baseline score is 0.50.5 (positive, correct).
  3. Use budget ε=2/255\varepsilon = 2/255, which is two brightness levels out of 255.
  4. The score change is εna=(2/255)×150 528×0.001\varepsilon n a = (2/255) \times 150\,528 \times 0.001.
  5. Compute: 150 528×0.001=150.528150\,528 \times 0.001 = 150.528. Then 150.528×2/255=301.056/255≈1.18150.528 \times 2 / 255 = 301.056 / 255 \approx 1.18.
  6. New score: 0.5−1.18=−0.680.5 - 1.18 = -0.68, which is negative. The label flips.
  7. Each pixel changed by two brightness levels out of 255, which is hard to see on a screen.
  8. Try it: with ε=1/255\varepsilon = 1/255, what is the new score? Answer: the change is 150.528/255≈0.59150.528/255 \approx 0.59, and 0.5−0.59=−0.090.5 - 0.59 = -0.09. The label still flips, with a smaller budget.

Interpretation: high-dimensional inputs give an attacker many small moves that add up, so a change that is invisible per value can still be large in total.

Common mistakes and tradeoffs

It is tempting to think an adversarial example needs obvious noise. In fact the change is spread across every input value and is kept small per value. The attacker wins by aiming the change, not by making it large.

It is tempting to think random noise of the same size will do the same thing. In fact random noise points in a random direction, and its effect on the score mostly cancels. The attack works because the direction is set by the gradient, so the change is aligned with the most sensitive directions. Random noise of the same size usually leaves the answer unchanged.

It is tempting to think adversarial examples are a sign of a broken model, or of Overfitting. In fact they occur in models that generalise well on ordinary test data. Goodfellow and colleagues argued in 2014 that the main cause is the linear behaviour of many models in high dimensions, not nonlinear pockets or overfitting; this is a debated explanation, and the 2013 paper suggested other causes.

Defences exist, and each has a cost. Adversarial Training trains the model on perturbed inputs; it improves robustness to the attacks it was trained on, usually at some cost in accuracy on clean inputs and in training time. Input transformations and detectors are often defeated by an attacker who knows the defence. Robustness to one attack does not guarantee robustness to another.

Adversarial examples matter when someone can choose the input and the answer has consequences, such as spam filters, fraud checks, medical image triage or vehicle perception. They matter less for a model used only offline on data nobody can tamper with, although the fragility is still a signal about how the model works. Testing against an attack, such as Fast Gradient Sign Method, is a cheap first check.

History

In 2013 Szegedy and colleagues reported that small, carefully computed changes to images could cause deep networks to misclassify them, and that the changed images often fooled other networks too. Their paper, Intriguing properties of neural networks, made the phenomenon widely known. Their attack needed an optimisation at each input, which was slow.

In 2014 Goodfellow, Shlens and Szegedy published Explaining and Harnessing Adversarial Examples. They proposed a single gradient step, the Fast Gradient Sign Method, which is fast enough to generate many examples, and they argued that linearity in high dimensions explains why the attack works. Their paper made it practical to study the problem, and adversarial training became the standard defence in the following years.

The field has since grown into a large area of robustness research. It is still open: defences that resist strong attacks are hard to build and to evaluate, and a defence that looks good against one attack is often broken by a stronger one.

Recap

  • An adversarial example is an input changed by a small budget, chosen so that a trained model's answer changes.
  • The best direction for the change is set by the gradient of the score with respect to the input.
  • A change of size ε\varepsilon on each of nn values can move the score by about ε∥w∥1\varepsilon \|w\|_1, which grows with the number of values.
  • Random noise of the same size usually does not flip an answer, because it is not aligned with the gradient.
  • Defences such as Adversarial Training help against the attacks they were trained on, but they carry costs and are not a complete guarantee.
  • Read next: Fast Gradient Sign Method for the one-step attack, and Adversarial Training for the most common defence.

Check yourself

QuestionA linear classifier has score s(x)=3x1+x2−2s(x) = 3x_1 + x_2 - 2 and an input x=(1,1)x = (1, 1) with score 2. Using an ∞\infty-norm budget of ε=0.5\varepsilon = 0.5, what is the adversarial change and the new score?Show answer
The gradient is (3,1)(3, 1), its sign is (+1,+1)(+1, +1), so δ=(−0.5,−0.5)\delta = (-0.5, -0.5). The new input is (0.5,0.5)(0.5, 0.5), and the score is 1.5+0.5−2=01.5 + 0.5 - 2 = 0. The score change is ε∥w∥1=0.5×4=2\varepsilon \|w\|_1 = 0.5 \times 4 = 2, and 2−2=02 - 2 = 0, which is the boundary. A slightly larger budget would make the score negative.
QuestionWhy does random noise of size ε\varepsilon on each value rarely flip the label of a linear classifier, while the gradient-based change of the same size often does?Show answer
The gradient-based change moves each value in the direction that lowers the score, so its effects on the score add up to ε∥w∥1\varepsilon \|w\|_1. Random signs make the effects cancel on average, so the total change is much smaller. The attack's power comes from aligning the change with the gradient.
QuestionAn image has n=10 000n = 10\,000 values, the model's typical weight size is 0.0020.002, and the baseline score is 0.30.3. What is the largest ε\varepsilon that leaves the label unchanged?Show answer
The score change is εna=ε×10 000×0.002=20ε\varepsilon n a = \varepsilon \times 10\,000 \times 0.002 = 20\varepsilon. For the label to stay the same, 0.3−20ε>00.3 - 20\varepsilon > 0, so ε<0.3/20=0.015\varepsilon < 0.3/20 = 0.015. Any budget below 0.015 leaves the label unchanged in this linear model.
QuestionA company says its image model is safe because it has 99 percent accuracy on its test set. Why is this not evidence of robustness to adversarial examples?Show answer
Accuracy on the test set measures performance on ordinary inputs, which are not chosen by an attacker. Adversarial examples are inputs chosen to be the hardest for the model, so a model can be highly accurate on normal data and still fail on carefully changed inputs. Robustness has to be measured by attacking the model.

Sources