Practice / Convolutions and recurrences

Convolution backward: CNN backprop

Ten problems on the backward pass of a convolutional layer: the kernel gradient as a correlation of the input with dy, the input gradient as a full convolution with the flipped kernel, the im2col and Toeplitz matrix views, 2-D and multi-channel layers on a batch, stride, same padding and pooling, with worked solutions and the mistakes that forget the flip or the sum over positions.

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 00 on this page, as in array code. The 1-D layer has input x∈Rnx \in \mathbb{R}^n, kernel w∈Rkw \in \mathbb{R}^k with k≤nk \le n, a scalar bias bb, and output y∈Rmy \in \mathbb{R}^m with yi=∑a=0k−1wa xi+a+by_i = \sum_{a=0}^{k-1} w_a\,x_{i+a} + b for i=0,…,m−1i = 0, \dots, m-1.
  • 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, (u∗v)j=∑iui vj−i(u * v)_j = \sum_i u_i\,v_{j-i}, and one appears in Problem 3.
  • corr⁡(u,v)\operatorname{corr}(u, v) is the valid cross-correlation of a vector uu of length pp with a shorter vector vv of length qq: corr⁡(u,v)i=∑ava ui+a\operatorname{corr}(u, v)_i = \sum_a v_a\,u_{i+a} for i=0,…,p−qi = 0, \dots, p - q, so it has length p−q+1p - q + 1. The layer is y=corr⁡(x,w)+b1y = \operatorname{corr}(x, w) + b\mathbf{1}. For matrices, corr⁡(U,V)ij=∑a,cVac Ui+a,j+c\operatorname{corr}(U, V)_{ij} = \sum_{a,c} V_{ac}\,U_{i+a,j+c}, with both offsets running over VV's shape.
  • pad⁡p(u)\operatorname{pad}_p(u) is uu with pp zeros added at each end; for a matrix, pad⁡p1,p2(U)\operatorname{pad}_{p_1,p_2}(U) adds p1p_1 rows of zeros above and below and p2p_2 columns of zeros left and right. flip⁡(w)a=wk−1−a\operatorname{flip}(w)_a = w_{k-1-a} reverses a vector, and rot⁡(K)ac=Kk1−1−a, k2−1−c\operatorname{rot}(K)_{ac} = K_{k_1-1-a,\,k_2-1-c} turns a k1×k2k_1 \times k_2 matrix through 180∘180^\circ.
  • As on the minibatch page, dAdA is the code-style name for ∇AL\nabla_A L, the array of ∂L/∂A…\partial L/\partial A_{\dots} with the shape of AA, for a scalar loss LL computed from the layer's output by the layers above. The backward pass receives dydy and returns dwdw, dbdb and dxdx.
  • 1\mathbf{1} is the all-ones vector and ⌊t⌋\lfloor t \rfloor the largest integer at most tt.

Builds on: Jacobians and the chain rule, Batched backprop: dense layers on a minibatch

