Skip to content

← Grade 7 Computing

How Machines Learn

Unit 10 · Artificial intelligence

Hide the stories, facts, photos, activities and interactives, for revising before an exam
In this unit 8 sections
  1. Features and labels
  2. Training and testing
  3. One-sided examples: bias
  4. Where bias gets in
  5. Fixing a model by fixing its data
  6. Project
  7. In short
  8. Exercise

Two bars of soap sit in a dish, one orange and one green, in a home in Nepal. Any child would know what they are. In 2019 a team of researchers showed a photo of them to the picture-recognizing AI of six companies, Microsoft, Google and Amazon among them. Every one of them answered food. One added “cheese”, “bread” and “cake”; another, “sweets” and “burger”. Not one said soap. Shown a bottle of liquid hand soap beside a sink in a home in Britain, two of the same systems said “soap dispenser”.

Nothing in their programs was broken. Each had learned from millions of photos, but far fewer of those came from homes like this one, and in a home with less money, everyday things can look different (soap in a bar, not a bottle) or sit in a different place (a toothbrush kept outside the bathroom). To these models, two bars on a dish looked more like something to eat.

In Grade 6 you trained a fruit sorter and saw that a model is only as good as its examples. In this unit you will look closely at the examples themselves: what a model is told about each one, and the right answer it must learn; how to test a model fairly, and measure how good it is; how bad or one-sided examples make a model that works well for some people and badly for others; and how to make it better by fixing its data. You will test models on this page, find a bad example, catch a model being unfair, and fix it.

Features and labels

Think of a fruit seller at a collection center in the Terai in the mango season. A cart of mangoes comes in, and she sorts them: ripe ones to sell today, the rest to wait a few days. She looks at each mango’s skin and presses it gently with her thumb. In a second she knows.

Now imagine a machine to help her. A camera looks at each mango and gives its skin color as a number, from 0 (green) to 1 (yellow). A soft pad presses it and gives its softness, a score from 0 (hard as a stone) to 10 (very soft). (Both scores are our own, made up for this unit; a real machine would give its own numbers.) To teach it, she sorts a few hundred mangoes herself, and for each one writes down the machine’s measurements and her own answer: ripe, or not ripe.

Features: what is measuredLabelMangoSkin colorSoftness0 to 10WeightRipe?1green2310 gNot ripe2yellow8280 gRipe3yellow-green4350 gNot ripe4yellow9240 gRipe…One row:one exampleAll the rows together: the training data. FeaturesLabel#Skin colorSoftness0 to 10WeightRipe?1green2310 gNo2yellow8280 gYes3yellow-green4350 gNo4yellow9240 gYes…The shaded row is one example.All the rows together: the training data.
Figure 10.1 Training data for a mango model, as a table. Each row is one mango: one example. The columns measured about it are its features; the last column, the right answer, is its label.

Each row of Figure 10.1 is one example. The things measured about every example, here its skin color, softness and weight, are its features. The right answer for that example, the thing the model must learn to give, is its label: here, ripe or not ripe. A collection of examples like this is a dataset, and the examples a model learns from are its training data.

You have met these before without the names. In Grade 6, the fruit sorter’s features were a fruit’s size and color, and its label was kagati or suntala. In a spreadsheet (Unit 5), each example would be a row, each feature a column, and the label one more column.

Choosing the features

A model knows only its features. It never sees the mango, only the numbers. So the features must be ones that change with the label. Softness and skin color change as a mango ripens, so they help a lot. Weight depends mostly on how big the mango is, not on how ripe it is, so it helps very little. The day of the week the mango was picked would not help at all.

For photos and voices the features are numbers too: the red, green and blue of every pixel in a photo (Unit 2), or how loud a sound is, thousands of times a second. A photo has millions of these numbers. Nobody picks out the useful ones by hand; the model finds its own patterns in them, such as edges, shapes and textures. That is why it can learn things no one thought to tell it, and also why it can learn the wrong thing: if every photo of a cow among its examples was taken in a green field, it may learn “green field” as much as “cow”.

Labels come from people

Somebody has to give every example its label: the fruit seller pressing each mango, a doctor marking X-rays, people paid to name photos, as in Grade 6’s story of ImageNet. Labeling takes a long time, and people make mistakes, or don’t agree. A model can’t tell a wrong label from a right one. It learns them both.

Examples and nonexamples

To learn what “ripe” is, a model must also see what “ripe” is not. Things that are not what the model is looking for are called nonexamples: here, the unripe mangoes, which are rows of the dataset like any other, with the label not ripe. A model shown only ripe mangoes would have nothing to compare them with. It would call every mango ripe, and a stone too.

