Model and data securityMiddleLesson 1697 min read

Adversarial examples: fooling a model

A tiny, carefully chosen change to an input can flip a model's answer. Learn why this happens, where it matters, and what defences really achieve.

Lesson in motion

In 60 seconds

Adversarial examples: fooling a model

A tiny, carefully chosen change to an input can flip a model's answer. Learn why this happens, where it matters, and what defences really achieve.

1/4
In simple words
Imagine a teacher who marks answers only by looking for certain words. A clever student can add those words and get marks without knowing the answer. A model can be fooled the same way: it learned shortcuts, and someone can press on those shortcuts.
An adversarial example is an input that has been changed on purpose so that a model gives a wrong answer, while a human sees little or no difference. Using such inputs to slip past a model at run time is called an evasion attack.

What it looks like

Input typeSmall changePossible wrong result
ImagePixel changes too faint for people to notice, or a small stickerA classifier names the wrong object
TextA swapped letter, a look-alike character, a synonym, extra spacesA spam or toxicity filter lets the message through
AudioQuiet noise mixed into a clipA speech system hears different words
Tabular dataSmall edits to a few fields, such as timings or amountsA fraud model scores a risky transaction as safe
Researchers have shown these effects many times in lab settings, for example with small stickers on road signs confusing some image models. In the real world, the most common case is simpler: people tweak spelling or wording to get past content filters and fraud checks.

Why it happens, intuitively

  • A model draws boundaries between classes in a space with very many dimensions. Many points sit close to a boundary, even if they look ordinary to us.
  • Tiny changes spread across many pixels or features add up, and can push an input across that boundary.
  • Models often learn shortcuts: patterns that work on the training data but are not what humans use, such as texture instead of shape.
  • If an attacker can query the model many times, they can search for the change that works. Attacks built on one model often transfer to similar models.

Defences and their limits

DefenceWhat it doesLimit
Adversarial trainingAdd attacked examples to training so the model learns to resist themCosts more compute, can lower normal accuracy, and helps mainly against the attacks you trained on
Input checks and cleaningNormalise text (for example, map look-alike characters), resize or compress images, reject odd inputsAdaptive attackers design changes that survive the cleaning
Ensembles and second opinionsUse several different models or a rule-based check as wellRaises the cost of attack but does not remove it
Limits on queriesRate limits, and hiding exact scores, make search harderDoes not stop transfer attacks built on a copy model
Design for failureDo not let one model score decide something important aloneNeeds process changes, not just ML work
Watch out
There is no known defence that makes a model fully robust. Treat any model that faces untrusted input as something that can be fooled, and put other controls around important decisions.

Frame the threat realistically

  1. 1

    Who gains?

    Is there someone who benefits if the model is wrong? Spammers, fraudsters and cheaters usually do.
  2. 2

    What access?

    Can they query the model freely, see scores, or only see yes or no?
  3. 3

    What is the harm?

    A wrong movie tag is small. A missed fraud alert or a wrong medical flag is large.
  4. 4

    What else catches it?

    Human review, rules, limits and later checks reduce the damage of one wrong answer.
Note
MITRE ATLAS lists evasion and many other attacks on AI systems, with real case studies. It is a good place to learn the common names security teams use.
Try this. Pick one model in your company that makes a decision about untrusted input (a spam filter, a KYC document check, a fraud score). Write down who would gain by fooling it, and what happens today if it is fooled once.

Watch and read more

Lab

A small evasion test set and a results table for a text classifier you run yourself.

~25 min

The problem

Use a simple spam or sentiment classifier on your own machine (train a tiny one with scikit-learn, or use any small open model). Do not test systems you do not own.

  1. Pick 10 messages the model gets right.
  2. For each, make 3 small changes a human would still understand: a misspelling, a look-alike character (for example the digit 0 for the letter o), and extra spaces or punctuation.
  3. Record which changes flip the prediction.
  4. Add one input cleaning step (lowercase, remove extra spaces, map common look-alikes) and run the test again.
  5. Write 3 sentences on what the cleaning fixed and what it did not.

Practice lab

Edit the code and run it. Python runs in your browser — nothing is sent to our server.

Output

No output. Add print(...) to see values.

You are done when

Hard questions

Try to answer before you reveal. If you can answer these, you understood the lesson.

Q1Why do attacks built against one model often work on a different model?Reveal

Models trained on similar data for the same task tend to learn similar features and similar decision boundaries. A change that pushes an input across one model's boundary often pushes it across the other's too. This is why hiding your model's details helps, but is not a full defence.

Q2An input cleaning step blocks all your test attacks. Why is that not proof of safety?Reveal

Your tests reflect the attacks you thought of. An attacker who knows about the cleaning step can design changes that survive it, such as new look-alike characters or different wording. Keep adding new cases, monitor real traffic for evasion attempts, and make sure important decisions have a backstop beyond the model.

Please sign in to continue.

Questions people ask

Do these attacks only matter for self-driving cars and images?

No. Any model that judges input from people who may want a different answer is exposed: spam and abuse filters, fraud scores, document checks, resume screening, malware classifiers and content moderation. Text-based evasion is very common in practice.

Are large language models affected?

Yes. Odd strings, rephrasing, other languages and encodings can make a language model behave differently, including getting past safety filters. Prompt injection is a related but separate problem, covered in other tracks.

Should we do adversarial training on every model?

Not by default. It costs compute and can reduce normal accuracy. Use it where an attacker clearly gains from evasion and the harm is high. For many systems, input checks, rate limits and a human or rule-based backstop give more value for the effort.

How do we test our own model?

Build a small test set of realistic evasions for your domain: misspellings, look-alike characters, mixed scripts such as Hindi written in English letters, and small edits to numeric fields. Run it before every release and track the result like any other metric. Open-source adversarial ML libraries can help for images and tabular data.

Lesson test

5 questions. Get 3 right (60%) to pass and complete this lesson.

Sign in to take the test and save your progress