Problems

  1. ·

    Give the output length mm in terms of nn and kk. Then compute yy for x=(1,2,0,−1,3)x = (1, 2, 0, -1, 3), w=(2,−1,1)w = (2, -1, 1) and b=1b = 1.

  2. ··

    Compute dwdw and dbdb. Write dwdw as a correlation.

  3. ··

    Compute dxdx. Show that it is the full correlation of dydy with the flipped kernel, corr⁡(pad⁡k−1(dy),flip⁡(w))\operatorname{corr}(\operatorname{pad}_{k-1}(dy), \operatorname{flip}(w)).

  4. ··

    Write the layer as y=Pw+b1y = Pw + b\mathbf{1} with P∈Rm×kP \in \mathbb{R}^{m\times k}, Pia=xi+aP_{ia} = x_{i+a}, and as y=Tx+b1y = Tx + b\mathbf{1} with T∈Rm×nT \in \mathbb{R}^{m\times n}, Tij=wj−iT_{ij} = w_{j-i} when 0≤j−i≤k−10 \le j - i \le k - 1 and Tij=0T_{ij} = 0 otherwise. Use the two forms to compute dwdw and dxdx, and compare with Problems 2 and 3.

  5. ··

    By hand: x=(2,−1,3,0,1)x = (2, -1, 3, 0, 1), w=(1,2,−1)w = (1, 2, -1), and the layers above return dy=(1,0,−2)dy = (1, 0, -2). Compute dwdw, dbdb and dxdx.

  6. ···

    Two dimensions, one channel: X∈Rn1×n2X \in \mathbb{R}^{n_1\times n_2}, K∈Rk1×k2K \in \mathbb{R}^{k_1\times k_2}, a scalar bias bb, and

    Yij=∑a=0k1−1∑c=0k2−1Kac Xi+a, j+c+b.Y_{ij} = \sum_{a=0}^{k_1-1}\sum_{c=0}^{k_2-1} K_{ac}\,X_{i+a,\,j+c} + b .

    Give the shape of YY and compute dKdK, dbdb and dXdX.

  7. ···

    Channels and a batch: NN inputs X(n)∈RC×n1×n2X^{(n)} \in \mathbb{R}^{C\times n_1\times n_2} with CC channels, FF filters, and biases b∈RFb \in \mathbb{R}^F. Write Xc(n)X^{(n)}_c for channel cc of input nn and Kf,c∈Rk1×k2K_{f,c} \in \mathbb{R}^{k_1\times k_2} for the slice of filter ff that reads channel cc. Output channel ff of example nn is Yf(n)=∑c=1Ccorr⁡(Xc(n),Kf,c)+bfY^{(n)}_f = \sum_{c=1}^{C} \operatorname{corr}(X^{(n)}_c, K_{f,c}) + b_f. Compute dKf,cdK_{f,c}, dbfdb_f and dXc(n)dX^{(n)}_c.

  8. ···

    Stride ss: yi=∑awa xsi+a+by_i = \sum_a w_a\,x_{si+a} + b for i=0,…,m−1i = 0, \dots, m-1, with m=⌊(n−k)/s⌋+1m = \lfloor (n-k)/s \rfloor + 1. Compute dwdw and dxdx. For n=8n = 8, k=3k = 3, s=2s = 2, which entry of dxdx is zero whatever dydy is?

  9. ··

    Same padding: kk is odd, p=(k−1)/2p = (k-1)/2, and y=corr⁡(pad⁡p(x),w)+b1y = \operatorname{corr}(\operatorname{pad}_p(x), w) + b\mathbf{1}, so yy has length nn. Show that dx=corr⁡(pad⁡p(dy),flip⁡(w))dx = \operatorname{corr}(\operatorname{pad}_p(dy), \operatorname{flip}(w)), and give dwdw.

  10. ··

    Pooling with 2×22\times2 windows and stride 22. For

    X=[132042150−1322611]X = \begin{bmatrix} 1 & 3 & 2 & 0 \\ 4 & 2 & 1 & 5 \\ 0 & -1 & 3 & 2 \\ 2 & 6 & 1 & 1 \end{bmatrix}

    and

    dY=[1−2312],dY = \begin{bmatrix} 1 & -2 \\ 3 & \tfrac12 \end{bmatrix},

    compute dXdX for max pooling, Yij=max⁡a,c∈{0,1}X2i+a, 2j+cY_{ij} = \max_{a,c \in \{0,1\}} X_{2i+a,\,2j+c}, and for average pooling, Yij=14∑a,c∈{0,1}X2i+a, 2j+cY_{ij} = \tfrac14\sum_{a,c \in \{0,1\}} X_{2i+a,\,2j+c}.

Worked solutions

Problem 1

Give the output length mm in terms of nn and kk. Then compute yy for x=(1,2,0,−1,3)x = (1, 2, 0, -1, 3), w=(2,−1,1)w = (2, -1, 1) and b=1b = 1.

  1. Output ii reads xi,xi+1,…,xi+k−1x_i, x_{i+1}, \dots, x_{i+k-1}, so it needs i+k−1≤n−1i + k - 1 \le n - 1, that is i≤n−ki \le n - k.The window has kk entries starting at ii, and a valid correlation uses no padding, so the window must lie inside xx.
  2. ii runs over 0,1,…,n−k0, 1, \dots, n - k, which is n−k+1n - k + 1 values.Counting from 00 adds one to the largest index.
  3. y0=2⋅1+(−1)⋅2+1⋅0+1=1y_0 = 2\cdot1 + (-1)\cdot2 + 1\cdot0 + 1 = 1.Window (x0,x1,x2)=(1,2,0)(x_0, x_1, x_2) = (1, 2, 0), each entry multiplied by the kernel entry in the same position, plus bb.
  4. y1=2⋅2+(−1)⋅0+1⋅(−1)+1=4y_1 = 2\cdot2 + (-1)\cdot0 + 1\cdot(-1) + 1 = 4.The window moves one step: (x1,x2,x3)=(2,0,−1)(x_1, x_2, x_3) = (2, 0, -1).
  5. y2=2⋅0+(−1)⋅(−1)+1⋅3+1=5y_2 = 2\cdot0 + (-1)\cdot(-1) + 1\cdot3 + 1 = 5.Window (x2,x3,x4)=(0,−1,3)(x_2, x_3, x_4) = (0, -1, 3), the last one that fits since n−k=2n - k = 2.
  6. m=n−k+1m = n - k + 1; here m=3m = 3 and y=(1,4,5)y = (1, 4, 5)Each waw_a was used three times, once per window: this is the weight sharing whose consequence for the gradient is Problem 2.

Problem 2

