A CNN by hand · 1. The setup
A worked example, one operation at a timeA 2 × 2 kernel K slides over a 5 × 5 image X to make a 4 × 4 map Z, one window at a time. A ReLU, a 2 × 2 max pool and one linear unit turn the map into a loss, and the loss sends Δ = ∂L/∂Z back to the map.
Open video ↗Read the walkthrough
One window. Lay K on the window whose top-left pixel is X₁₂. Each weight multiplies the pixel under it: Z₁₂ = K₁₁X₁₂ + K₁₂X₁₃ + K₂₁X₂₂ + K₂₂X₂₃ = 1·0 + 1·1 + (−1)·0 + 2·1 = 3.
Every window. Zij = Σa,b Xi+a−1, j+b−1Kab: i and j pick the window, a and b the weight. Deep learning calls this slide a convolution and writes Z = X ⋆ K. K is never flipped.
Back to the map. The ReLU zeroes the three negatives, the max pool keeps h = [3, 2, 6, 5], and y = hw = 3 + 2 − 6 + 5 = 4 against a target of 3 gives L = 1. Then ∂L/∂y = 2 and ∂L/∂h = 2w = [2, 2, −2, 2], and the pool sends each value back to its window's winner, so Δ has four nonzero entries: 2 at (1, 2) and (1, 3), −2 at (3, 1), 2 at (4, 3).