Computing Output Dimensions
What you'll learn: How to calculate the exact height and width of a convolutional layer's output based on its hyperparameters.
Why This Matters
When building convolutional neural networks, you need to know the spatial dimensions of your data as it flows through each layer. Getting this wrong leads to shape mismatches and broken models. The output dimension formula is your architectural blueprint.
The Formula
Given an input of size H × W (height × width), the output dimensions after convolution are:
Output Height = ⌊(H + 2P - K) / S⌋ + 1
Output Width = ⌊(W + 2P - K) / S⌋ + 1
Where:
- H, W: Input height and width
- P: Padding size (pixels added to borders)
- K: Kernel size (height/width of the filter)
- S: Stride (step size of the sliding window)
- ⌊ ⌋: Floor function (round down to nearest integer)
Understanding Each Component
Padding (P) expands your input, so H + 2P accounts for padding on both top/bottom (or left/right).
Subtracting kernel size (K) accounts for the fact that a kernel of size K can't start its slide in the last K-1 positions.
Dividing by stride (S) determines how many steps the sliding window takes.
Adding 1 counts the initial position where the kernel starts.
Example Calculation
Input: 32×32, Kernel: 5×5, Stride: 1, Padding: 2
Output = ⌊(32 + 2(2) - 5) / 1⌋ + 1
= ⌊(32 + 4 - 5) / 1⌋ + 1
= ⌊31 / 1⌋ + 1
= 31 + 1
= 32×32
The padding of 2 perfectly preserves the spatial dimensions!
Key Takeaway: The output dimension formula ⌊(H + 2P - K) / S⌋ + 1 lets you predict spatial sizes throughout your network, essential for designing architectures that connect properly layer-to-layer.