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 , the Dot Product of the weights and the input plus a bias , and it predicts the positive class when . Let and .
- Start with a correct input. Take . The score is , which is positive, so the classifier says positive, which is correct. Check: the score is above zero.
- Set a budget. Allow each input value to change by at most in either direction. A change this size is what the attacker is allowed to make. Check: the largest single change is 0.5.
- Find the direction that hurts most. The Gradient of the score with respect to the input is . 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).
- Make the change. New input: . Check: each value moved by exactly 0.5, within the budget.
- Recompute the score. , which is negative. The classifier now says negative. Check: the answer has flipped.
- 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.
- score after a change of size x
The math
Step 2 in symbols: the budget
The set of allowed changes is the box . Every value may move by up to , all in one step. In the example, allows any with each .
Step 3 in symbols: the gradient
For the linear score, . For a Feedforward Neural Network, the same quantity 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 alters the score by about . To lower the score as much as possible within the box, choose
where takes the sign of each entry. For the linear example, , so , which matches step 4. The score changes by , which equals . Check: the baseline is , so the new score is , the same as step 5.
Step 6 in symbols: why many small changes add up
Suppose an input has values and the model's weights have typical size . The attacker moves every value by in the best direction, so the score changes by about . The effect grows with , even when is tiny.
For example, take , and . The score changes by . A baseline score of 0.5 becomes . Each input value moved by one hundredth, but together the moves flipped the answer.
Worked examples
- A classifier scores a single input value as , and predicts positive when . The input is , so , which is correct.
- Try a budget . The gradient is , its sign is , so the change is .
- New input: . Score: , still positive. The budget was too small.
- Try . The change is , and .
- Score: , which is negative. The answer has flipped.
- Check: the change in score is , and .
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.
- The classifier has weights and bias , so .
- The input is . Score: , which is positive and correct.
- Use budget . The sign of is , so .
- New input: . Score: , still positive.
- The change in score is , and . This matches step 4.
- Increase the budget to . Then and .
- Score: , which is negative. The answer has flipped.
- Try it: is 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, , not on any single one.
- Take a colour image of values, so values, each stored as a brightness from 0 to 255 and scaled to the range 0 to 1.
- Suppose a linear model over the pixels has typical weight size , and the baseline score is (positive, correct).
- Use budget , which is two brightness levels out of 255.
- The score change is .
- Compute: . Then .
- New score: , which is negative. The label flips.
- Each pixel changed by two brightness levels out of 255, which is hard to see on a screen.
- Try it: with , what is the new score? Answer: the change is , and . 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 on each of values can move the score by about , 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.