Data Engineering For Ml

Wrong Labels: How Many Can a Model Survive, and Can You Find Them?

0 of 16 complete

0%

Contents

Back|Data Engineering For MlWrong Labels: How Many Can a Model Survive, and Can You Find Them?
1/16
29 min left
Prerequisites
Data Labeling and Annotation: The Expensive Bottleneck of Supervised MLrequired
1 of 16

Marking Papers at Midnight

Picture a teacher marking a tall pile of worksheets late at night. Most of the marks are right. A few are wrong: a 3 marked as an 8, a correct answer crossed out. By the bottom of the pile, tired eyes make more of these slips.

Now imagine a student who learns only from those marked sheets. Every wrong mark teaches them something false.

A flat illustration of a tired person at a kitchen table late at night, marking a tall stack of worksheets covered in handwritten digits with a red pen, a few red marks on the sheets, a mug of tea beside them. Beneath it: a model learns from its labels the way a student learns from marked work, wrong marks included.

A machine learning model is that student. It learns from examples that people have labelled, and some of those labels are always wrong. In this lesson I measure how many wrong labels a model can survive, whether it matters which labels are wrong, and whether you can find them without re-checking every row by hand.

The Words You Need First

A hand-drawn list of six words, each with a short meaning: label, training set, test set, wrong label, accuracy, seed.

Label. The answer a person attached to an example. For a picture of a handwritten 7, the label should be "7". Usually it is right, sometimes not.

Training set. The labelled examples the model learns from.

Test set. Separate labelled examples, kept aside, used only to check the model afterwards. The model never learns from them.

Wrong label. A label that does not match the example. Engineers also call these noisy labels, or label noise.

Model. Here, a program that learns to tell the digits apart from the training set.

Accuracy. The share of test examples the model gets right. When I say a model "lost 3 points", I mean its accuracy fell by 3 percentage points, say from 97% to 94%.

Seed. A number that fixes which rows are picked at random, so a run can be repeated exactly. Using many seeds shows how much a result moves just from the luck of which rows were picked.

Two Kinds of Wrong Label

Not all wrong labels are alike, and the lab tests two kinds.

Two hand-drawn cards. Random: a 3 labelled 6, a 7 labelled 2, mistakes scattered with no pattern. Look-alike: a 3 labelled 8, a 4 labelled 9, the same confusable pairs again and again.

Random. Each wrong label is a different digit picked at random. A spilled drink or a slipped finger makes these: mistakes with no pattern.

Look-alike. Each wrong label swaps a digit for its look-alike partner: 3 and 8, 4 and 9, 1 and 7, 5 and 6. A tired person makes these: the same confusions, again and again. The digits 0 and 2 have no partner here, so they are never swapped.

Why should the kind matter? Take random mistakes at 40 percent. Of all the 3s, 60 percent are still labelled 3, and the wrong ones are spread over the other nine digits, about 4 percent each. The true label still wins any vote by a mile. Look-alike mistakes all point the same way, so a wrong answer can gather real support.

How the Lab Was Built

A flowchart. 1197 training digits get some of their labels made wrong, from 0 to 40 percent, in two ways. Four models learn from them. Each is checked on 600 test digits whose labels were not touched. Everything is repeated with 20 seeds.

Only the training labels are changed here, so the test score shows what each model really learnt. A later slide changes the test labels on purpose, as a separate experiment.

  1. The data. 1,797 real scans of handwritten digits, 8 by 8 pixels each, from scikit-learn, a free Python library. 1197 are the training set and 600 are the test set.

  2. The wrong labels. A program changes a chosen share of the training labels: 0, 5, 10, 20, 30 or 40 percent, randomly or between look-alikes. The program knows exactly which labels it changed, so the lab can check any way of finding them.

  3. The models. Four common kinds:

    • logistic regression, which draws straight dividing lines between the digits;
    • a random forest, which lets 300 small decision trees, each a chain of simple yes-or-no questions about the pixels, vote;
    • nearest neighbour, which copies the label of the single most similar training digit;
    • 15 nearest neighbours, which lets the 15 most similar training digits vote.
  4. The score. Accuracy on the 600 test digits.

  5. Repeats. Every setting runs with 20 seeds, so each result is an average of 20 runs.