The most useful nonexamples are the ones that look a little like the examples. A model that finds momos in photos needs photos of momos, and also many photos that are not momos: plates of idli, a bowl of rice, a white cloth folded on a plate. They teach it exactly where “momo” ends.

Try it

Feature, label, or no help?

Imagine a model that reads each SMS and decides: scam or safe? For each card, decide what it would be in the training data.

whether the message has a link in it

For a model that spots scam SMS: a feature that helps, the label, or no help?

Tap a group above.

Card
1 of 10
Right the first time
0

A model learns to tell whether a photo shows a dog or a cat. Which is the label of an example?

Pick an answer to see why.

Why does a model that finds momos in photos need photos of things that are not momos?

Pick an answer to see why.

Training and testing

Imagine your teacher gives you twenty math questions to practice, with their answers. A week later the test is the same twenty questions. A friend who understood none of it, but learned the twenty answers by heart, gets full marks. The test has told the teacher nothing. A good test asks new questions: like the ones practiced, but not the same.

A model must be tested the same way. Remember how a nearest-neighbor model works: it gives the label of the example closest to the new thing. Ask it about one of its own examples, and the closest example is that very one, at no distance at all. So it gives that example’s label, every time. Tested on its own training examples, it would score 100%, even if it were useless on anything new. And handling new things is the whole job.

So before training, the examples are split in two:

  • the training set: the examples the model learns from;
  • the test set: examples kept apart, never used in training, and used only to check the model at the end.

Often about one example in five is kept for testing, picked at random so that the test set is like everything else. After training, the model answers for each example in the test set, and its answers are compared with their labels.

All the examples: 20each with its labelTraining set: 15Test set: 5, kept aparttrainsThe model✓✓✓✓✗4 of 5 rightaccuracy 80% All the examples: 20each with its labelTraining set: 15Test set: 5kept aparttrainsThe model✓✓✓✓✗4 of 5 rightaccuracy 80%
Figure 10.2 Twenty labeled mangoes (each drawn in its skin color, with a ring for its label: blue for not ripe, red for ripe) are split before training. Fifteen train the model; five are kept apart. The model then answers for the five test mangoes: it gets four right.

Accuracy

The simplest measure of how good a model is, is its accuracy: the share of the test examples it got right, usually as a percentage. (In Unit 5, accuracy meant how close one value is to the truth; for a model, it is how often its answers are right.)

accuracy=number rightnumber tested×100% \text{accuracy} = \frac{\text{number right}}{\text{number tested}} \times 100\%

In Figure 10.2 the model got 4 of the 5 test mangoes right, so its accuracy is 4 ÷ 5 × 100% = 80%.

A bad example

In this model, the fruit seller has made one mistake. A ripe mango, yellow and soft, was written down as not ripe. Find it.

Try it

Find the wrong label

Fifteen mangoes trained this model; 12 more are kept for testing. Each is drawn in its skin color, with a ring for its label: blue for not ripe, red for ripe. Press Test all 12 mangoes and look at the ✗ marks: which example is closest to each? Find the example whose label looks wrong, and tap it to take it away. Then compare Now with the First test.

0246810Softness →greenyellow↑ Skin color

Training examples: 15

ScoreRight
First test– 
Now– 

Press Test all 12 mangoes. The model answers for each test mango, and its score is kept.

Took that example away. Compare the score now with the first test.

0 of 0 right (–). The ✓ and ✗ on the map show which.

With no examples, the model knows nothing and can’t answer at all.

This model runs in your browser. Nothing you do here is sent anywhere.

What you should see

So one wrong label cost this model two right answers. Fixing the data, and not the program, made it better. That is how most models are improved in real life.

Why is a model tested on examples that were not in its training set?

Pick an answer to see why.

A model got 18 of 24 test examples right. What is its accuracy?

Pick an answer to see why.

One-sided examples: bias

Now imagine that the collection center starts buying from a new farm, which grows Bambaiya mangoes, one of the common kinds in Nepal. A Bambaiya is different: it is often still mostly green when it is ripe. The center’s model learned only from mangoes of kinds that turn yellow as they ripen. What will it say?

A hand holding a large oval mango, pale green with a whitish bloom and patches of bright green
Figure 10.3 A Bambai, or Bambaiya, mango from Madhubani, just across the border from Nepal. Its skin often stays green as it ripens, so its color says little about whether it is ready. Photo: Sntshkumar750, CC BY-SA 4.0