Compute dwdw and dbdb. Write dwdw as a correlation.

  1. ∂yi/∂wa=xi+a\partial y_i/\partial w_a = x_{i+a} for every i=0,…,m−1i = 0, \dots, m-1.Only the term waxi+aw_a x_{i+a} of the sum contains waw_a, and every output has such a term because the same kernel is applied at every position.
  2. ∂L∂wa=∑i=0m−1∂L∂yi xi+a=∑idyi xi+a\dfrac{\partial L}{\partial w_a} = \sum_{i=0}^{m-1} \dfrac{\partial L}{\partial y_i}\,x_{i+a} = \sum_i dy_i\,x_{i+a}.LL depends on ww only through yy, and by step 1 every one of the mm outputs contains waw_a, so the chain rule has mm terms.
  3. ∑idyi xi+a=corr⁡(x,dy)a\sum_i dy_i\,x_{i+a} = \operatorname{corr}(x, dy)_a.The definition of corr⁡(u,v)a=∑ivi ua+i\operatorname{corr}(u, v)_a = \sum_i v_i\,u_{a+i} with u=xu = x and v=dyv = dy: dydy plays the kernel and slides over xx, giving n−m+1=kn - m + 1 = k outputs, one per kernel entry.
  4. ∂yi/∂b=1\partial y_i/\partial b = 1 for every ii, so ∂L/∂b=∑idyi\partial L/\partial b = \sum_i dy_i.bb is added to every output, so the chain rule sums dydy over all of them.
  5. dw=corr⁡(x,dy)dw = \operatorname{corr}(x, dy), that is dwa=∑idyi xi+adw_a = \sum_i dy_i\,x_{i+a}; db=1⊤dydb = \mathbf{1}^\top dyThe 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 dxdx. Show that it is the full correlation of dydy with the flipped kernel, corr⁡(pad⁡k−1(dy),flip⁡(w))\operatorname{corr}(\operatorname{pad}_{k-1}(dy), \operatorname{flip}(w)).

  1. xjx_j appears in yiy_i exactly when j=i+aj = i + a for some 0≤a≤k−10 \le a \le k - 1, and then its coefficient is wj−iw_{j-i}.Put a=j−ia = j - i in the term waxi+aw_a x_{i+a}: kernel entry j−ij - i is the one that lands on xjx_j when the window starts at ii.
  2. dxj=∑idyi wj−idx_j = \sum_i dy_i\,w_{j-i}, summing over the ii with 0≤j−i≤k−10 \le j - i \le k - 1.The chain rule sums over the outputs whose windows contain xjx_j. Inputs near the ends lie in fewer windows: x0x_0 lies only in window 00.
  3. Let d~=pad⁡k−1(dy)\tilde d = \operatorname{pad}_{k-1}(dy), so d~t=dyt−k+1\tilde d_t = dy_{t-k+1} for k−1≤t≤m+k−2k - 1 \le t \le m + k - 2 and d~t=0\tilde d_t = 0 otherwise.With k−1k - 1 zeros at each end, every term wj−iw_{j-i} in step 2 can be written without a range condition: indices that fall outside dydy hit a zero.
  4. corr⁡(d~,flip⁡(w))j=∑cwk−1−c d~j+c=∑cwk−1−c dyj+c−k+1\operatorname{corr}(\tilde d, \operatorname{flip}(w))_j = \sum_c w_{k-1-c}\,\tilde d_{j+c} = \sum_c w_{k-1-c}\,dy_{j+c-k+1}.The definition of corr⁡\operatorname{corr} with kernel flip⁡(w)\operatorname{flip}(w), then step 3; the result has (m+2k−2)−k+1=m+k−1=n(m + 2k - 2) - k + 1 = m + k - 1 = n entries, one per input.
  5. Put i=j+c−k+1i = j + c - k + 1; then k−1−c=j−ik - 1 - c = j - i and the sum becomes ∑idyi wj−i\sum_i dy_i\,w_{j-i}.A change of summation index; as cc runs over 0,…,k−10, \dots, k-1, j−ij - i runs over the same range, so the terms match step 2 one for one.
  6. dx=corr⁡(pad⁡k−1(dy),flip⁡(w))dx = \operatorname{corr}(\operatorname{pad}_{k-1}(dy), \operatorname{flip}(w)), that is dxj=∑idyi wj−idx_j = \sum_i dy_i\,w_{j-i}The second form is the true convolution dy∗wdy * w, which is why the flip appears: the forward pass reads the kernel forwards as the window moves right, so seen from a fixed input xjx_j the kernel index j−ij - i decreases as the window index ii increases. The forward pass is a correlation and the input gradient is a convolution.

Problem 4

