Please enable JavaScript to use CodeHS

AI Security • Machine Learning

How do people trick an AI? Adversarial examples, explained

An AI that looks at pictures can be fooled by changes you cannot see. Learn how that works, why it matters, and how defenders fight back. Then try it yourself in two hands-on labs.

AI security ~12 min 2 interactive labs
+ tiny noise original original + noise RING 62% CROSS 57% # same picture to you. different answer to the model.
The Idea

What is an adversarial example?

An adversarial example is an input, like a picture, that someone changed on purpose so an AI gives the wrong answer. The change is small and carefully chosen. To a person, the picture often looks exactly the same.

Think of a very strict spelling checker. It is great at its job. But if someone knows exactly how it works, they can slip in a change that a human would never notice and that makes the checker say the wrong thing. Adversarial examples work like that, but for AI that looks at images, listens to audio, or reads text.

Short version: A normal mistake is an AI getting confused by something hard. An adversarial example is someone aiming at the AI's weak spots on purpose.

Some words we will use. A model is the math inside an AI that turns an input into an answer. A classifier is a model that sorts things into labels, like "cat" or "dog". An attack is any attempt to make the model fail. Nobody gets hurt in this tutorial: the labs use a tiny toy model that lives in your browser.


How It Works

How does an AI image classifier decide?

A computer does not see a picture the way you do. It sees a grid of numbers. Each tiny square, called a pixel, has a number for how bright it is. A classifier follows four steps.

  1. Read the pixelsTurn the picture into a grid of brightness numbers.
  2. Weigh each pixelFor each label, every pixel has a weight: a push toward that label, or away from it.
  3. Add up a scoreEach label gets one total score from all those pushes.
  4. Pick the winnerThe highest score wins. The scores are turned into percentages called confidence.

Real classifiers have many layers and millions of numbers inside. The idea at the heart of them is the same: lots of small bits of evidence get added up into one score per label.

toy_classifier.py (the idea, simplified)
# every label has one weight per pixel
ring_score  = sum(ring_weight[i]  * pixel[i] for i in range(144))
cross_score = sum(cross_weight[i] * pixel[i] for i in range(144))

# the bigger score wins
if ring_score > cross_score:
    print("ring")
else:
    print("cross")

Lab 1: Nudge the pixels

Lab 1 TOY MODEL: 12x12 pixels, 2 labels, made for learning

Click a square to change its brightness (dark, medium, bright). Watch both scores and the confidence bars move. Turn on the heatmap to see which squares push toward Ring and which push toward Cross.

 

Ring
50%
Cross
50%

This is a toy: two hand-made weight patterns and simple adding. Real image models are far bigger, but they also turn pixels into one score per label.


The Trick

How can tiny changes that people can't see flip the answer?

Here is the surprising part. Remember that every pixel gets a small vote. An attacker who knows (or can guess) the model's weights does not need to change the picture a lot. They nudge every pixel a tiny amount, each one in the direction that helps the wrong label. One nudge does almost nothing. Thousands of nudges, all pointing the same way, add up.

Real photos have hundreds of thousands or millions of pixels, so each nudge can be far too small for your eyes to notice. Our toy has only 144 pixels, so it needs bigger nudges, and you may be able to see a faint speckle. That is a limit of the toy, not of real attacks.

Aimed beats random. Random static mostly cancels out: some nudges help, some hurt. Adversarial noise is not random. Every nudge is chosen to push the same way. Try both in the lab and compare.

Lab 2: The noise budget

Lab 2 TOY MODEL: the same one from Lab 1

The noise budget is how far an attacker may change each pixel. Slide it up and see how much budget it takes to flip the answer. Then press Reveal the difference to see where the changes were hiding.

Start with
Kind of noise
0%

Each pixel can change by at most this share of the full dark-to-bright range.

Original
With noise
Ring
50%
Cross
50%

Toy numbers: a change of 1% is about 2.5 steps on the usual 0 to 255 brightness scale. Real attacks on full-size photos are often much smaller than this.

Spot the difference

Look closely ILLUSTRATION: not tied to the toy model

These two pictures are not the same. In the right one, every pixel was changed by at most 2 out of 255 brightness steps. Can you spot where? Then press the button to boost the difference so it becomes visible.

