Deep Learning, Visually #22
Watch the Short: youtube.com/watch?v=qxIv68yQA0A
A test set is supposed to tell you how good your network really is. It can only do that if you never use it to choose anything. In this article we break that rule on purpose, in a real experiment, and measure what it costs.
An honest score
We train a small digit network on 1,000 images: 26,432 weights, Adam, the same setup as the runs in this series. We stop at epoch 14, where its loss on new images is lowest.
We keep 200 more images as a test set. The network never trains on them, so its score there should be honest. It scores 87.5%, which rounds to 88. On the other 68,600 digit images we have, it scores 87.7%. The small test set is close to the truth.
Chasing a higher score
Now we chase a better number. We add a tiny bit of random noise to every weight (standard deviation 0.005), and we keep the change only if the score on those 200 images goes up. Then we repeat that 3,000 times.
Most tries are thrown away. Six are kept, at tries 1, 3, 6, 11, 30 and 806. The test score climbs from 87.5% to 91.5%.
It looks like progress. It isn't.
The true score
Here we can check, because we have 68,600 images that took no part in any of this. Over the same six changes, the true score falls from 87.7% to 86.5%.
The test score went up by 4 points while the network got worse.
The noise can't teach the network anything about digits, because no image reaches it through a gradient. The changes that got kept are simply the ones that happened to suit those 200 images. Ten of the 200 flipped from wrong to right, and two flipped the other way. That's the whole gain.
So by choosing with the test set, we trained on it. The network never saw a test image during training. The information leaked through our choices.
How much of this is luck?
One run proves little, so we ran the experiment 40 times, each with different images and different noise. On average, the score on the 200 images rose from 87.7% to 91.5%, and the true score fell from 87.7% to 86.6%. It fell in 39 of the 40 runs. We picked the run in the video because it's close to that average.
This experiment is extreme on purpose: pure choosing, with nothing learned. An ordinary search is gentler. When we picked the best of 100 networks by its score on a held-out set, that score flattered the winner by about 1.8 points with 200 images, and 0.5 with 1,000. A smaller set and more tries mean a bigger leak.
Three sets
The fix is a third set.
- The training set learns the weights.
- The validation set does all the choosing: which size, which learning rate, when to stop.
- The test set stays locked until the very end, and you open it once.
The validation score will flatter you, for exactly the reason above. That's fine, because you were never going to report it.
Validation also tells you when to stop training. This is the loss from the full digit run: the training loss keeps falling, but the loss on 10,000 validation images is lowest at epoch 14 and then climbs. Keeping the weights from that point is called early stopping, and it's where the network in this article came from.
Now open a fresh set of 200 images, once. Our network reads 91.5% on the set it chose with, and 87.0% here. The truth is 86.5%. One reading of 200 images is noisy, typically landing within about 2.6 points of the truth. But it isn't biased: across the 40 runs it averaged 86.5%, against a truth of 86.6%.
At full size
The same three sets work at any scale. The full network from the lecture uses 50,000 training images, 10,000 for validation and 10,000 for test. Its validation accuracy was 96.74%, and the test set, opened once, gave 96.91%. With sets that large, a few choices can't move the number by much.
Try it
You don't need a network to see how far luck can go. Take 3,000 imaginary models that are all exactly 88% right, and test each on 200 images.
import numpy as n r=n.random.default_rng(22) s=r.binomial(200,.88,3000)/2 print(n.median(s),s.max()) It prints 88.0 94.5. Every model is the same, but the best one looks more than 6 points better.
Key takeaways
- A score is only honest if nothing was chosen with it.
- Choosing with a set leaks that set into the network, even if the network never trains on it.
- Use the training set for weights, the validation set for every choice, and the test set once, at the end.
- The smaller the set and the more you try, the bigger the leak.
References
- CS878 Deep Learning (Fall 2023), Lecture 9: Learning Rate, Debugging, Best Practices.
- Logit Solutions AI, Neural Networks From Scratch (full lecture), Chapter 14: Generalization. https://youtu.be/uYVk4iFb4uQ
This article is part of Deep Learning, Visually by Logit Solutions AI: short animated explainers that cover one idea at a time, each with a written version like this one.