Try it

One-sided examples

The center, its model and its mangoes are imagined, but the problem is real. This model learned from 14 mangoes, all of kinds that turn yellow. Its 12 test mangoes are 6 of those kinds and 6 Bambaiya. Press Test all 12 mangoes and read the score for each kind. Then fix the model by collecting more examples. Does it help to collect more of the other kinds? More Bambaiya?

0246810Softness →greenyellow↑ Skin color
Collect more examples:

Training examples: 14

ScoreAllOther kindsBambaiya
First test– – – 
Now– – – 

Press Test all 12 mangoes. The model answers for each test mango, and its score is kept.

Took that example away. Compare the score now with the first test.

0 of 0 right (–). The ✓ and ✗ on the map show which.

One more example collected. Compare the score now with the first test.

With no examples, the model knows nothing and can’t answer at all.

This model runs in your browser. Nothing you do here is sent anywhere.

What you should see

A model that works well for one group and badly for another, because of how it was made, is said to be biased. Here the cause was one-sided data: training data with none, or too few, of some of the things the model will meet. It is one of the commonest causes of bias in AI. It is the same problem you met in Unit 5’s survey, where asking only one class gave answers that didn’t stand for the whole school.

Look at what the total hid. 75% sounds fairly good. Only the score for each group showed that the model was right about every other mango and wrong about half the Bambaiya. Worse, if the center had tested it only on mangoes of the other kinds, it would have scored 100%, and the center would have bought it happily. A one-sided test set gives a misleading result.

A model is tested on 100 mangoes: 80 of a kind that turns yellow, and 20 Bambaiya. It gets 78 of the first kind right and 10 of the Bambaiya right. Which sentence is true?

Pick an answer to see why.

Bias you can meet

The mango model is made up, but real AI has been caught being one-sided in exactly this way, and some of it is AI you use.

Photos. In 2017, researchers at Google looked at where the photos came from in ImageNet, the famous collection from Grade 6. Of the photos whose country they could find, about 45% were from the United States. India, with far more people, had about 2%. They then tested models trained on such collections with photos of bridegrooms at weddings in India, found by people in Hyderabad. The models recognized them much less well than the bridegrooms in their usual test photos; one often called them “chain mail”, a kind of armor. The soap at the start of this unit is the same story: the 2019 study found that, for things photographed in the homes with the lowest incomes (under about US$50 a month), the systems were right about 10% less often than in the homes with the highest (over US$3,500).

A model that learned what a house looks like mostly from photos of city streets, concrete houses several stories tall, may not see a house at all in a village like the one below.

Houses of stone and mud with flat roofs, built in rows up a dry hillside below a forest of pine trees
Figure 10.4 A village in Mugu district. Its houses of stone and mud, with flat roofs, look very different from the city houses most photo collections are full of. Photo: SushobhanBhandari, CC BY-SA 4.0

Voices. Voice typing and voice assistants learn from recordings of people speaking, with what they said written out. Far more such recordings exist in English than in Nepali, and fewer still in Maithili, Bhojpuri, Tamang or Nepal Bhasa. So voice AI understands English best, and Nepali less well. Even within Nepali, a model that learned mostly from people in Kathmandu may hear a speaker from Jumla or Janakpur less well: their accents were rare among its examples.

Faces. Face unlock on a phone, and the cameras that check faces at some offices and airports, use models trained on photos of faces. If most of those faces had light skin, the model works worse for people with darker skin.

A portrait of a young woman with dark curly hair and red-framed glasses, in a white jacket
Figure 10.5 Joy Buolamwini in 2018. She calls bias that is built into technology “the coded gaze”. Photo: Niccolò Caranti, CC BY-SA 4.0

A voice typing model makes many more mistakes for people from one district than for people from Kathmandu. What is the most likely cause?

Pick an answer to see why.

Where bias gets in

Nobody decides to build a biased model. Bias gets in through ordinary decisions that people make at each step of working with data, often without noticing. At each step, there is a question to ask.

  1. Collecting the data. Who and what is in it, and who is missing? (Only city photos; only Kathmandu voices; only kinds of mango that turn yellow.) Ask: does the data include every kind of person and thing the model will be used for, in fair numbers?
  2. Labeling it. Who gives the right answers, and could they be wrong or one-sided? (A tired labeler mixing up two leaf diseases; labelers who think a wedding dress must be white.) Ask: who labeled it, how were the labels checked, and do they agree?
  3. Choosing the features. Does a feature stand in for something unfair? (“Has internet at home” as a feature for a scholarship says more about a family’s money than about the student.) Ask: could this feature treat a group unfairly?
  4. Testing. Is the test set like the real world, and is the score given for each group? (The mango model tested only on yellow kinds.) Ask: was it tested on everyone it will be used on, group by group?
  5. Using the model. Is it being used on people or things it was never trained for? (A model made in another country, used unchanged in Nepal.) Ask: is it being used where its data came from?

