TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
←

50. Conv2D Backward Pass

Hard

Implement the backward pass of 2D convolution, computing gradients w.r.t. both the kernel (dkernel) and the input (dx), given the upstream gradient dout.

For each output position (i, j, c_out):

Kernel gradient (accumulate patch contribution):

dkernel[kh, kw, cin, cout] += dout[i, j, cout] * x[i*stride+kh, j*stride+kw, cin]

Input gradient (distribute gradient back through kernel):

dx[i*stride+kh, j*stride+kw, cin] += dout[i, j, cout] * kernel[kh, kw, cin, cout]

Signature: def conv2d_backward(dout, x, kernel, stride=1, padding=0)

  • dout: (H_out, W_out, C_out) — upstream gradient
  • x: (H, W, C_in) — original input (before padding)
  • kernel: (kH, kW, C_in, C_out) — filter weights
  • stride: int (default 1)
  • padding: int (default 0)
  • Returns: [dx, dkernel] — list of gradients w.r.t. input and kernel

The test harness compares the concatenated flattened output: np.concatenate([dx.flatten(), dkernel.flatten()]). Your function must return [dx, dkernel] as numpy arrays or lists.

Math

∂W[kh​,kw​,cin​,cout​]∂L​=i,j∑​δ[i,j,cout​]⋅x[i⋅s+kh​,j⋅s+kw​,cin​]

Asked at

NumPy

import numpy as np

 

def conv2d_backward(...):

    pass

🔒

Premium problem

Free accounts include problems #1–20. Upgrade to unlock the editor, hidden test cases, and reference solutions for every problem.

Upgrade to PremiumBack to problems

Already premium?