Write the layer as y=Pw+b1y = Pw + b\mathbf{1} with P∈Rm×kP \in \mathbb{R}^{m\times k}, Pia=xi+aP_{ia} = x_{i+a}, and as y=Tx+b1y = Tx + b\mathbf{1} with T∈Rm×nT \in \mathbb{R}^{m\times n}, Tij=wj−iT_{ij} = w_{j-i} when 0≤j−i≤k−10 \le j - i \le k - 1 and Tij=0T_{ij} = 0 otherwise. Use the two forms to compute dwdw and dxdx, and compare with Problems 2 and 3.

  1. (Pw)i=∑axi+a wa=yi−b(Pw)_i = \sum_a x_{i+a}\,w_a = y_i - b.Row ii of PP is window ii of xx, so its product with ww is the sum in the definition.
  2. (Tx)i=∑jTij xj=∑a=0k−1wa xi+a=yi−b(Tx)_i = \sum_j T_{ij}\,x_j = \sum_{a=0}^{k-1} w_a\,x_{i+a} = y_i - b.Only the jj with 0≤j−i≤k−10 \le j - i \le k - 1 contribute; put a=j−ia = j - i.
  3. ∂y/∂w=P\partial y/\partial w = P and ∂y/∂x=T\partial y/\partial x = T.yy is affine in ww for fixed xx, and affine in xx for fixed ww; the Jacobian of Mv+cMv + c with respect to vv is MM.
  4. dw=P⊤dydw = P^\top dy and dx=T⊤dydx = T^\top dy.For a scalar LL, the gradient with respect to an input is the transposed Jacobian times the gradient at the output.
  5. (P⊤dy)a=∑ixi+a dyi(P^\top dy)_a = \sum_i x_{i+a}\,dy_i and (T⊤dy)j=∑iwj−i dyi(T^\top dy)_j = \sum_i w_{j-i}\,dy_i.(M⊤v)j=∑iMijvi(M^\top v)_j = \sum_i M_{ij}v_i; these are Problem 2, step 2 and Problem 3, step 2.
  6. dw=P⊤dydw = P^\top dy and dx=T⊤dydx = T^\top dy, the same vectors as Problems 2 and 3PP 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. TT is the layer as a dense m×nm \times n matrix: banded, with the kernel copied along each row and zeros elsewhere. Its transpose sends each dyidy_i back to window ii, which is Problem 3's flipped kernel; TT itself is never formed.

Problem 5

By hand: x=(2,−1,3,0,1)x = (2, -1, 3, 0, 1), w=(1,2,−1)w = (1, 2, -1), and the layers above return dy=(1,0,−2)dy = (1, 0, -2). Compute dwdw, dbdb and dxdx.

  1. dw0=1⋅2+0⋅(−1)+(−2)⋅3=−4dw_0 = 1\cdot2 + 0\cdot(-1) + (-2)\cdot3 = -4.Problem 2: dwadw_a is dydy dotted with the window of xx starting at aa; for a=0a = 0 that is (x0,x1,x2)=(2,−1,3)(x_0, x_1, x_2) = (2, -1, 3).
  2. dw1=1⋅(−1)+0⋅3+(−2)⋅0=−1dw_1 = 1\cdot(-1) + 0\cdot3 + (-2)\cdot0 = -1 and dw2=1⋅3+0⋅0+(−2)⋅1=1dw_2 = 1\cdot3 + 0\cdot0 + (-2)\cdot1 = 1.The windows (x1,x2,x3)=(−1,3,0)(x_1, x_2, x_3) = (-1, 3, 0) and (x2,x3,x4)=(3,0,1)(x_2, x_3, x_4) = (3, 0, 1).
  3. db=1+0−2=−1db = 1 + 0 - 2 = -1.Problem 2: the sum of dydy.
  4. pad⁡2(dy)=(0,0,1,0,−2,0,0)\operatorname{pad}_2(dy) = (0, 0, 1, 0, -2, 0, 0) and flip⁡(w)=(−1,2,1)\operatorname{flip}(w) = (-1, 2, 1).Problem 3 with k−1=2k - 1 = 2 zeros at each end.
  5. dx0=0⋅(−1)+0⋅2+1⋅1=1dx_0 = 0\cdot(-1) + 0\cdot2 + 1\cdot1 = 1, dx1=0⋅(−1)+1⋅2+0⋅1=2dx_1 = 0\cdot(-1) + 1\cdot2 + 0\cdot1 = 2, dx2=1⋅(−1)+0⋅2+(−2)⋅1=−3dx_2 = 1\cdot(-1) + 0\cdot2 + (-2)\cdot1 = -3.Slide flip⁡(w)\operatorname{flip}(w) along the padded dydy, starting at positions 00, 11, 22.
  6. dx3=0⋅(−1)+(−2)⋅2+0⋅1=−4dx_3 = 0\cdot(-1) + (-2)\cdot2 + 0\cdot1 = -4 and dx4=(−2)⋅(−1)+0⋅2+0⋅1=2dx_4 = (-2)\cdot(-1) + 0\cdot2 + 0\cdot1 = 2.Positions 33 and 44, the last that fit in a length-77 vector. Spot check with dxj=∑idyi wj−idx_j = \sum_i dy_i\,w_{j-i}: x0x_0 is only in window 00, under w0=1w_0 = 1, so dx0=dy0=1dx_0 = dy_0 = 1.
  7. dw=(−4,−1,1)dw = (-4, -1, 1), db=−1db = -1, dx=(1,2,−3,−4,2)dx = (1, 2, -3, -4, 2)Five entries in dxdx and three in dwdw, the shapes of xx and ww.

