TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
←

49. Conv2D Forward Pass

Medium

Implement the 2D convolution forward pass — the backbone of CNNs. A kernel (filter) slides across an input feature map, computing dot products at each position to produce an output feature map.

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

output[i, j, c_out] = sum over (ki, kj, c_in) of:
    input[i*stride + ki, j*stride + kj, c_in] * kernel[ki, kj, c_in, c_out]

With padding, the input is zero-padded by padding pixels on all sides.

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

  • x: (H, W, C_in) — single input image, channels-last
  • kernel: (kH, kW, C_in, C_out) — filter weights
  • stride: int (default 1)
  • padding: int (default 0) — zero-pad input on all sides
  • Returns: (H_out, W_out, C_out) where H_out = (H + 2*padding - kH) // stride + 1

Math

out[i,j,c]=kh​=0∑KH​−1​kw​=0∑KW​−1​cin​=0∑Cin​−1​x[i⋅s+kh​, j⋅s+kw​, cin​]⋅W[kh​,kw​,cin​,c]

Asked at

NumPy

import numpy as np

 

def conv2d_forward(...):

    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?