ML Security
Intermediate 16 min read ML Security

Adversarial Perturbations

Tiny changes, flipped decisions

Adversarial perturbations are small crafted input changes that flip model outputs. Defenses mix pre-processing, robust training (adversarial training, augmentation), and anomaly detection — evaluated by an honest attack-targeted measurement of robustness.

adversarialevasionrobustnessadversarial trainingperturbation
Before we start

What you will learn

  • Understand how perturbations flip decisions
  • Defend with preprocessing, robust training, and anomaly screening
  • Measure robustness against attack instead of hoping
Introduction

What is this?

Adversarial perturbations are imperceptible or hard-to-spot changes — a few pixels, a rephrased sentence, a slightly-rebalanced feature vector — that exploit the model's learned boundaries to produce confidently wrong outputs. Defenses harden the input surface and the training itself.

Motivation

Why do you need it?

  • Demonstrate that 'high accuracy' means little against a targeted adversary.
  • Real cases: autonomous-safety misreads, fraud-model evasion, spam filters trained mutable.
  • Without robustness measurement you can't know where you stand.
  • Defensive tax is real; you must decide where robustness is worth the cost.
Think about it

Mental model

Imagine

Changing one letter in a word so a dictionary misreads it

Think of

Adversarial perturbation

'Dog' with one pixel re-shaded becomes (to the model) 'bird', though a human sees a dog. The model over-indexes on fragile cues; the attacker only has to find the fragile ones, and — unlike a human — the model is deterministic to probe.

Mechanics

How it works

  • Attacker computes a gradient-based or search-based perturbation minimizing the decision-margin to a target class.
  • Perturbation is bounded so it's plausible (epsilon-ball in feature space).
  • Live inputs get sanitized: clipping, JPEG/denoising, perplexity checks on text, NER-gating.
  • Robust training: adversarial examples added to the batch, or data augmentation in the same spirit.
  • Robustness measured by attack-targeted metrics — not clean-data accuracy.

Adversarial pipeline vs defense.

Input
Sanitize
Model
Anomaly
Decision
Real world

In production

  • A faces-and-liveness system blocks replayed/spoofed samples before the model ever scores them.
  • A finance classifier drops suspiciously clean 'too-perfect' vectors into review queues.
  • A content spam filter adversarial-augments its historical easy-wins so evasions stop scoring clean.
Guidelines

Best practices

  • Set the robustness bar per decision-stakes; a joke model doesn't need the max-defense tax.
  • Preprocess hostile surfaces (images, free text, external features) before scoring.
  • Train adversaries into the batch — procedural augmentation beats hoping clean data survives.
  • Report robustness metrics on attack suites, not only clean accuracy.
Watch out

Common mistakes

  • Measuring 'robustness' only on clean test data — that's not a robustness claim.
  • Denoising everything — real low-quality samples get destroyed along with the adversarial ones.
  • Blocking attacks by banning inputs that 'look adversarial' — the adversary iterates; your bans lag.
  • One-size preprocessing for every customer segment — the smooth path to 'it works in staging, not prod'.
When things break

Debugging tips

  • Model flips on clean, slightly-blurred input in prod — your sanitizer is too aggressive; relax by segment.
  • Adversarial-augmented retrain improved clean accuracy but not attack cases — the augmentation wasn't targeted enough.
  • Anomaly gate tripping on legit high-traffic patterns — separate traffic-anomaly from adversarial-anomaly signals.
Interviews

Interview checkpoint

Practice

Hands-on lab

Craft and Counter an Attack
40 min · intermediate
In progress
Objectives
  • Implement a gradient-based perturbation
  • Flip a trained model's decision with a bounded change
  • Show that sanitization or robust retraining restores sanity

Craft and Counter an Attack

40 min · intermediate

0/3 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/2
1

An adversarial perturbation is…

2

Clean-test-set accuracy is a valid measure of adversarial robustness.

Go deeper

Official documentation