The same goes for conclusions drawn from data without any AI at all. A survey that only asked people with a phone, or a total that hides a group, can lead to a wrong conclusion just as easily as to a wrong model.

Try it

Where did the bias get in?

Each card describes a model that ended up biased. Decide at which step of working with the data the bias got in.

A voice typing model is trained on recordings made almost only by young men in Kathmandu.

Where did the bias get in?

Tap a group above.

Card
1 of 10
Right the first time
0

Fixing a model by fixing its data

You fixed two models on this page by changing their data: taking away a wrong label, and adding the missing Bambaiya. People who build AI improve models the same way. A good routine:

  1. Test, group by group. Find the accuracy for each group of people or things the model will meet, not only the total.
  2. Look at the mistakes. What do the ✗ have in common? Are they all one kind? Are they near an example with a strange label?
  3. Fix wrong labels. Check the examples near the mistakes, and correct or take away the bad ones.
  4. Collect what is missing. Add examples of the group it does badly on, and nonexamples that look like them: for the Bambaiya, ripe ones and unripe ones, so the model learns what tells them apart (softness, not color).
  5. Keep it balanced. More of what the model already knows doesn’t help, as you saw. Aim for every group to be well covered.
  6. Test again, on examples the model has never seen, and record the results before and after, group by group, so you know the change helped and broke nothing else.

Sometimes data can’t fix a model. If good examples can’t be collected, or if a mistake would be very serious, the job may be better left to people, or the model’s answers checked by a person before anything is decided (Grade 6’s bank). And it helps to have people from many places, and with many backgrounds, building and testing AI: they notice who is missing.

Project

In short

Exercise

In a dataset for a model that predicts whether a bus from Kathmandu to Pokhara will arrive late, which is the label?

Pick an answer to see why.

A model gets 100% on its training examples and 60% on its test set. What does this show?

Pick an answer to see why.

A model was tested on 60 photos and got 51 right. What is its accuracy?

Pick an answer to see why.

A team’s model does badly on Bambaiya mangoes. What will help most?

Pick an answer to see why.

Which of these is a decision made while testing that can give a misleading result?

Pick an answer to see why.

a.

A team in Pokhara wants a model that says each morning whether it will rain in the afternoon. For every day of the past five years they have: the morning’s humidity, the cloud cover at 9 a.m., the wind speed, the month, and whether it rained that afternoon. Name the features and the label. Suggest one more feature that might help, and one that would not.

Answer
b.

Explain why a model must be tested on examples that were not in its training data. Use the example of a nearest-neighbor model.

Answer
c.

A crop disease model was tested on 120 leaf photos and got 102 right.

  • i.What is its accuracy?
  • ii.Of the photos, 90 were of rice leaves and 30 of maize leaves. It got 87 rice photos right and 15 maize photos right. Find its accuracy for each crop.
  • iii.What do your answers show?
Answer
d.

What is bias in an AI model? Explain, with an example, how one-sided training data makes a biased model.

Answer
e.

A model that finds stray dogs in photos from street cameras was trained only on photos taken in daylight. Its results:

Daytime photos Night photos
Before 47 of 50 21 of 50
After adding 500 night photos to its training data 46 of 50 44 of 50
  • i.Work out every accuracy, and the accuracy over all 100 photos before and after.
  • ii.Did the change work? Explain, using your numbers.
  • iii.Why should the night photos used for testing not be among the 500 added to its training data?
Answer
f.

Name the five steps of working with data where bias can get in. For each, give one example and the question to ask.

Answer
g.

What are nonexamples? Give two nonexamples that would be useful for a model that finds momos in photos, and say why look-alikes are the most useful.

Answer
h.

Of every 100 messages a bank’s customers get, 98 are real and 2 are scams. A company says its scam-spotting model has 98% accuracy. Why should the bank not be impressed yet? What would you ask the company?

Answer
i.

Your friend says: “AI is a machine, so it can’t be unfair to anyone.” Write a reply.

Answer
j.

A company wants to make voice typing that works well for everyone in Nepal. Write a plan for its data: who to record, how to label, and how to test.

Answer