A CNN by hand · 3. The image's gradient

A worked example, one operation at a time

Pixel X₂₃ sits in four windows, under K₂₂, K₂₁, K₁₂ and K₁₁. Laid out by window position those weights are K rotated 180°, so the image's gradient is the padded Δ convolved with the rotated kernel.

Open video ↗
Read the walkthrough

Four windows. ∂L/∂X₂₃ = Δ₁₂K₂₂ + Δ₁₃K₂₁ + Δ₂₂K₁₂ + Δ₂₃K₁₁ = 2·2 + 2·(−1) + 0·1 + 0·1 = 2.

The half turn. In general ∂L/∂Xpq = Σi,j ΔijKp−i+1, q−j+1: as the window index goes up, the kernel index goes down, along both axes. Reading K backwards along both axes is rotating it by 180°: rot₁₈₀(K) = [[2, −1], [1, 1]].

The recipe. Pad Δ with one ring of zeros, because edge pixels sit in fewer windows, then slide rot₁₈₀(K) over it: ∂L/∂X = pad(Δ) ⋆ rot₁₈₀(K), the full convolution of Δ with K. The forward pass is a correlation; its backward pass for the image is a true convolution.