Convolutional Neural Networks

convolution operation

Imagine sliding a small stencil — a little grid of weights — across an image. At every stop you multiply the numbers under the stencil by the stencil's weights, add them into a single number, write that number down, slide one step, and repeat. The grid of numbers you produce is the result of the convolution operation. The stencil is small (say 3x3) but you reuse it everywhere, so it behaves like a pattern detector that asks the same question — 'is my pattern present here?' — at every location of the input.

Concretely, for a 2D input I and a kernel K of size k by k, the output at position (i, j) is O[i,j] = sum over m, n of I[i+m, j+n] times K[m,n], where the sum runs over the kernel's footprint. Each output value is therefore a local weighted sum — a dot product — between the kernel and the patch of input it currently covers. For a multi-channel input (for example the red, green, blue planes of a photo) the sum also runs over channels, so a single kernel is really a small 3D tensor and still produces one scalar per spatial position. This sliding dot product is the single most-repeated arithmetic operation inside a CNN, which is why hardware (GPUs, the systolic arrays in TPUs) is built to do it fast.

Two precise properties make convolution the right tool for images. It is linear, meaning the output is a weighted sum of inputs with no curving — which is exactly why a nonlinearity such as ReLU is applied afterward, otherwise stacking convolutions would collapse into one big linear map. And it is shift-equivariant: move the input pattern and the response moves with it, because the same kernel is applied at every position. Without nonlinearities and without depth, convolution alone is just a fixed local filter; the power comes from learning the weights and stacking many such layers.

A fixed 3x3 vertical-edge kernel [ [-1,0,1],[-1,0,1],[-1,0,1]] slid over a photo gives large positive responses where a dark-to-light vertical edge appears, near zero on flat regions, and large negative responses on light-to-dark edges.

Pitfall: what deep-learning frameworks call 'convolution' is mathematically cross-correlation. True signal-processing convolution first flips the kernel (O[i,j] = sum of I[i-m, j-n] K[m,n]). Because the weights are learned, the flip makes no practical difference — the network simply learns the flipped kernel — so libraries drop it. Do not be confused when a textbook's formula has minus signs that your code does not.

Also called
sliding-window dot productcross-correlation