Problem 6

Two dimensions, one channel: X∈Rn1×n2X \in \mathbb{R}^{n_1\times n_2}, K∈Rk1×k2K \in \mathbb{R}^{k_1\times k_2}, a scalar bias bb, and

Yij=∑a=0k1−1∑c=0k2−1Kac Xi+a, j+c+b.Y_{ij} = \sum_{a=0}^{k_1-1}\sum_{c=0}^{k_2-1} K_{ac}\,X_{i+a,\,j+c} + b .

Give the shape of YY and compute dKdK, dbdb and dXdX.

  1. YY is (n1−k1+1)×(n2−k2+1)(n_1 - k_1 + 1) \times (n_2 - k_2 + 1).Problem 1 in each direction separately: the window must fit vertically and horizontally.
  2. ∂L/∂Kac=∑i,jdYij Xi+a, j+c=corr⁡(X,dY)ac\partial L/\partial K_{ac} = \sum_{i,j} dY_{ij}\,X_{i+a,\,j+c} = \operatorname{corr}(X, dY)_{ac}.Problem 2 in two dimensions: KacK_{ac} is used at every output position (i,j)(i, j) with coefficient Xi+a,j+cX_{i+a,j+c}, and the chain rule sums over all of them.
  3. ∂L/∂b=∑i,jdYij\partial L/\partial b = \sum_{i,j} dY_{ij}.bb is added to every entry of YY.
  4. XpqX_{pq} appears in YijY_{ij} with coefficient Kp−i, q−jK_{p-i,\,q-j} whenever 0≤p−i≤k1−10 \le p - i \le k_1 - 1 and 0≤q−j≤k2−10 \le q - j \le k_2 - 1, so dXpq=∑i,jdYij Kp−i, q−jdX_{pq} = \sum_{i,j} dY_{ij}\,K_{p-i,\,q-j}.Problem 3, steps 1 and 2, with one offset per direction.
  5. ∑i,jdYij Kp−i, q−j=corr⁡(pad⁡k1−1,k2−1(dY),rot⁡(K))pq\sum_{i,j} dY_{ij}\,K_{p-i,\,q-j} = \operatorname{corr}(\operatorname{pad}_{k_1-1,k_2-1}(dY), \operatorname{rot}(K))_{pq}.Problem 3, steps 3 to 5, in each direction: flipping both axes of KK is turning it through 180∘180^\circ, and the padding gives n1×n2n_1 \times n_2 outputs.
  6. dK=corr⁡(X,dY)dK = \operatorname{corr}(X, dY); db=∑i,jdYijdb = \sum_{i,j} dY_{ij}; dX=corr⁡(pad⁡k1−1,k2−1(dY),rot⁡(K))dX = \operatorname{corr}(\operatorname{pad}_{k_1-1,k_2-1}(dY), \operatorname{rot}(K))The shapes are k1×k2k_1 \times k_2, a scalar and n1×n2n_1 \times n_2, those of KK, bb and XX. Nothing new happens in two dimensions: each axis behaves like the 1-D layer.

Problem 7

