Question about padding size in Conv2D

Hello,
In Convolution2D layer, when the filer size is 4 and dilation is 1 and stride is 1, if the output dimensions are the same as the input dimensions, what the padding size should be?

You may refer to the docs for more info

H_{out} = floor((H_{in} + 2 * padding[0] - dilation[0] * (kernel_size[0] - 1) - 1) / stride[0] + 1)

Hi, I know the documentation, but the documentation doesn’t answer my question.
My understanding is when kernel_size=4, dilation and stride are both 1, there is no integer padding that satisfies output dimension is the same as input dimension.
Unless dilation and padding are both zero, can the convolution layer work with zero dilation?

you’re right. there is no way to make the output dimension the same as the output

Is there a way that I can make the output dimension the same as the input dimension, within above parameters, just like padding mode “same” in Keras?

you may try nn.ReflectionPad2d before nn.Conv2d

nn.Sequential(
        nn.ReflectionPad2d((1, 2, 1, 2)),
       nn.Conv2d(.....padding=0...)
)
1 Like

Thanks to your help.