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-lastkernel: (kH, kW, C_in, C_out) — filter weightsstride: int (default 1)padding: int (default 0) — zero-pad input on all sides(H_out, W_out, C_out) where H_out = (H + 2*padding - kH) // stride + 1Math
Asked at
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.
Already premium?