What Does Conv2d Output with 32×32 Input and Kernel 5?

Conv2d with 32×32 input, kernel_size=5, stride=1, padding=0 outputs 28×28. Formula: (32 - 5 + 0) / 1 + 1 = 28. Without padding, the kernel can't reach the edges.

Formula Breakdown

output_size = (input_size - kernel_size + 2 * padding) / stride + 1

Plugging in the values:

output = (32 - 5 + 2*0) / 1 + 1
output = (32 - 5 + 0) / 1 + 1
output = 27 / 1 + 1
output = 28

PyTorch Code

import torch
import torch.nn as nn

# Classic LeNet-style first conv on CIFAR-10
conv = nn.Conv2d(3, 6, kernel_size=5)  # default stride=1, padding=0
x = torch.randn(1, 3, 32, 32)
output = conv(x)
print(output.shape)  # torch.Size([1, 6, 28, 28])

Where This Appears

This configuration is the classic LeNet-5 first layer used with CIFAR-10 (32×32 images). Without padding, a 5×5 kernel loses 2 pixels on each side, shrinking 32 to 28. If you want to preserve the spatial size, add padding=2: output = (32 - 5 + 2×2) / 1 + 1 = 32.

Related Questions

Try the Conv2d Calculator