Ch 1 ▸ Why not a dense layer? ▸ Module 4/4
Lost neighbors.
🔒 This module is locked
It builds on Counting parameters. Pass that module’s knowledge check to unlock it, or skip ahead if you already know the material.
A dense layer’s output is a sum of products, and a sum doesn’t care what order it’s added in. That has a strange consequence.
Shuffling the pixels
Pick one shuffle of the 256 positions and apply it to the duck. The result looks like noise. Now apply the same shuffle to the weights of the classifier’s duck output. Each pixel moves to a new position, and the weight it was paired with moves to the same new position. Every pixel still meets its own weight, so every product is the same, and the duck score is exactly the same: 3.38.
Input
·
Duck weights+ bias −15.86
=
DUCK SCORE
3.38
Nothing shuffled. The duck matches its weights.
▸ POINT AT A PIXEL TO FOLLOW IT, AND ITS WEIGHT, THROUGH THE SHUFFLE
Shuffle only the input, and the duck score drops from 3.38 to −4.56, because the pixels now meet the wrong weights. The classifier’s answer becomes empty sky. Shuffle the weights too, and the score is back to 3.38.
So imagine training a dense network on images that have all been shuffled the same way. It can do exactly as well as one trained on the normal images: whatever weights work on the normal images work on the shuffled ones, shuffled the same way. Nothing the layer computes depends on which pixels were next to each other. Flattening made the neighbors hard to see, and the dense layer never looks for them.
A person can’t recognize the shuffled duck at all, because people rely on the arrangement: an edge is a run of neighboring pixels that change brightness together, and a shape is built from edges. A dense layer can only find such patterns by learning them, weight by weight, from examples.
Moving the duck
The duck output’s weights are tied to positions: positive where the duck’s body was, negative over the open water. Move the duck, and its pixels meet the wrong weights.
At its home position the duck scores 3.38. Move it one pixel to the right and the score drops to 0.77; two pixels, −0.87. The duck still wins, but only just: empty sky is at −1.20. Three pixels to the right, the duck scores −2.26 and empty sky −0.45, so the classifier’s answer is empty sky. The duck is still there, unchanged, but to this layer the same duck three pixels over is a different pattern.
A network that has to find ducks anywhere in an image would need a separate duck output for every position. Each would be learned separately, from examples of a duck at that position. That’s the same pattern learned hundreds of times over, with a full set of weights each time.
What a better layer would do
The two problems point to two properties a layer for images should have:
- Local connections: each output reads only a small patch of neighboring pixels, not the whole image. The layer then uses the arrangement of the pixels, because its inputs are always neighbors.
- Weight sharing: the same small set of weights is used at every position. A pattern learned once can then be found anywhere in the image, and the number of weights doesn’t depend on the image’s size.
A convolution is a layer with both: a small grid of weights, called the kernel, that slides across the image and computes a dot product at every position. A 3×3 convolution reads 9 neighboring pixels at a time and has 10 parameters. The dense layer that does the same job on the 16×16 duck has 65,792, and it has no idea which of its inputs are neighbors.
Knowledge check ▸ 4 questions
Before you move on.
Answer every question correctly to unlock the next module. A wrong answer gets a hint, and you can try again as many times as you like.
1The input and an output’s weights are shuffled with the same shuffle. What happens to the output?
2What does the shuffle experiment show about a dense layer?
3In figure 1.5, the duck moves three pixels to the right. What does the classifier answer?
4What does weight sharing mean?