#128Convolution — feature-map sizeEasy

Convolution — feature-map size

Background

Sliding a K×K filter over an H×W image (with padding P and stride S) shrinks the spatial size in a predictable way. Per dimension:

out=N+2PKS+1\text{out} = \left\lfloor \frac{N + 2P - K}{S} \right\rfloor + 1

For "valid" convolution (P=0P = 0, S=1S = 1) this is just NK+1N - K + 1.

Problem statement

Implement conv_output_size(N, K, stride, padding) returning the output size along one dimension.

Input

  • Nint, input size (height or width).
  • Kint, kernel size.
  • strideint 1\ge 1.
  • paddingint 0\ge 0.

Output

Returns an int: (N+2PK)/S+1\lfloor (N + 2P - K)/S \rfloor + 1.

Examples

Example 1 — valid conv

Input:  N = 5, K = 3, stride = 1, padding = 0
Output: 3

Explanation: 53+1=35 - 3 + 1 = 3.

Example 2 — same padding keeps the size

Input:  N = 5, K = 3, stride = 1, padding = 1
Output: 5

Explanation: (5+23)/1+1=5(5 + 2 - 3)/1 + 1 = 5.

Constraints

  • Use floor division for the stride term.
  • Return a plain int.

Notes

  • Stride 2 roughly halves the size; "same" padding (P=K/2P=\lfloor K/2\rfloor at stride 1) keeps it unchanged — both are everyday CNN design choices.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Example — valid conv N=5, K=3
  • Reference — same padding keeps size
  • Sample — stride 2 downsamples