The Lab, Running

This is a real recording of the lab's report, printed on the laptop where it ran. The whole lab takes about four minutes on a laptop, with no special hardware.

A terminal recording of the command python3 label_noise.py --report --sections 1,2,3,4. It prints test accuracy for each model at each share of wrong labels, first random and then look-alike, then how many wrong labels two ways of flagging caught and what fixing them did, then what wrong labels in the test set do to the measured score.

Each accuracy is the average over the seeds, with the lowest and highest in brackets. In the recording, "knn1" is nearest neighbour, "knn15" is 15 nearest neighbours, and "confusion" means look-alike mistakes.

One correction first. My first run of this lab made a mistake in how it prepared the pixels before training, and a reviewer caught it. Under that mistake, logistic regression lost 8.6 points at 20 percent wrong labels, which made it look far more fragile than it is. Every number here comes from the corrected run.

Random Wrong Labels

A line chart of test accuracy against the share of training labels made wrong at random, from 0 to 40 percent, one line per model.

wrong labelslogisticforestnearest 1nearest 15
0%97.7%98.1%99.2%97.5%
5%96.2%98.1%93.8%97.4%
10%95.4%98%89.1%97.4%
20%93.9%97.4%79.6%97.4%
30%92.2%96.4%69.5%97.2%

Look-Alike Wrong Labels

A line chart of test accuracy against the share of training labels swapped between look-alike digits, from 0 to 40 percent, one line per model.

wrong labelslogisticforestnearest 1nearest 15
5%95.8%97.9%93.6%97.5%
10%94.1%97.5%89.2%97.3%
20%89.7%94.3%79.2%95.5%
30%79.1%82.3%69.9%84%
40%55.8%58.5%59.5%58.3%

From 20 percent up, the same share of wrong labels did much more damage when the mistakes shared a pattern. At 30 percent, the random forest scored 96.4% with random mistakes and 82.3% with look-alike ones. At 40 percent every model fell to between 55.8% and 59.5%.

Is the Difference Real?

For each model and each rate, the lab ran 20 seeds with random mistakes and 20 with look-alike ones. Line up run 1 with run 1, run 2 with run 2, and so on, and ask for each pair: which kind of mistake left the higher score? A pair where both scored exactly the same counts for neither side. The two runs in a pair do not have the same rows made wrong, because the two kinds of mistake pick their rows differently, so this is a rough comparison, but a very lopsided count still means something.

For each model at 20 percent wrong labels, out of 20 pairs of runs: how many scored higher with random mistakes, and how many with look-alike ones.

At 20 percent wrong, random mistakes scored higher in all 20 pairs for logistic regression, the forest, and 15 nearest neighbours. If the two kinds were equally harmful, that would be like 20 coin flips all landing the same way, which happens about twice in a million tries.

At 10 percent the gap was already clear for logistic regression, 17 pairs to 2, which luck would give about once in 1,372 tries. The forest leaned the same way, 14 to 4. At 5 percent the two kinds were too close to call for every model, and 15 nearest neighbours even leaned slightly towards doing better with look-alike mistakes.

Nearest neighbour never showed a clear difference at any rate. It copies whichever label is nearest, so it cannot learn a pattern from the mistakes, good or bad.

Can You Find the Wrong Labels?

Re-checking every label by hand is slow and costly. A common trick is to let a model point at the labels it finds suspicious.

A bar chart for 20 percent random wrong labels, out of 239 wrong labels: how many were found by flagging every disagreement (307 flags), by flagging the 239 most suspicious rows when told the count, and by picking 239 rows at random.

Here is the trick. Split the training set into 5 parts. Train a model on 4 parts and ask it about the fifth, the part it has not seen. Do that 5 times, so every row gets a prediction from a model that never saw its label. Engineers call this an out-of-fold prediction.

Why not ask a model about rows it trained on? Because it has already learnt their labels, wrong ones included, and would vouch for them.

