Back to Practice
#0019

Convolution Output Size

MediumML18 min15 XP

Problem

Given input_size, kernel_size, padding, and stride, return the output size for one spatial dimension using floor((input + 2*padding - kernel) / stride) + 1.

Why This Matters

CNN debugging often starts with shape debugging. If you can predict output size, model architecture becomes much easier to reason about.

Function Signature

def conv_output_size(input_size, kernel_size, padding, stride):

Examples

Example 1
Inputinput 32, kernel 3, padding 1, stride 1
Output32

Padding 1 keeps a 3x3 convolution from shrinking the image when stride is 1.

Example 2
Inputinput 28, kernel 5, padding 0, stride 2
Output12

The kernel moves in steps of 2, so the output is smaller.

Constraints

  • All inputs are positive integers except padding, which may be zero.
  • Use integer floor division.
  • Return the output size for one dimension only.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 4 hidden categories
Same-size 3x3 convolution
Input32, 3, 1, 1
Expected32

This is the common same-padding case for odd kernels.

Stride shrinks output
Input28, 5, 0, 2
Expected12

Floor division handles partial final placements.

Hidden Test Categories
Kernel size 1Large paddingStride 3Input barely larger than kernel