Picture Aoriginal
Picture Bevery pixel changed by up to 2 of 255

To you, A and B look the same. A model does not look at the picture as a whole. It adds up every pixel, so a speckle that is too faint for you to see can still matter to it.

What did you notice? A not-very-sure image flips with a small budget. A very sure image needs a much larger one. And random static of the same size barely moves the scores. That is why defenders worry about aimed changes, not just noisy pictures.


Real World

Why does tricking an AI matter in real life?

Many everyday tools use classifiers. If someone can aim at their weak spots, a wrong answer can matter. Here are three places researchers and security teams think about. These are described in general terms.

Road signs

A self-driving system has to read signs. In research experiments, scientists have shown that carefully designed stickers or patterns can make some sign-reading models give the wrong answer. This is why car makers do not rely on one camera model alone.

Face unlock

Face recognition compares a face to a stored one. Researchers have shown that specially made patterns, like printed glasses frames, can fool some face recognition models in lab tests. Many real systems add extra checks for this reason.

Spam filters

A spam filter scores each message. Spammers try small changes, like odd spellings or extra harmless text, to push the score toward "not spam". Filters keep retraining to catch the new tricks.

Notice the pattern: in each case there is a model that gives a score, and someone who gains if the score goes the wrong way. Adversarial examples are one kind of evasion attack, which means slipping past a model at the moment it makes its decision.

Same family, different doors. Adversarial examples trick a finished model with a changed input. Prompt injection hides instructions in text a chatbot reads. Data poisoning damages the training data before the model learns.


Fighting Back

How do defenders respond to adversarial examples?

There is no single fix. Instead, teams stack several defenses so one trick is not enough. Here are three common ones.

More diverse training data

Train on pictures from many cameras, lights, angles, and places. A model that has seen more of the world has fewer odd blind spots to aim at.

Adversarial training

Make adversarial examples on purpose, then train the model on them with the correct labels. It is like a boxer sparring with a partner who throws tricky punches. This often helps, but it costs extra training and does not stop every attack.

Checks by humans

For important decisions, a person reviews what the AI flagged or was unsure about. People notice things models miss, and models notice things people miss. Together they cover more.

Extra layers of safety

Use more than one sensor or model, limit what a single wrong answer can cause, and watch for strange inputs. If one part is fooled, another can catch it.

Defending is a back-and-forth game. Researchers invent a new attack, defenders learn from it, and then new attacks appear. That is normal in security, the same way new locks lead to new lock picks, then better locks.

Keep it ethical. Testing models you own, or that you have permission to test, is how researchers make AI safer. Trying to fool a system you do not own, or one that people depend on for safety, is not okay.


Check Yourself

Quick quiz

1. How does an image classifier pick its answer?

2. What makes an adversarial example different from random static?

3. Which is a real way defenders make these attacks harder?

Key takeaways

  • A classifier turns pixels into numbers, adds them up into one score per label, and picks the biggest.
  • An adversarial example is a small, aimed change that makes a model give the wrong answer.
  • Tiny nudges work because thousands of them, all pointing the same way, add up.
  • Aimed noise is far more powerful than random noise of the same size.
  • Defenders layer diverse data, adversarial training, and human checks. No single fix is complete.

FAQ

Adversarial examples: common questions

What is an adversarial example?

An adversarial example is an input, such as an image, that has been changed in a small, carefully chosen way so an AI model gives the wrong answer. People looking at it often see no difference from the original.

Why do tiny changes fool an AI?

An image classifier adds up evidence from thousands of pixels. If every pixel is nudged a tiny amount in the direction that helps the wrong label, those small pushes add up and the wrong label can win, even though the picture looks the same to a person.

Can adversarial examples be stopped completely?

Not yet. Defenders make attacks harder with more diverse training data, adversarial training, and checks by humans for important decisions, but there is no single fix that works against every attack.

How is this different from prompt injection or data poisoning?

Adversarial examples change the input to a finished model. Prompt injection hides instructions in text that a chatbot reads. Data poisoning damages the training data before the model learns from it.

Keep learning about AI security

Adversarial examples are one of several ways AI can be attacked. Explore the others and the ideas underneath them:

Want to go deeper? The CodeHS AI and cybersecurity courses build these skills step by step.