Channels and a batch: NN inputs X(n)∈RC×n1×n2X^{(n)} \in \mathbb{R}^{C\times n_1\times n_2} with CC channels, FF filters, and biases b∈RFb \in \mathbb{R}^F. Write Xc(n)X^{(n)}_c for channel cc of input nn and Kf,c∈Rk1×k2K_{f,c} \in \mathbb{R}^{k_1\times k_2} for the slice of filter ff that reads channel cc. Output channel ff of example nn is Yf(n)=∑c=1Ccorr⁡(Xc(n),Kf,c)+bfY^{(n)}_f = \sum_{c=1}^{C} \operatorname{corr}(X^{(n)}_c, K_{f,c}) + b_f. Compute dKf,cdK_{f,c}, dbfdb_f and dXc(n)dX^{(n)}_c.

  1. Kf,cK_{f,c} appears only in output channel ff, through the term corr⁡(Xc(n),Kf,c)\operatorname{corr}(X^{(n)}_c, K_{f,c}), and it appears there for every example nn.Each filter makes one output channel, its slice cc reads only input channel cc, and the filters are shared across the batch.
  2. dKf,c=∑ncorr⁡(Xc(n),dYf(n))dK_{f,c} = \sum_n \operatorname{corr}(X^{(n)}_c, dY^{(n)}_f).Problem 6, step 2, for each example, summed over examples because the parameter is shared, as on the minibatch page.
  3. dbf=∑n∑i,jdYf,ij(n)db_f = \sum_n \sum_{i,j} dY^{(n)}_{f,ij}.bfb_f is added to every position of output channel ff in every example.
  4. Xc(n)X^{(n)}_c appears in every output channel ff of example nn, through corr⁡(Xc(n),Kf,c)\operatorname{corr}(X^{(n)}_c, K_{f,c}), and in no other example.Every filter reads every input channel; examples do not interact.
  5. dXc(n)=∑fcorr⁡(pad⁡k1−1,k2−1(dYf(n)),rot⁡(Kf,c))dX^{(n)}_c = \sum_f \operatorname{corr}(\operatorname{pad}_{k_1-1,k_2-1}(dY^{(n)}_f), \operatorname{rot}(K_{f,c})).Problem 6, step 5, for each output channel the input reaches, added because the chain rule sums over every path.
  6. dKf,c=∑ncorr⁡(Xc(n),dYf(n))dK_{f,c} = \sum_n \operatorname{corr}(X^{(n)}_c, dY^{(n)}_f); dbf=∑n∑i,jdYf,ij(n)db_f = \sum_n\sum_{i,j} dY^{(n)}_{f,ij}; dXc(n)=∑fcorr⁡(pad⁡k1−1,k2−1(dYf(n)),rot⁡(Kf,c))dX^{(n)}_c = \sum_f \operatorname{corr}(\operatorname{pad}_{k_1-1,k_2-1}(dY^{(n)}_f), \operatorname{rot}(K_{f,c}))The three sums are over the three kinds of sharing: the kernel gradient sums over positions (inside corr⁡\operatorname{corr}) 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 Kf,cK_{f,c} reads one channel only.

Problem 8

Stride ss: yi=∑awa xsi+a+by_i = \sum_a w_a\,x_{si+a} + b for i=0,…,m−1i = 0, \dots, m-1, with m=⌊(n−k)/s⌋+1m = \lfloor (n-k)/s \rfloor + 1. Compute dwdw and dxdx. For n=8n = 8, k=3k = 3, s=2s = 2, which entry of dxdx is zero whatever dydy is?

  1. dwa=∑idyi xsi+adw_a = \sum_i dy_i\,x_{si+a}.Problem 2 with window ii starting at sisi instead of ii: waw_a is still used once per output, now with coefficient xsi+ax_{si+a}.
  2. xjx_j appears in yiy_i with coefficient wj−siw_{j-si} when 0≤j−si≤k−10 \le j - si \le k - 1, so dxj=∑idyi wj−sidx_j = \sum_i dy_i\,w_{j-si} over those ii.Problem 3, steps 1 and 2, with window ii starting at sisi.
  3. Let uu be dydy with s−1s - 1 zeros inserted between neighbouring entries: usi=dyiu_{si} = dy_i, other entries 00, length s(m−1)+1s(m-1) + 1. Then dxj=∑tut wj−tdx_j = \sum_t u_t\,w_{j-t}.The nonzero entries of uu sit at t=sit = si, so the sum over tt is the sum over ii in step 2; the zeros contribute nothing.
  4. That is Problem 3's formula with uu in place of dydy, which gives the first s(m−1)+ks(m-1) + k entries of dxdx; inputs xjx_j with j≥s(m−1)+kj \ge s(m-1) + k lie in no window, and their gradient is 00.The last window starts at s(m−1)s(m-1) and ends at s(m−1)+k−1s(m-1) + k - 1; when ss does not divide n−kn - k, the inputs after it are never read.
  5. For n=8n = 8, k=3k = 3, s=2s = 2: m=⌊5/2⌋+1=3m = \lfloor 5/2 \rfloor + 1 = 3, the last window covers x4,x5,x6x_4, x_5, x_6, and x7x_7 is never read.Step 4 with s(m−1)+k=7s(m-1) + k = 7.
  6. dwa=∑idyi xsi+adw_a = \sum_i dy_i\,x_{si+a}; dxj=∑idyi wj−sidx_j = \sum_i dy_i\,w_{j-si} (over 0≤j−si≤k−10 \le j - si \le k-1), which is Problem 3 applied to dydy with s−1s - 1 zeros inserted between its entries; for n=8n = 8, k=3k = 3, s=2s = 2, dx7=0dx_7 = 0This is why the backward pass of a strided convolution is called a transposed convolution: it is T⊤dyT^\top dy for the strided TT of Problem 4, and the inserted zeros are how a stride-11 routine computes it.

Problem 9

