Answers · How models work
Why do CNNs use the same weights everywhere instead of learning a separate filter for each spot?
In shortThey share one small filter across every position because a pattern such as an edge means the same thing wherever it appears; the huge cut in parameters is a consequence, not the main reason.
Convolutional networks reuse one small filter at every location because the rule that turns a patch into a feature should not depend on where the patch is. The parameter saving is real, but it is the arithmetic consequence of that constraint rather than the reason for it.
Take a colour photograph 256 pixels on a side. It has 65,536 positions and, with three colours, 196,608 input numbers. A connected layer with as many outputs as inputs needs one weight for each input and output pair, so the count is 196,608 squared, which is 38,654,705,664, about forty billion. That is not a layer one can train on any ordinary data set.
In the first expression carries and , the output position, so every location has its own weights. In the second carries only and , offsets inside a three by three window, and the output position appears only on . The same nine numbers are used at every and . The picture size is absent from the parameter count: a three by three greyscale filter has weights, and a colour layer with 64 filters has weights. The image side length never enters.
It is worth separating the constraints. Restricting each output to a small window is locality. On its own it cuts the weight count a great deal but still leaves each position its own numbers. Forcing all positions to use the same numbers is sharing. A convolution applies both, and the question is about the second.
The saving is the smaller half
It is tempting to read this as a compromise: the connected layer is what one would want, sharing is what one can afford, and something is lost. That reading predicts that convolution would fade as memory became cheaper. It has not, because sharing also encodes a claim about pictures. A vertical edge is a vertical edge in the top left or the bottom right; what makes it an edge is the local arrangement of brightness, and that arrangement means the same thing in both places. A connected layer does not know this. It can learn an edge detector for the top left that fails in the top right because the training set happened not to contain an edge there. Sharing forces one detector to work everywhere, which is the correct constraint for most photographic evidence.
Every position is another example
The constraint also changes the learning problem. A weight used at one position only sees whatever appeared at that position, so 65,536 positions are 65,536 little problems, each with as many examples as there are images. A shared weight sees every window position in every image. Ten thousand images of 256 pixels a side give it patches, about six hundred and fifty million examples for the same nine numbers. The effective number of examples per weight is larger by the number of positions, which is why a convolutional network can generalise from a training set that would be hopeless for a connected layer of similar capability.
What sharing forbids
The constraint is real and it forbids things. Nothing in a shared filter can respond differently in the middle of a picture than at the edge, because no term in the arithmetic knows where the window is. A task that genuinely depends on absolute position is therefore invisible to a stack of shared filters, however deep. Asked to paint a dot at a given pair of coordinates, such a network fails in a way that looks absurd for a network that can recognise many kinds of object. The repair is not to abandon sharing. It is to hand the network extra input channels holding the row and column index of each position. Sharing stays, and the filters can read position as ordinary input data.
When sharing is wrong for some part of a problem, the fix is to supply as data whatever the constraint refuses to represent.
Worth keeping
- Sharing a filter across positions asserts that a pattern means the same thing wherever it appears, which is true of most photographic evidence.
- A dense layer needs a weight for every input pixel of every output unit, so a colour 256 by 256 image with as many outputs as inputs needs about forty billion weights.
- Sharing forbids absolute position, and when position matters the repair is to add position as input channels rather than to abandon sharing.
Where this goes next
This question is one step of How a Filter Reads a Picture, a course that works through its subject from the beginning. The page that teaches this part of it is Why the Same Weights Everywhere, and it reads the whole way through whether or not you have an account.
What an account changes is the practice: problems standing under every idea, marked with a reason rather than a tick, in a copy of the course that is yours.
Read that pageThe whole courseOther questions people ask about this
- What do you give up when you pool or downsample a feature map?You give up exact position: each block of the map becomes one number, so later layers see a coarser grid, gain reach, and lose the ability to say where a response came from.
- What does it mean to use stride 2 in a convolution?Using stride 2 keeps the filter the same but moves its window two positions at a time, so it produces about half as many outputs in each direction and about a quarter of the work in two dimensions.
- Why does a convolution make the image smaller if you don't pad it?A convolution shrinks the image because the window must fit completely inside it, so the first and last few positions have no full window. Padding adds extra values at the border so those positions can be used.
- What does a convolution actually calculate at a single point in the image?It multiplies each filter weight by the pixel beneath it, adds those products, and writes the single total at the matching position in the output.
- Why does the model get stuck repeating the same sentence over and over?The model is not stuck in a memory loop: once it has written a phrase, that phrase is in its context and makes writing it again more likely, so a decoder that always picks the top word cannot leave.
- Why does diffusion add Gaussian noise instead of any random corruption?Diffusion adds Gaussian noise because Gaussian errors combine exactly: each step keeps the spread fixed and gives a closed-form noisy picture at any later step, which makes the reverse step learnable.