Back to Practice
#0024
CNN Output Volume Shape
MediumML18 min15 XP
Problem
Given input height, width, input channels, filter count, kernel size, padding, and stride, return (output_height, output_width, output_channels).
Why This Matters
Model summaries are easier to debug when you can predict tensor shapes before running code.
Function Signature
def conv_output_volume(h, w, in_channels, filters, kernel, padding, stride):
Examples
Example 1
Input32x32x3 image, 16 filters, 3x3 kernel, padding 1, stride 1
Output(32, 32, 16)
Spatial size stays the same, but output channels equal the number of filters.
Constraints
- Use the same kernel, padding, and stride for height and width.
- Output channel count equals number of filters.
- Input channels affect parameter count, not output channel count.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 4 hidden categories
Same spatial size, more channels
Input32, 32, 3, 16, 3, 1, 1
Expected(32, 32, 16)
Sixteen filters produce sixteen output feature maps.
Stride shrinks spatial dimensions
Input28, 28, 1, 8, 5, 0, 2
Expected(12, 12, 8)
Height and width shrink, channel count becomes filter count.
Hidden Test Categories
Rectangular inputKernel size 1Padding 0Many filters