TL;DR
Adversarial training is a way to make a model harder to fool. At each training step, the method first finds, for each training input, the small change that hurts the model most, then updates the model so that it does well on the changed input as well as the original. The model gets better at keeping its answer under the attacks it was trained against, usually at a cost in accuracy on ordinary inputs and in training time.
Builds on: Adversarial Examples, Minimax Game, Backpropagation · Leads to: the fast gradient sign method as the cheap attack used in training, and robust training methods beyond it
Where it's used
Image classifiers in safety-sensitive settings. A vision model that reads road signs or pedestrians is checked by attacking it with small, targeted changes, and adversarial training is the most common way to improve its results under those attacks. The benefit is measured as accuracy on attacked inputs, not only on the clean test set.
Malware and spam detection. A detector that looks at file features or message text can be probed by an attacker who learns which features to change. Training the detector on attacked examples, produced with changes the attacker can really make, is one of the standard defences in this area.
Research benchmarks for robustness. Many papers report a model's accuracy under a fixed attack and budget, for example a small change to each pixel on a standard image dataset. Adversarially trained models are the baseline that new defences are compared against.
How it works
Take a tiny model that predicts a number: , with loss . The current weight is . One training example has input and target . The attacker may change the input by at most in either direction. The learning rate is .
- Measure the clean loss. The prediction is , and the error is . The loss is . Check: the model is off by one unit, so the loss is half of 1.
- Find the worst change. The attacker may move anywhere in . Moving down makes the prediction smaller, which makes the error more negative and the loss larger. So the worst change is the largest move down, . Check: the loss at is , larger than at .
- Compute the gradient at the attacked input. The gradient of the loss with respect to is . Here , so the gradient is . Check: the sign is negative, so increasing lowers the loss on the attacked input.
- Step the weight. . The new weight is . Check: the weight rose, which is the direction the gradient asked for.
- Look at the clean input again. The prediction is now , and the clean loss is . It fell from . Check: the model is closer to the target on the original input, as well as on the attacked one.
- loss at change x
The math
Setting
Let be all the weights of the model, the model, and the Loss Function on input with label . The attacker's budget is an -ball in the infinity norm: a change is allowed if every entry of has size at most , written . The infinity norm is the largest absolute value among the entries (see Vector Norm). The training data is a distribution over pairs .
Step 1 in symbols: the objective is a minimax
Adversarial training solves
The inner maximum is the attacker: for each example, find the change that makes the loss largest. The outer minimum is the learner: choose so that this worst-case loss is small. This is a Minimax Game in which the two players' gains are opposite.
Step 2 in symbols: the attack
Approximate the loss around with a first-order step, . To make this as large as possible inside the box, choose each entry of to have the sign of the gradient and size :
This is the Fast Gradient Sign Method. In the walkthrough, , so and . Check: this matches step 2 of the walkthrough.
A stronger attack takes several smaller steps and projects back into the box after each one. This is projected gradient descent, with steps of size :
Step 3 in symbols: the learner's update
Danskin's theorem, a standard result about maxima, says that under mild conditions the worst-case loss changes with at the same rate as the loss at the worst input, so the gradient of the inner maximum with respect to equals the gradient of the loss at the maximising input. So the learner can treat as a fixed input and take an ordinary Gradient Descent step:
In the walkthrough, , and the step gives .
Step 4 in symbols: mixing clean and attacked losses
Many implementations train on a weighted mix of the two losses, with :
With and the numbers above, . The mix keeps the model accurate on clean inputs while it learns the attacked ones.
Worked examples
A model predicts with weight . The input is , the target is , the budget is , and the learning rate is .
- Clean prediction: . Clean loss: .
- The loss as a function of the change is .
- On , the largest value is at , where . Attacked loss: .
- The attacked input is . The gradient with respect to is .
- Update: .
- New clean prediction: . New clean loss: . It fell from .
Interpretation: the update made the model better at the original input as well as the attacked one. Here the attacked and clean inputs pulled the weight in the same direction, which is not always the case.
A linear classifier with weights and bias predicts positive when . The input is positive and correct. The budget is per value. Compare two models.
- Model A has . Clean score: , which is positive.
- The worst change for model A pushes the score down by . Attacked score: , which is negative. Model A fails under attack.
- Model B has . Clean score: .
- The worst change for model B pushes the score down by . Attacked score: , which is positive. Model B keeps its answer.
- Check: the change for B is , and , as step 4 says.
Interpretation: both models are correct on the clean input, but only B has a margin larger than the total budget effect. Adversarial training pushes the model toward B-like weights, where the margin on the training data is big compared with the budget.
A training run for an image model takes 3 hours on clean data per full pass over the dataset. The adversarial version uses a 7-step attack inside each training step.
- Each clean training step needs one forward and one backward pass.
- Each attack step needs one more forward and backward pass to compute the gradient with respect to the input, so a 7-step attack adds 7 passes.
- Total passes per training step: .
- Compute ratio against clean training: .
- Adversarial training time: hours per pass over the dataset.
- Try it: if the attack is cut to 3 steps, what is the ratio and the time? Answer: the ratio is , and the time is hours.
Interpretation: the price of a stronger attack is paid in training time, and this is one reason a cheaper one-step attack is used in many experiments. The attack is part of the training loop, so its cost compounds across every step.
Common mistakes and tradeoffs
It is tempting to think that adversarial training makes a model robust to every attack. In fact it helps against the attacks it was trained on, and its robustness can be weaker against a different or stronger attack. Robustness claims must be tested with attacks the defender did not use in training.
It is tempting to think that training on a weak one-step attack is enough. In fact a model can learn to hide its gradient, so the one-step attack finds a weak direction while a stronger attack still succeeds. This is called gradient masking, and it can make a model look robust when it is not. Multi-step attacks are the standard check.
It is tempting to think that the robust model will be as accurate as before. In fact there is usually a tradeoff: accuracy on clean inputs tends to fall, because the model must keep a larger margin. This is a version of the Bias-Variance Tradeoff: the model gives up some fit to the clean data to become less sensitive to small changes.
When to use it. Use adversarial training when inputs can be chosen by an adversary and a wrong answer has real costs, and when you can afford the extra training time. Test it with a range of attacks before trusting it.
When not to use it. If inputs are not chosen by anyone and the main risk is noise or distribution shift, it may cost accuracy for little benefit. In that case Data Augmentation, which applies ordinary transformations such as crops and flips, is cheaper and often enough.
Alternatives. Input preprocessing and detectors can be useful, but a determined attacker who knows the defence can often get around them. Certified defences give a guaranteed robust answer for a radius around each input, usually at a cost in accuracy and compute, but the guarantee holds for the stated budget only.
History
In 2013, Szegedy and colleagues showed that deep image networks could be fooled by small, carefully computed changes. In 2014 Goodfellow, Shlens and Szegedy proposed training on such changes as a regulariser, and introduced the Fast Gradient Sign Method to generate them cheaply. Their paper argued that the phenomenon is tied to the linear behaviour of models in high dimensions.
In 2017 Madry and colleagues framed adversarial training as the minimax problem above, and trained with a multi-step projected gradient attack. That made the training objective precise and gave a stronger benchmark, and it became the reference point for later robustness work. The field still struggles with the cost of training and with the gap between robustness to known attacks and robustness in general.
Recap
- Adversarial training solves a minimax problem: the model minimises the loss an attacker can achieve inside a small budget.
- Each step has two parts: find the worst change for each input, then update the model on the changed input, usually mixed with the clean loss.
- The fast gradient sign method gives a one-step attack; projected gradient descent gives a stronger multi-step one.
- Robustness is gained against the attacks used in training, usually at a cost in clean accuracy and in training time.
- Gradient masking can make a weak attack look unsuccessful, so test with stronger attacks.
- Read next: Fast Gradient Sign Method for the attack used in the inner loop, and Data Augmentation for a cheaper approach to variety in training data.
Check yourself
QuestionRecall: what is the inner maximisation in the adversarial training objective, and what is the outer minimisation?Show answer
QuestionCompute: a model has , input , target , budget . The FGSM attack gives . What is the gradient of the loss with respect to at , using ?Show answer
QuestionWhy: why does a model trained only against a one-step attack sometimes look robust but fail under a multi-step attack?Show answer
QuestionApplied: a company wants its document classifier to resist people who edit documents to fool it, and its clean accuracy is already close to the limit of its data. What should it weigh before adding adversarial training?Show answer
Sources
- Goodfellow, Shlens and Szegedy, Explaining and Harnessing Adversarial Examples (arXiv:1412.6572).
- Madry et al., Towards Deep Learning Models Resistant to Adversarial Attacks (arXiv:1706.06083).
- Adversarial machine learning, Wikipedia.