← 返回 tesla 的题目列表NumPy Conv2D Forward and Parameter Count
类型:qbank
Implement a Conv2D forward pass in NumPy without using deep-learning framework kernels. The round can also ask for output-shape reasoning, total parameter count, bias handling, and vectorization.
Requirements
Given input tensor dimensions, input channels, output channels, kernel size, stride, and padding, compute the Conv2D output dimensions.
Compute total parameters. Include the bias term when bias is enabled.
Implement the forward convolution operation using NumPy, not TensorFlow / PyTorch built-ins.
Discuss or implement an optimization from naive nested loops to a more vectorized formulation.
One version of the round includes prepared unit tests, so the implementation must be runnable.
Notes
The baseline implementation can use loops over batch, output channel, output height, and output width, while each window multiplication is a NumPy operation.
Parameter count is out_channels * in_channels * kernel_h * kernel_w, plus out_channels if bias is present.
Output height / width are floor((input + 2 * padding - kernel) / stride) + 1 per spatial axis when the window fits exactly under the chosen convention.
For vectorization, discuss im2col, strided windows, or batching windows into a matrix multiply. A correct baseline plus a clear vectorization plan is better than a broken clever implementation.
Preparation
Implement conv2d_forward(x, w, b, stride, pad) in NumPy and compare its output against a tiny hand-calculated case with one batch, one channel, and one filter.
Practice deriving output shapes and parameter counts out loud before coding; include asymmetric height/width kernels and bias=False cases.
After the loop version passes, write an im2col or sliding_window_view version and explain the memory trade-off of materializing all patches.