With 20 percent random wrong labels there were 239 wrong rows. The simplest flag is to mark every row where the out-of-fold prediction disagrees with the given label. That flagged 307 rows, and 237 of them really were wrong: it found 99 percent of the wrong labels, and 77 percent of its flags were real. Picking 307 rows at random would have found about 61.

The lab also tried a version that was told how many rows were wrong, 239, and flagged the 239 rows whose label looked least likely to the out-of-fold model. It found 222, against about 48 for a random pick of the same size. On your own data you are never told that number, so the first version is the one you can use. A free Python tool called cleanlab estimates that number for you. This lab uses only the simplest ranking, the first step of a technique called confident learning.

Fix Them, or Throw Them Away?

Once rows are flagged, you can remove them, or have a person check and correct them.

A chart for random wrong labels at each rate: logistic regression accuracy with the noisy labels, after removing the flagged rows, after fixing the flagged rows, and after fixing the same number of rows picked at random.

random wrong labelsnoisyremove flaggedfix flaggedfix random rows
10%95.4%96.8%97.2%95.6%
20%93.9%96.3%97.1%94.7%
40%89.5%94.4%96.5%93.1%

These are logistic regression scores. To keep the comparison with random rows fair, both used the same number of rows, the true number of wrong ones. Clean labels scored 97.7%.

Checking the flagged rows beats checking the same number of rows at random: at 20 percent, 97.1% against 94.7%. Flagging sends a person's time to where the mistakes are.

Removing the flagged rows costs no one's time, and at low noise it comes close to fixing. It falls behind as the noise grows, because the rows it throws away are mostly the wrong-labelled images themselves, which fixing would have kept with the right label. My rule: remove when the noise is low and people's time is short, and send rows to a person when the noise is high. When the mistakes share a pattern, the next slide shows why flags alone are not enough.

When the Finder Is Fooled

The finding trick has a weak spot, and look-alike mistakes hit it.

For look-alike wrong labels at each rate: logistic regression accuracy with the noisy labels, after removing the flagged rows, after fixing the flagged rows, and after fixing random rows.

Take the version you can actually use, flagging every disagreement. At 20 percent look-alike mistakes it still found 94 percent of the wrong labels, close to the 99 percent it found with random ones. But it needed more flags to do it: 325 against 307, so only 69 percent of its flags were real, against 77 percent.

At 40 percent it broke down. It flagged 516 rows and only 256 of them were wrong, about half of the 479 wrong labels. Fixing the most suspicious rows even scored 83.6%, lower than fixing random rows at 86.7%, because the flags piled onto some digits and left others, while random fixing spread the corrections evenly.

The reason: the finder is itself a model trained on the noisy labels. When the mistakes share a pattern, it learns some of the pattern too, and those wrong labels start to look normal to it. At 40 percent, about half of each pair is wrong, so no method could tell the true label. A finder can only spot mistakes that disagree with the rest of the data, which is why patterned mistakes need a better labelling guide as well as checking.

Wrong Labels in the Test Set

So far only the training labels were wrong. What if the test set, your answer key, has wrong labels too?

A chart of one unchanged model's accuracy at each share of wrong labels in the test set: its true accuracy stays flat, and its measured accuracy falls in a straight line.

The lab took one logistic regression model trained on clean labels, and scored it against copies of the test set with random wrong labels. The model never changed. Its true accuracy stayed at 97.7%. Its measured accuracy fell: 88% with 10 percent wrong labels in the test set, 78.2% with 20 percent, 58.9% with 40 percent.

A wrong answer key punishes the model for being right. It also caps your score: with 20 percent of the answer key wrong, even a perfect model could only score about 80 percent.

These were random mistakes, which lower every model by about the same amount. If your test labels come from the same tired labellers as your training labels, their mistakes match, and the answer key rewards the model that learnt those mistakes. That can change which model looks best. When a model seems stuck, check the test labels before you blame the model.

Compare the Two Kinds Yourself

This box holds the real per-run accuracies from the lab: for three models and each share of wrong labels, 20 runs with random mistakes and 20 with look-alike ones. Press Run to compare them pair by pair.

Then change MODEL and RATE. Try "knn15" at 5, then "forest" at 30. In the code, knn15 means 15 nearest neighbours.

