QUESTION 4 OF 10

Why add padding before convolution?

Reveal the answer and explore the explanation.

04
MediumHigh priority

Why add padding before convolution?

Answer
Answer

Adds border values to control output dimensions and preserve edge information.

Practical example

Add zeros around an image before convolution.

PythonRelated concept example
import torch
from torch import nn
x = torch.randn(1, 3, 32, 32)
conv = nn.Conv2d(3, 8, kernel_size=3, padding=1)
print(conv(x).shape)

Padding one with a 3×3 kernel and stride one preserves height and width.

Reference: Further reading ↗

Last 30 min revisionHigh-yield interview Q&As