Practice / Convolutions and recurrences
Convolution backward: CNN backprop
Before you start
A convolutional layer is a dense layer with most of its weights forced to zero and the rest tied together: the same small kernel is applied at every position. Both constraints show up in the backward pass. Because the kernel is shared, its gradient is a sum over every position it was used at, which turns out to be another correlation. Because each output sees only a window of the input, the input gradient sends each output's gradient back to its own window, which turns out to be a convolution with the kernel flipped. These ten problems derive both in one dimension, check them by hand, and then carry them to two dimensions, several channels, a batch, stride, padding and pooling. The five mistakes at the end are the ones that produce an array of the right size or a plausible number: an unflipped kernel, a weight gradient averaged over positions, a valid correlation where a full one belongs, a stride ignored on the way back, and a max-pool gradient spread over the window.
- Indices start at on this page, as in array code. The 1-D layer has input , kernel with , a scalar bias , and output with for .
- Deep-learning libraries call this a convolution. Mathematically it is a cross-correlation: the kernel is read in the same direction as the input. A true convolution flips the kernel, , and one appears in Problem 3.
- is the valid cross-correlation of a vector of length with a shorter vector of length : for , so it has length . The layer is . For matrices, , with both offsets running over 's shape.
- is with zeros added at each end; for a matrix, adds rows of zeros above and below and columns of zeros left and right. reverses a vector, and turns a matrix through .
- As on the minibatch page, is the code-style name for , the array of with the shape of , for a scalar loss computed from the layer's output by the layers above. The backward pass receives and returns , and .
- is the all-ones vector and the largest integer at most .
Builds on: Jacobians and the chain rule, Batched backprop: dense layers on a minibatch
Problems
- ·
Give the output length in terms of and . Then compute for , and .
- ··
Compute and . Write as a correlation.
- ··
Compute . Show that it is the full correlation of with the flipped kernel, .
- ··
Write the layer as with , , and as with , when and otherwise. Use the two forms to compute and , and compare with Problems 2 and 3.
- ··
By hand: , , and the layers above return . Compute , and .
- ···
Two dimensions, one channel: , , a scalar bias , and
Give the shape of and compute , and .
- ···
Channels and a batch: inputs with channels, filters, and biases . Write for channel of input and for the slice of filter that reads channel . Output channel of example is . Compute , and .
- ···
Stride : for , with . Compute and . For , , , which entry of is zero whatever is?
- ··
Same padding: is odd, , and , so has length . Show that , and give .
- ··
Pooling with windows and stride . For
and
compute for max pooling, , and for average pooling, .
Answers
- ; here and
- , that is ;
- , that is
- and , the same vectors as Problems 2 and 3
- , ,
- ; ;
- ; ;
- ; (over ), which is Problem 3 applied to with zeros inserted between its entries; for , , ,
- and
- Max pooling: ; average pooling: every entry of window gets , that is , , and in the four windows
Worked solutions
Problem 1
Give the output length in terms of and . Then compute for , and .
- Output reads , so it needs , that is .The window has entries starting at , and a valid correlation uses no padding, so the window must lie inside .
- runs over , which is values.Counting from adds one to the largest index.
- .Window , each entry multiplied by the kernel entry in the same position, plus .
- .The window moves one step: .
- .Window , the last one that fits since .
- ; here and Each was used three times, once per window: this is the weight sharing whose consequence for the gradient is Problem 2.
Problem 2
Compute and . Write as a correlation.
- for every .Only the term of the sum contains , and every output has such a term because the same kernel is applied at every position.
- . depends on only through , and by step 1 every one of the outputs contains , so the chain rule has terms.
- .The definition of with and : plays the kernel and slides over , giving outputs, one per kernel entry.
- for every , so . is added to every output, so the chain rule sums over all of them.
- , that is ; The kernel gradient is a sum over positions for the same reason the minibatch page's bias gradient is a sum over examples: one parameter, many uses, and the chain rule adds the contributions.
Problem 3
Compute . Show that it is the full correlation of with the flipped kernel, .
- appears in exactly when for some , and then its coefficient is .Put in the term : kernel entry is the one that lands on when the window starts at .
- , summing over the with .The chain rule sums over the outputs whose windows contain . Inputs near the ends lie in fewer windows: lies only in window .
- Let , so for and otherwise.With zeros at each end, every term in step 2 can be written without a range condition: indices that fall outside hit a zero.
- .The definition of with kernel , then step 3; the result has entries, one per input.
- Put ; then and the sum becomes .A change of summation index; as runs over , runs over the same range, so the terms match step 2 one for one.
- , that is The second form is the true convolution , which is why the flip appears: the forward pass reads the kernel forwards as the window moves right, so seen from a fixed input the kernel index decreases as the window index increases. The forward pass is a correlation and the input gradient is a convolution.
Problem 4
Write the layer as with , , and as with , when and otherwise. Use the two forms to compute and , and compare with Problems 2 and 3.
- .Row of is window of , so its product with is the sum in the definition.
- .Only the with contribute; put .
- and . is affine in for fixed , and affine in for fixed ; the Jacobian of with respect to is .
- and .For a scalar , the gradient with respect to an input is the transposed Jacobian times the gradient at the output.
- and .; these are Problem 2, step 2 and Problem 3, step 2.
- and , the same vectors as Problems 2 and 3 is what libraries call the im2col matrix, one window per row, and building it turns the layer and its kernel gradient into ordinary matrix products. is the layer as a dense matrix: banded, with the kernel copied along each row and zeros elsewhere. Its transpose sends each back to window , which is Problem 3's flipped kernel; itself is never formed.
Problem 5
By hand: , , and the layers above return . Compute , and .
- .Problem 2: is dotted with the window of starting at ; for that is .
- and .The windows and .
- .Problem 2: the sum of .
- and .Problem 3 with zeros at each end.
- , , .Slide along the padded , starting at positions , , .
- and .Positions and , the last that fit in a length- vector. Spot check with : is only in window , under , so .
- , , Five entries in and three in , the shapes of and .
Problem 6
Two dimensions, one channel: , , a scalar bias , and
Give the shape of and compute , and .
- is .Problem 1 in each direction separately: the window must fit vertically and horizontally.
- .Problem 2 in two dimensions: is used at every output position with coefficient , and the chain rule sums over all of them.
- . is added to every entry of .
- appears in with coefficient whenever and , so .Problem 3, steps 1 and 2, with one offset per direction.
- .Problem 3, steps 3 to 5, in each direction: flipping both axes of is turning it through , and the padding gives outputs.
- ; ; The shapes are , a scalar and , those of , and . Nothing new happens in two dimensions: each axis behaves like the 1-D layer.
Problem 7
Channels and a batch: inputs with channels, filters, and biases . Write for channel of input and for the slice of filter that reads channel . Output channel of example is . Compute , and .
- appears only in output channel , through the term , and it appears there for every example .Each filter makes one output channel, its slice reads only input channel , and the filters are shared across the batch.
- .Problem 6, step 2, for each example, summed over examples because the parameter is shared, as on the minibatch page.
- . is added to every position of output channel in every example.
- appears in every output channel of example , through , and in no other example.Every filter reads every input channel; examples do not interact.
- .Problem 6, step 5, for each output channel the input reaches, added because the chain rule sums over every path.
- ; ; The three sums are over the three kinds of sharing: the kernel gradient sums over positions (inside ) and examples, the bias gradient over positions and examples, and the input gradient over the filters that read the channel. No gradient sums over input channels, because each slice reads one channel only.
Problem 8
Stride : for , with . Compute and . For , , , which entry of is zero whatever is?
- .Problem 2 with window starting at instead of : is still used once per output, now with coefficient .
- appears in with coefficient when , so over those .Problem 3, steps 1 and 2, with window starting at .
- Let be with zeros inserted between neighbouring entries: , other entries , length . Then .The nonzero entries of sit at , so the sum over is the sum over in step 2; the zeros contribute nothing.
- That is Problem 3's formula with in place of , which gives the first entries of ; inputs with lie in no window, and their gradient is .The last window starts at and ends at ; when does not divide , the inputs after it are never read.
- For , , : , the last window covers , and is never read.Step 4 with .
- ; (over ), which is Problem 3 applied to with zeros inserted between its entries; for , , , This is why the backward pass of a strided convolution is called a transposed convolution: it is for the strided of Problem 4, and the inserted zeros are how a stride- routine computes it.
Problem 9
Same padding: is odd, , and , so has length . Show that , and give .
- Let , of length ; then has entries.Problem 1 applied to .
- and .Problems 3 and 2 hold for any input vector, padded or not.
- , and the padding entries are constants, so . reaches only through , entry for entry; the gradients that land on the zeros are discarded because nothing upstream produced them.
- .Problem 3, step 4, at index , with .
- .Entry of is , or when that index falls outside .
- and The backward pass of a same convolution is a same convolution with the flipped kernel, which is why frameworks can reuse the forward routine for it. In two dimensions the flip becomes .
Problem 10
Pooling with windows and stride . For
and
compute for max pooling, , and for average pooling, .
- Where a window's largest entry is unique, equals that entry for every small enough change of , so is at the largest entry and at the other three.A small perturbation cannot change which entry is largest when the largest is strictly larger than the rest. With a tie the max is not differentiable, and libraries pick one of the tied entries.
- The largest entries are at , at , at and at .The windows are rows – and – crossed with columns – and –: , , , . The two s in the last window are not its largest entry, so they cause no tie.
- Max pooling: , , , , every other entry .The windows do not overlap, so each lies in one window only and receives that window's if it is the largest entry, otherwise.
- Average pooling: for all four entries of window .The mean is linear with equal weights.
- Max pooling: ; average pooling: every entry of window gets , that is , , and in the four windowsMax pooling routes the whole gradient to one entry per window, so the forward pass must save where the maximum was; average pooling spreads it evenly and needs to save nothing.
Where this goes wrong
1. Input gradient with the kernel unflipped
The forward pass is a correlation with , and it is natural to expect the backward pass to be one too.
- , and for Problem 5 Right so far: Problem 3, step 2, and Problem 5, step 4.
- “Backward is the forward operation run on the gradient: correlate the padded with .”The analogy that causes the mistake: the forward pass reads the kernel forwards, so the backward pass is assumed to read it forwards too.
- The correct answer is (Problem 5). From 's point of view the kernel index runs backwards as the window index runs forwards, so the kernel must be flipped. A symmetric kernel hides the error, and so does a shape check: the length is right.
2. Averaging the kernel gradient over positions
Weight sharing makes one kernel stand in for copies, and averaging the copies' gradients sounds like the fair way to combine them.
- , with one term per output positionRight so far: Problem 2, step 2.
- “The kernel is shared by positions, so its gradient is the average of the per-position gradients.”The shortcut that causes the mistake: confusing the of a mean loss, which is part of the loss, with the way the chain rule combines the uses of a shared parameter.
- The chain rule adds the contributions of every use; it never divides by their number. If the loss is a mean, its is already inside . With the extra the kernel learns times more slowly than the bias, and the factor changes with the image size.
3. Input gradient from a valid correlation
Problem 2 found the kernel gradient as a valid correlation, and the input gradient looks as if it should be built the same way.
- for Right so far: Problem 3, step 2. There is one entry per input.
- “Correlate with the flipped kernel; the kernel gradient needed no padding, so neither does this.”The analogy that causes the mistake: in Problem 2 the kernel is shorter than and the result has length ; here the kernel is and the input it slides over is itself, which is shorter than the answer.
- That has length , not : for Problem 5 it is the single number , which is alone. The inputs near the ends lie in fewer windows but still get gradients, and the zeros at each end of are what supply the missing windows.
4. Strided input gradient without the inserted zeros
The stride- input gradient is a correlation with the flipped kernel, and with a stride it is tempting to reuse it unchanged.
- Right so far: Problem 8, step 2.
- “The stride only changes where the windows start, which the forward pass handled; the backward pass is the same correlation as before.”The shortcut that causes the mistake: treating the stride as a detail of the forward loop, when it is in the coefficient .
- That is , which sends back to window instead of window , and it has length : for , , that is entries for inputs. Insert zeros between the entries of first (Problem 8).
5. Max-pool gradient spread over the window
Average pooling and max pooling both reduce a window to one number, and their gradients are easy to treat as the same.
- is the largest entry of window , and for Problem 10 the top-left window is with Right so far: Problem 10, step 2.
- “Every entry of the window took part in the pooling, so each gets an equal share of the gradient.”The analogy that causes the mistake: average pooling's rule, Problem 10, step 4, applied to a maximum.
- A small change to , or does not change the maximum , so their derivatives are ; only gets the gradient, all of it: (Problem 10, step 3).
Print this set: convolution-backward.pdf (problems, answers, and worked solutions on separate pages).