The last line is a chance written as a decimal. 1.9e-06 means about 2 in a million, and 0.3 means 3 times in 10, which is easily luck.

What This Lab Cannot Tell You

Two columns. What the lab shows: how four kinds of model coped with two kinds of wrong label on handwritten digits, how well a simple finder caught them, and what a wrong answer key does to a score. What it cannot show: bigger datasets or other kinds of data, deep neural networks, the kinds of mistakes your own labellers make, and the cost of re-checking.

One small dataset. 1,797 small images of digits. Bigger datasets, text, or images of the real world may behave differently.

No deep networks. Large neural networks, the kind of model behind image and speech tools, can also learn wrong labels by heart if trained long enough. They tend to learn the clean patterns first, which is why stopping their training early helps. This lab did not test them.

Made-up mistakes. A program chose the wrong labels, so the lab knows exactly which they are. Your labellers make their own kinds of mistake, probably a mix of both kinds here, and you never have the full list.

A perfect re-checker. When the lab "fixed" a flagged row, it used the true label. A real person re-checking will make some mistakes too.

One division, one finder. The same 600 test digits were kept aside in every run, so the seeds show how results move with the mistakes, not with which digits were kept aside. The finder and the model retrained after fixing were both logistic regression.

What to Do on Monday

A hand-drawn list of five steps: check the test labels first; flag suspicious training labels with out-of-fold predictions; send the flagged rows to a person; look for repeated confusions; and prefer models that let many examples vote.

  1. Check the test labels first. Have two people re-label about 200 random test rows without seeing the old labels, and see how often they agree with each other and with the old label. The labelling lesson shows how to measure agreement. Every wrong label in your test set lowers your score and hides real progress.

  2. Flag suspicious training labels. Use out-of-fold predictions, and flag the rows where the prediction disagrees with the given label.

  3. Send the flagged rows to a person, when you can afford it. Fixing flagged rows beat fixing random rows at every rate of random mistakes here. When time is short and noise is low, removing them is a free second best.

  4. Look for repeated confusions. Count which pairs of labels get swapped. A finder is fooled by mistakes that share a pattern, so those need a clearer labelling guide, not just more checking.

  5. Try a model that lets many examples vote when your labels are messy. Here the forest and 15 nearest neighbours shrugged off random mistakes that sank nearest neighbour.

A closing card. In large type: 237 of 239. Below: wrong labels found by flagging every row where an out-of-fold prediction disagreed, at 20 percent random wrong labels, from 307 flags. Then: let a model point, and let a person check.

A model can point you to most of your wrong labels, as long as the mistakes do not share a pattern, and a person's time is best spent where it points.

Knowledge Check

Knowledge Check

4 questions - Score 80% to pass

Q1

Why did nearest neighbour lose accuracy about as fast as the labels went wrong?

Q2

From 20 percent up, why did look-alike mistakes hurt more than random ones?

Q3

Why does the finder use out-of-fold predictions?

Q4

An unchanged model's measured accuracy dropped from 97.7 percent to about 78.2 percent. What changed?

40%
89.5%
94.9%
59.8%
96.5%

Nearest neighbour falls about as fast as the labels go wrong, from 99.2% to 59.8%. It copies the label of the single closest example, so every wrong label it lands on goes straight into its answer.

The random forest and 15 nearest neighbours barely move at any rate tested: from clean to 40 percent wrong, the forest lost 3.2 points and 15 nearest neighbours 1.0. Both let many examples vote, and random mistakes are outvoted.

Logistic regression sits in between. It lost 3.8 points at 20 percent wrong.

Part of the reason is that look-alike swaps land only on the 8 digits that have a partner: 960 of the 1197 training rows. So 40 percent wrong overall means about 50 percent wrong inside those digits. When half of the 3s are labelled 8 and half of the 8s are labelled 3, no model can tell which label is true. Even 20 percent overall is 25 percent inside the pairs.

That packing is also a caution. Part of the gap between the two kinds comes from look-alike mistakes being crowded onto fewer digits, not only from their pattern. And the lab only swaps both ways equally. Real tired labellers often err one way, reading 3 as 8 far more often than 8 as 3, and I did not test that.