Adversarial training

On this page 7

Adversarial training is a defense technique in which a machine learning model is trained on adversarial examples generated on the fly, inputs deliberately perturbed to cause errors, so that it learns to classify them correctly. In AI security, it is the most established empirical defense against evasion attacks and the reference point against which other robustness methods are measured.

Formally, adversarial training solves a min-max optimization problem: an inner step searches for the perturbation within a fixed budget that maximizes the loss, and an outer step updates the model weights to minimize that worst-case loss. The idea dates to early work on the fast gradient sign method and was given its standard form by Madry and colleagues in 2017 with projected gradient descent.

How adversarial training works

Adversarial training works by replacing, or mixing, clean training inputs with worst-case versions of those inputs found by an attack against the current model. Because the attack is recomputed at every step, the model is always trained against its own current weaknesses rather than a fixed set of hard examples.

The perturbation is bounded by a threat model, usually an Lp norm ball of radius epsilon around the original input. For image classifiers on CIFAR-10, a common budget is epsilon = 8/255 under the L-infinity norm, which changes each pixel by at most about 3% of its range and is nearly invisible to people.

One training iteration follows the steps listed in the table.

StepOperationTypical setting
Sample batchTake clean inputs and labelsStandard mini-batch
Random startAdd uniform noise inside the epsilon ballAvoids gradient masking
Inner maximizationRun several gradient ascent steps on the loss7-10 PGD steps
ProjectionClip the perturbation back into the budgetL-infinity or L2 ball
Outer minimizationUpdate weights on the adversarial batchSGD or AdamW step
One iteration of PGD-based adversarial training

The cost is substantial. With 10 attack steps, each training iteration needs roughly eleven forward and backward passes instead of one, so PGD adversarial training typically takes several times longer than standard training on the same hardware.

Main variants of adversarial training

Researchers have proposed many variants that trade cost, clean accuracy and robustness differently. The choice of variant usually matters less than the strength of the inner attack and the correctness of the evaluation.

VariantKey ideaMain trade-off
FGSM trainingSingle-step attack per batchFast, but prone to catastrophic overfitting
PGD trainingMulti-step projected attackStrong baseline, high cost
TRADESSeparate clean loss and robustness regularizerTunable accuracy-robustness balance
Free and fast trainingReuse gradients across stepsLower cost, careful tuning needed
Ensemble adversarial trainingAdd examples from other modelsHelps against transfer attacks
Adversarial fine-tuningRobustify a pretrained modelCheaper than training from scratch
Common adversarial training variants

Single-step methods can suffer catastrophic overfitting, a sudden collapse in which the model becomes robust to FGSM but not to multi-step attacks. Random initialization and gradient alignment regularizers reduce this risk, but every single-step result should be verified with a strong multi-step attack.

Adversarial training compared with other defenses

Adversarial training differs from most other defenses because it changes what the model learns, rather than filtering inputs or hiding gradients. Many early alternatives, including defensive distillation and input transformations, were later broken because they relied on gradient masking, which only makes attacks harder to compute without removing the vulnerability.

The table compares adversarial training with neighboring defenses in the same Topic.

DefenseGuarantee typeResists adaptive attacksTypical cost
Adversarial trainingEmpiricalLargely, within its threat modelHigh training cost
Randomized smoothingCertified (L2 radius)Yes, within the certificateHigh inference cost
Certified robustness trainingFormal boundsYes, for small radiiVery high, limits model size
Defensive distillationNoneNo, broken by Carlini–Wagner attackModerate
Input preprocessingNoneUsually noLow
Defenses against evasion attacks

In practice, adversarial training is often combined with other controls: input validation, anomaly detection, rate limiting and monitoring. No single defense covers every threat in frameworks such as MITRE ATLAS or NIST AI 100-2.

Adversarial training for language models

For large language models, adversarial training takes a different form, because text is discrete and the relevant threats are jailbreaks and prompt injection rather than small pixel changes. Developers collect or generate attack prompts through red teaming, then fine-tune the model to refuse or resist them, often in combination with safety classifiers.

Another line of work applies perturbations in the embedding or latent space, which keeps gradient-based optimization possible. Such training improves resistance to known attack families, but novel jailbreak styles frequently transfer past it, so evaluation with tools such as garak or PyRIT must continue after deployment.

Limitations and evaluation pitfalls

The most important limitation is the accuracy-robustness trade-off: robust models usually lose clean accuracy, often by 5-15 percentage points on image benchmarks, while robust accuracy under strong attacks commonly remains around 50-70% on CIFAR-10 at standard budgets. Robustness also applies only to the threat model used in training; a model hardened for L-infinity noise may remain weak against patches or rotations.

MistakeConsequenceBetter practice
Weak evaluation attackOverstated robustnessUse AutoAttack or adaptive attacks
Ignoring robust overfittingRobust accuracy declines late in trainingEarly stopping on robust validation
Single threat modelBlind to other perturbation typesTest multiple norms and physical attacks
Gradient maskingIllusion of securityCheck black-box and transfer attacks
Frequent mistakes in adversarial training projects

A credible claim of robustness requires adaptive attacks designed against the specific defense, reported clean and robust accuracy, and a clearly stated perturbation budget. Without these, results cannot be compared and may give a false sense of security.

Put simply

Some attackers fool AI systems with tiny, hidden changes. A few altered pixels can make a model see a stop sign as a speed limit sign. People would not notice the change, but the model is easily tricked.

Adversarial training is like a sparring partner for the model. During training, an opponent keeps searching for the trick that works best right now. The model then learns from each trick it falls for. Over time, it becomes much harder to fool. The price is slower training and slightly lower everyday accuracy.

In practice

A logistics company uses an image classifier to read damage labels on parcels at sorting centers. A security review shows that printed stickers with small noise patterns can make the model misread labels, which could route damaged goods past inspection. Standard training gives 95% clean accuracy but almost no robustness to such perturbations.

The team applies PGD adversarial training with an L-infinity budget matched to realistic print noise and adds physical patch examples to the attack set. Training takes about four times longer. The hardened model reaches 91% clean accuracy and 68% robust accuracy under adaptive attacks, and the company pairs it with anomaly monitoring so that suspicious labels go to a human inspector.