Principles of AI · Unit 1: Inside the Machine · Activity 1.1.2

Deep Dive: Train, Test, Trust

🔬 Deep Dive
This is your textbook for this topic. Take your time. Read it more than once.
Principles of AI · 1.1.2Concept

Learning From Labeled Examples

In 1.1.1 your model learned from raw text and predicted a next word. This activity uses a different kind of learning. You show the computer many examples that already have the right answer attached, such as a photo labeled "daisy" or "rose." The computer adjusts itself until it can give the right label to examples it hasn't seen. Learning from examples with answers attached is called supervised learning, and the answers are called labels.

training data + labels → model → label for a new input

When the answer is a category (daisy or rose, spam or not spam, diseased or healthy) the task is classification. When the answer is a number (a house price, tomorrow's temperature) the task is regression. Both are "predicting," but they predict different kinds of things. Most of this page is about classification, and the section on how models learn shows regression at the end.

💡In class you trained an image classifier with Teachable Machine. It starts from a model that has already learned general features of images, then adjusts to your labeled pictures. That is why a few dozen photos per class can be enough for a demo. It is also why the demo is easy to fool: it only knows the examples you gave it.
Principles of AI · 1.1.2Concept

What an Image Is to a Computer

You see a flower. The computer gets a grid of numbers. A digital picture is made of tiny squares called pixels. In a black-and-white picture each pixel is a single number for how bright it is, from 0 (black) to 255 (white). In a color picture each pixel is three numbers: how much red, green, and blue.

Pixel lab · click a square to flip it

To a computer this picture is 36 numbers: 0 for black through 255 for white. Right now 8 pixels are 0 and 28 are 255.

Color is three numbers
one pixel = [220, 60, 90]

A photo from a phone camera has millions of pixels, so one photo is millions of numbers. Nothing in those numbers says "petal" or "stem." The model has to discover which patterns of numbers tend to go with which label.

Structured and unstructured data

A spreadsheet with a row per student and a column per quiz score is structured data: it has rows, columns, and meaning attached to each position. Photos, audio, and free-form text are unstructured data: just raw values with no labeled columns. Most of the information in the world is unstructured, and learning from it is a big reason modern AI models need so much data and computing power.

⚠️Watch out: the model never "sees" a daisy. If every daisy in your training photos was on green grass and every rose was on a wooden table, the numbers for "green background" can become the strongest clue. The model may have learned grass, not daisies.
Principles of AI · 1.1.2ConceptSkill

Train, Then Test on Images It Hasn't Seen

Picture a student who memorizes the answer key to a practice quiz. Ask the exact same questions and they score 100%. Change the questions and the score collapses. A model can do the same thing. A model's score on the very images it trained on mostly shows that it can fit those images. It doesn't show that it will cope with new ones.

So before training, set some labeled images aside. These test images are never used to train. After training, run the model on them and compare its answers to the real labels. The rest are the training images. This is called a train/test split.

Split lab · you have 60 labeled images

With 12 test images, one wrong answer moves your score by 8.3 percentage points. A reasonable balance for a set this small: enough to learn from, enough to test with.

⚠️Check your lab: if you tested your Teachable Machine model by holding up pictures that were also in your training folders, you tested it on images it had already seen. Honest testing means new photos: different flowers, different lighting, different backgrounds. Your teacher may have you collect a test set first.

Even a good test has limits. It only tells you how the model does on images like the test images. If the test photos were all taken in a classroom and the model will be used outdoors, the score may not carry over. A test is only as good as its match to real use.

Principles of AI · 1.1.2ConceptSkill

The Confusion Matrix

"How many did it get right?" is a start, but the more useful question is "how was it wrong?" A confusion matrix is a table that sorts every test result by what the answer really was and what the model said. For a yes/no task, pick one answer as the positive class (the thing you're looking for: diseased, spam, peanuts). Then every result lands in one of four boxes:

True positive
Really positive, model said positive. A correct catch.
True negative
Really negative, model said negative. A correct all-clear.
False positive
Really negative, model said positive. A false alarm.
False negative
Really positive, model said negative. A miss.

From the four boxes you can calculate three common scores:

accuracy = (TP + TN) ÷ all results

precision = TP ÷ (TP + FP)

recall = TP ÷ (TP + FN)

Accuracy is how often the model was right overall. Precision asks: when the model says "positive," how often is it right? Recall asks: of the things that really are positive, how many did the model find?

ExampleScore a leaf-photo model

A farm app is tested on 100 leaf photos it never trained on. 20 leaves are really diseased. The app caught 16 of them, missed 4, wrongly flagged 8 healthy leaves, and correctly cleared 72 healthy leaves.

Step 1 — Fill in the four boxes
True positive 16 · false negative 4 · false positive 8 · true negative 72. They add up to 100, one box per photo.
Confusion matrix lab · "positive" = the thing you're looking for
Model said
positive
Model said
negative
Really
positive
Really
negative

The leaf-photo example from this page.

Accuracy
88.0%
88 right out of 100
Precision
66.7%
16 real out of 24 flagged
Recall
80.0%
16 caught out of 20 real

The cost of each mistake

A false negative and a false positive are different mistakes with different prices. For a peanut-allergy scanner, a miss can put someone in the hospital; a false alarm only sets aside a safe snack. For a school's spam filter, a false alarm can bury an important message; a miss is just one more junk email. Tuning a model to make fewer of one mistake usually means more of the other, so people have to decide the trade-off. That decision is a human one, and it should be made on purpose.

Principles of AI · 1.1.2⚠ Watch Out

The Accuracy Trap

Suppose only 5 out of every 100 leaves are diseased. A "model" that never looks at the photo and always answers "healthy" is right 95% of the time. It sounds excellent. It also catches zero diseased leaves. When one class is rare, accuracy can hide a useless model. Recall and the confusion matrix expose it right away.

The same thing happens inside training. In the flower lab you trained with 50 daisy images and only 10 rose images. A lopsided set pushes the model toward the common class. On a test set with the same mix, a model that always said "daisy" would score 83.3% without learning anything about roses.

Lopsided data lab · 50 daisies in the training set

Roses are 16.7% of the images. A model that ignores the picture and says "daisy" every time would be right 83.3% of the time on a test set with the same mix. So a score in that range doesn't yet show the model has learned anything about roses.

🔑More examples usually help, but balance and variety matter as much as size. Ask three questions of any training set: how many examples are there, how many of each class, and do they cover the different situations the model will meet (lighting, angles, backgrounds, kinds of flower, kinds of person)?
Principles of AI · 1.1.2builds on PLTW 2.1.3Concept

How a Model Learns: Guess, Measure the Loss, Adjust

Teachable Machine hides the learning inside a progress bar. The idea underneath is simple enough to try by hand. A classifier looks for a decision boundary: a line (or, with more features, a surface) that separates one class from the other. It starts with a guess. It counts how badly that guess does. It adjusts the line and tries again.

The measure of "how badly" is called loss. A big loss means the line is doing a poor job; a loss near zero means it's doing well. Learning is the process of nudging the line's numbers (here, its slope and its shift) so the loss goes down. Those adjustable numbers are the model's weights. Your dice models in 1.1.1 had a few counts; real models have billions of weights, but they are tuned by this same guess-measure-adjust loop.

Boundary lab · teach a line to tell daisies from roses
how white the petals are →petal count →
● daisy   ● rose  Above the line = "daisy". A circled point is a mistake; its dashed red line is its share of the loss.
Mistakes
9 of 21
Loss
121.0

Best any straight line can do on this data: 1 mistake (that odd daisy looks like a rose).

Try it two ways. First, drag the sliders yourself and watch the mistake count and the loss. Then reset and press Let it train: the computer tries small random changes and keeps any change that lowers the loss. Notice that it stops improving before it reaches zero mistakes. The odd daisy sits in rose territory, and no straight line can fix that. Real data is full of cases like this, and engineers have to decide whether to use a more flexible model, fix a wrong label, or accept the error.

Predicting a number: regression

When the answer is a number, the model draws a best-fit line through the data instead of a boundary between classes. The loss is how far each prediction is from the real value. A common version is mean squared error: square each miss (so big misses count a lot and the sign doesn't matter), then take the average. For example, if a model predicts 3, 5, and 8 when the real values are 4, 5, and 6, the misses are −1, 0, and 2. Squared they are 1, 0, and 4, and the average is (1 + 0 + 4) ÷ 3 ≈ 1.67. Training tries line after line until that number is as small as it can get.

💡A test set works for regression too: measure the loss on held-out data, not the data you trained on. The principle from the train-and-test section carries over unchanged.
Principles of AI · 1.1.2Concept⚠ Watch Out

Who Was the Model Built On?

A model learns whatever its training examples teach it, including their gaps. If some people or situations are scarce in the data, the model has had little chance to learn them. An overall accuracy number averages everyone together, so it can look fine while the model fails a particular group.

A real case: Gender Shades

In 2018, researchers Joy Buolamwini and Timnit Gebru tested three commercial systems that guess a person's gender from a face photo. They built a more balanced test set and reported results by group instead of one overall score. The systems were close to perfect for lighter-skinned men (error rates under 1%) and much worse for darker-skinned women, with error rates as high as about 35% for the worst system. They also pointed out that benchmark datasets used at the time contained mostly lighter-skinned faces. The companies involved later reported making improvements.

The lesson is about method, not just that one study. Report results by group, not only overall, and build test sets that include the people the system will actually meet. Face-recognition errors have had real consequences: in the United States, several people have been wrongly arrested after a face-recognition match that turned out to be wrong.

Where bias can come from

Who is in the data
Few examples of some groups or situations, so the model has little to learn from.
How it was labeled
People labeled the data. Rushed, mistaken, or one-sided labels become the model's "truth."
What was measured
A shortcut like "green background = daisy" can be learned instead of the real thing.
How it was tested
A test set that doesn't match real use, or one overall score, can hide a failing group.

Five questions before you trust a model

  1. Was it tested on data it never trained on?
  2. What does the confusion matrix look like, not just the accuracy?
  3. Which mistake is worse for this job, and which one does this model make more?
  4. Who and what was in the training data, and who was left out?
  5. Were the results checked group by group, and by whom?
🔑A model is a tool built by people, from data chosen by people, tested by people. "The AI said so" is never the end of the question. It is the start of the questions above.
← Back to Activity 1.1.2📝 Formative Activity →