0
$\begingroup$

Im confused about what PyTorchs padding parameter does when using torch.nn.ConvTranspose2d. The docs say that:

"The padding argument effectively adds dilation * (kernel_size - 1) - padding amount of zero padding to both sizes of the input".

So my guess was that the dimensions of the feature maps increase when applying padding. However running test they decrease:

inp = torch.ones((1, 1, 2, 2))
conv_no_pad = nn.ConvTranspose2d(1, 1, kernel_size=(3, 3), stride=2, padding=0)
conv_pad = nn.ConvTranspose2d(1, 1, kernel_size=(3, 3), stride=2, padding=1)
print(conv_no_pad(inp).shape)
# => (1, 1, 5, 5)
print(conv_pad(inp).shape)
# => (1, 1, 3, 3)

Can somebody explain how the padding works?

$\endgroup$
1
$\begingroup$

As you quoted The padding argument effectively adds dilation * (kernel_size - 1) - padding, so you see the padding value is subtracted, the resulting shape becomes lower. It's a reverse (in some sense) operation to Conv2d, which means the arguments work the opposite way here. And I think this behavior is introduced to make it easier to design neural nets with symmetric architecture (like autoencoders) -- you just copy the parameters of kernel size, stride and padding from the corresponding Conv2d layer and get an operation which preserves the input shape of an image.

$\endgroup$
1
  • $\begingroup$ Yep, I came across transposed convolutions when trying to create an autoencoder. However you have to add the output_padding=1 parameter in pytorch, so the transposed convolution works the opposite way of a normal convolution. I think I now understood it, it's a little bit confusing. If dilation = 1 and kernel_size=3 and padding = 0; 1 * (3 - 1) - 0 = 2 padding is added to the input. If padding = 1; 1 * (3 - 1) - 1) = 1 padding is added to the input. $\endgroup$ – Tim von Känel Sep 11 '20 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.