Same padding: kk is odd, p=(k−1)/2p = (k-1)/2, and y=corr⁡(pad⁡p(x),w)+b1y = \operatorname{corr}(\operatorname{pad}_p(x), w) + b\mathbf{1}, so yy has length nn. Show that dx=corr⁡(pad⁡p(dy),flip⁡(w))dx = \operatorname{corr}(\operatorname{pad}_p(dy), \operatorname{flip}(w)), and give dwdw.

  1. Let x~=pad⁡p(x)\tilde x = \operatorname{pad}_p(x), of length n+2p=n+k−1n + 2p = n + k - 1; then yy has (n+k−1)−k+1=n(n + k - 1) - k + 1 = n entries.Problem 1 applied to x~\tilde x.
  2. dx~=corr⁡(pad⁡k−1(dy),flip⁡(w))d\tilde x = \operatorname{corr}(\operatorname{pad}_{k-1}(dy), \operatorname{flip}(w)) and dw=corr⁡(x~,dy)dw = \operatorname{corr}(\tilde x, dy).Problems 3 and 2 hold for any input vector, padded or not.
  3. xj=x~j+px_j = \tilde x_{j+p}, and the padding entries are constants, so dxj=dx~j+pdx_j = d\tilde x_{j+p}.xx reaches LL only through x~\tilde x, entry for entry; the gradients that land on the zeros are discarded because nothing upstream produced them.
  4. dx~j+p=∑cwk−1−c dyj+p+c−(k−1)=∑cflip⁡(w)c dyj+c−pd\tilde x_{j+p} = \sum_c w_{k-1-c}\,dy_{j+p+c-(k-1)} = \sum_c \operatorname{flip}(w)_c\,dy_{j+c-p}.Problem 3, step 4, at index j+pj + p, with k−1=2pk - 1 = 2p.
  5. ∑cflip⁡(w)c dyj+c−p=corr⁡(pad⁡p(dy),flip⁡(w))j\sum_c \operatorname{flip}(w)_c\,dy_{j+c-p} = \operatorname{corr}(\operatorname{pad}_p(dy), \operatorname{flip}(w))_j.Entry j+cj + c of pad⁡p(dy)\operatorname{pad}_p(dy) is dyj+c−pdy_{j+c-p}, or 00 when that index falls outside dydy.
  6. dx=corr⁡(pad⁡p(dy),flip⁡(w))dx = \operatorname{corr}(\operatorname{pad}_p(dy), \operatorname{flip}(w)) and dw=corr⁡(pad⁡p(x),dy)dw = \operatorname{corr}(\operatorname{pad}_p(x), dy)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 rot⁡(K)\operatorname{rot}(K).

Problem 10

Pooling with 2×22\times2 windows and stride 22. For

X=[132042150−1322611]X = \begin{bmatrix} 1 & 3 & 2 & 0 \\ 4 & 2 & 1 & 5 \\ 0 & -1 & 3 & 2 \\ 2 & 6 & 1 & 1 \end{bmatrix}

and

dY=[1−2312],dY = \begin{bmatrix} 1 & -2 \\ 3 & \tfrac12 \end{bmatrix},

