Skip to content
AI360Xpert
Core ML

Adversarial Training

To make a model immune to adversarial attacks, developers generate attacks during the training process itself and force the model to learn the correct labels for the poisoned data.

During training, the system intentionally creates adversarial examples and adds them to the batch alongside clean data. The model learns to map both the clean and noisy inputs to the same correct label.
During training, the system intentionally creates adversarial examples and adds them to the batch alongside clean data. The model learns to map both the clean and noisy inputs to the same correct label.

Why Does This Exist?

Machine learning models are lazy. During training, they find the easiest mathematical boundary that separates the training data. Because this boundary is highly complex and non-linear, it leaves jagged "blind spots" between data points. An attacker creates an Adversarial Example by finding one of these jagged edges and pushing a data point just over the line, tricking the model.

You cannot fix this by just adding random noise to your images; random noise rarely hits the exact jagged edge the attacker is targeting. To fix the blind spots, you have to actively search for them during training and smooth them out. Adversarial Training is the process of generating actual adversarial attacks on the fly during training and feeding them back into the model with the correct label. It teaches the model, "Even if this image has mathematically hostile noise applied to it, it is still a Panda."

Think of It Like This

A sparring partner who learns your weaknesses

Imagine a boxer preparing for a championship fight. If they only train by hitting a stationary punching bag (standard training on clean data), they will look great in the gym but get knocked out by a real opponent who knows how to exploit their dropped guard.

To truly prepare, the boxer hires a sparring partner whose entire job is to find the boxer's specific weaknesses and exploit them in practice. By constantly getting hit in those weak spots during training, the boxer learns to keep their guard up.

Adversarial training is the sparring partner. It actively calculates the model's worst weaknesses on every batch and forces the model to defend against them before the model is ever deployed.

How It Actually Works

The Min-Max Game

Standard neural network training is a minimization problem: find the weights that minimize the loss on the training data.

Adversarial training is a Min-Max problem. It consists of two nested loops acting in opposition:

  1. The Inner Loop (Max): For a given batch of data and the current model weights, the system generates adversarial perturbations (e.g., using FGSM or PGD) that maximize the loss. It tries to create the worst possible examples for the current model.
  2. The Outer Loop (Min): The model updates its weights to minimize the loss across both the clean data and the newly generated adversarial data.

By repeating this, the model's decision boundaries are forced to become smoother and less sensitive to tiny perturbations in the input space.

Projected Gradient Descent (PGD)

While the Fast Gradient Sign Method (FGSM) is a quick way to generate attacks, it's often too simple for robust training. If you only train against FGSM, the model learns a specific shortcut to ignore FGSM, but remains vulnerable to slightly different attacks.

Modern adversarial training uses Projected Gradient Descent (PGD). Instead of taking one big step in the direction of the gradient (like FGSM), PGD takes multiple small steps, clipping the image back into a valid range after each step. It is a more thorough, iterative search for the absolute worst-case noise. If a model is trained to survive PGD attacks, it is generally considered robust against most first-order adversarial attacks.

Show Me the Code

import torch
def train_adversarially(model, data, labels, optimizer, loss_fn):    # 1. Generate adversarial examples (Inner Loop - Maximize Loss)    # In practice, this uses an iterative PGD attack    adversarial_data = generate_pgd_attack(model, data, labels, epsilon=0.03)        # Combine clean and adversarial data    combined_data = torch.cat([data, adversarial_data])    combined_labels = torch.cat([labels, labels])        # 2. Standard training step (Outer Loop - Minimize Loss)    optimizer.zero_grad()    predictions = model(combined_data)        # The model is penalized if it misclassifies EITHER the clean or noisy data    loss = loss_fn(predictions, combined_labels)    loss.backward()    optimizer.step()        return loss

Watch Out For

The Robustness vs. Accuracy Trade-off

Adversarial training almost always causes a drop in standard accuracy. By forcing the model to smooth out its decision boundaries and ignore tiny pixel variations, you are preventing it from using some valid, high-frequency signals that genuinely help with classification. A model might drop from 95% to 90% accuracy on clean data, in exchange for surviving adversarial attacks.

Massive computational cost

Standard training requires one forward pass and one backward pass per batch. Adversarial training using PGD might require 10 forward/backward passes per batch just to generate the attacks in the inner loop, before finally updating the weights. This makes training 5x to 10x slower and much more expensive.

The Quick Version

  • Adversarial training defends against evasion attacks by generating adversarial examples on the fly and including them in the training batch.
  • It is formulated as a min-max problem: the attacker maximizes the loss to find the worst-case noise, while the model minimizes the loss on that noise.
  • Projected Gradient Descent (PGD) is the standard algorithm used to generate the robust attacks during training.
  • The defense comes at a cost: it significantly increases training time and often reduces standard accuracy on clean data.
  • Adversarial Examples explains the underlying vulnerability and the math behind FGSM.
  • Data Augmentation covers standard techniques (like rotating or cropping images) that improve generalization but do not defend against calculated adversarial attacks.
  • Out-of-Distribution Detection discusses how models can identify inputs that don't belong, an alternative defense strategy.

Related concepts