Implement a Conv2D forward pass using PyTorch's built-in torch.nn.functional.conv2d.
Signature: def conv2d_forward_backward(x, weight, bias, stride=1, padding=0)
x: input tensor of shape (N, C_in, H, W)weight: filter tensor (C_out, C_in, kH, kW)bias: bias tensor (C_out,)stride: convolution stride (default 1)padding: zero-padding size (default 0)Use torch.nn.functional.conv2d for the forward pass. The output shape is (N, C_out, H_out, W_out) where H_out = (H + 2*padding - kH) // stride + 1.
Example:
x = [[[[1,1,1],[1,1,1],[1,1,1]]]] # (1,1,3,3)
w = [[[[1,1],[1,1]]]] # (1,1,2,2)
b = [0.0]
# Output shape: (1,1,2,2), each position sums 4 ones = 4.0
Math
Asked at
Test Results