compute dXdX for max pooling, Yij=max⁡a,c∈{0,1}X2i+a, 2j+cY_{ij} = \max_{a,c \in \{0,1\}} X_{2i+a,\,2j+c}, and for average pooling, Yij=14∑a,c∈{0,1}X2i+a, 2j+cY_{ij} = \tfrac14\sum_{a,c \in \{0,1\}} X_{2i+a,\,2j+c}.

  1. Where a window's largest entry is unique, YijY_{ij} equals that entry for every small enough change of XX, so ∂Yij/∂Xpq\partial Y_{ij}/\partial X_{pq} is 11 at the largest entry and 00 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.
  2. The largest entries are 44 at (1,0)(1, 0), 55 at (1,3)(1, 3), 66 at (3,1)(3, 1) and 33 at (2,2)(2, 2).The windows are rows 00–11 and 22–33 crossed with columns 00–11 and 22–33: {1,3,4,2}\{1, 3, 4, 2\}, {2,0,1,5}\{2, 0, 1, 5\}, {0,−1,2,6}\{0, -1, 2, 6\}, {3,2,1,1}\{3, 2, 1, 1\}. The two 11s in the last window are not its largest entry, so they cause no tie.
  3. Max pooling: dX1,0=1dX_{1,0} = 1, dX1,3=−2dX_{1,3} = -2, dX3,1=3dX_{3,1} = 3, dX2,2=12dX_{2,2} = \tfrac12, every other entry 00.The windows do not overlap, so each XpqX_{pq} lies in one window only and receives that window's dYijdY_{ij} if it is the largest entry, 00 otherwise.
  4. Average pooling: ∂Yij/∂Xpq=14\partial Y_{ij}/\partial X_{pq} = \tfrac14 for all four entries of window (i,j)(i, j).The mean is linear with equal weights.
  5. Max pooling: dX=[0000100−2001200300]dX = \begin{bmatrix} 0 & 0 & 0 & 0 \\ 1 & 0 & 0 & -2 \\ 0 & 0 & \tfrac12 & 0 \\ 0 & 3 & 0 & 0 \end{bmatrix}; average pooling: every entry of window (i,j)(i, j) gets dYij/4dY_{ij}/4, that is 14\tfrac14, −12-\tfrac12, 34\tfrac34 and 18\tfrac18 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 ww, and it is natural to expect the backward pass to be one too.

  1. dxj=∑idyi wj−idx_j = \sum_i dy_i\,w_{j-i}, and for Problem 5 pad⁡2(dy)=(0,0,1,0,−2,0,0)\operatorname{pad}_2(dy) = (0, 0, 1, 0, -2, 0, 0)Right so far: Problem 3, step 2, and Problem 5, step 4.
  2. “Backward is the forward operation run on the gradient: correlate the padded dydy with ww.”The analogy that causes the mistake: the forward pass reads the kernel forwards, so the backward pass is assumed to read it forwards too.
  3. dx=corr⁡(pad⁡2(dy),w)=(−1,2,3,−4,−2)dx = \operatorname{corr}(\operatorname{pad}_2(dy), w) = (-1, 2, 3, -4, -2)The correct answer is (1,2,−3,−4,2)(1, 2, -3, -4, 2) (Problem 5). From xjx_j's point of view the kernel index j−ij - i runs backwards as the window index ii 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 mm copies, and averaging the copies' gradients sounds like the fair way to combine them.

  1. ∂L/∂wa=∑i∂L∂yi ∂yi∂wa\partial L/\partial w_a = \sum_i \dfrac{\partial L}{\partial y_i}\,\dfrac{\partial y_i}{\partial w_a}, with one term per output positionRight so far: Problem 2, step 2.
  2. “The kernel is shared by mm positions, so its gradient is the average of the mm per-position gradients.”The shortcut that causes the mistake: confusing the 1/N1/N of a mean loss, which is part of the loss, with the way the chain rule combines the uses of a shared parameter.
  3. dwa=1m∑idyi xi+adw_a = \tfrac1m\sum_i dy_i\,x_{i+a}The chain rule adds the contributions of every use; it never divides by their number. If the loss is a mean, its 1/N1/N is already inside dydy. With the extra 1m\tfrac1m the kernel learns mm 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.

  1. dxj=∑idyi wj−idx_j = \sum_i dy_i\,w_{j-i} for j=0,…,n−1j = 0, \dots, n-1Right so far: Problem 3, step 2. There is one entry per input.
  2. “Correlate dydy 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 dydy is shorter than xx and the result has length kk; here the kernel is ww and the input it slides over is dydy itself, which is shorter than the answer.
  3. dx=corr⁡(dy,flip⁡(w))dx = \operatorname{corr}(dy, \operatorname{flip}(w))That has length m−k+1=n−2k+2m - k + 1 = n - 2k + 2, not nn: for Problem 5 it is the single number −3-3, which is dx2dx_2 alone. The inputs near the ends lie in fewer windows but still get gradients, and the k−1k - 1 zeros at each end of dydy are what supply the missing windows.

4. Strided input gradient without the inserted zeros

The stride-11 input gradient is a correlation with the flipped kernel, and with a stride it is tempting to reuse it unchanged.

  1. dxj=∑idyi wj−sidx_j = \sum_i dy_i\,w_{j-si}Right so far: Problem 8, step 2.
  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 wj−siw_{j-si}.
  3. dx=corr⁡(pad⁡k−1(dy),flip⁡(w))dx = \operatorname{corr}(\operatorname{pad}_{k-1}(dy), \operatorname{flip}(w))That is dxj=∑idyi wj−idx_j = \sum_i dy_i\,w_{j-i}, which sends dyidy_i back to window ii instead of window sisi, and it has length m+k−1m + k - 1: for n=8n = 8, k=3k = 3, s=2s = 2 that is 55 entries for 88 inputs. Insert s−1s - 1 zeros between the entries of dydy 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.

  1. YijY_{ij} is the largest entry of window (i,j)(i, j), and for Problem 10 the top-left window is {1,3,4,2}\{1, 3, 4, 2\} with dY00=1dY_{00} = 1Right so far: Problem 10, step 2.
  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.
  3. dX00=dX01=dX10=dX11=14dX_{00} = dX_{01} = dX_{10} = dX_{11} = \tfrac14A small change to X00=1X_{00} = 1, X01=3X_{01} = 3 or X11=2X_{11} = 2 does not change the maximum 44, so their derivatives are 00; only X10X_{10} gets the gradient, all of it: dX10=1dX_{10} = 1 (Problem 10, step 3).

Print this set: convolution-backward.pdf (problems, answers, and worked solutions on separate pages).