Padding setting of torch.nn.ConvTranspose2d

I am a little confused about the padding setting of the torch.nn.ConvTranspose2d.

As I see the document, it seems that the padding of the deconvolution is calculated by some settings of convolution. padding(deconv) = K(conv) - 1 - padding(conv)

An Example:
If I want to upsample x3 using the deconvolution, the setting of the convolution is kernel = 3, stride = 2, padding = 0; According to the previous equation, the padding of the deconvolution should be 2;
And by using the this equation Hout=(Hin−1)∗stride[0]−2∗padding[0]+kernel_size[0]+output_padding[0]
, we can get the setting of deconvolution, kernel = 7, stride = 3, padding = 2;

I don’t know if my understanding is right. Actually, I don’t know if the first equation is required. If not, the setting of kernel = 5, stride = 3, padding = 1; also meets the second equation.

Any help would be much appreciated.
It would be better if you could provide some related material.

1 Like

You are right. There are several setting for your ConvTranpose layer to achieve the same result.
The same goes for the opposite direction: you can get the same output shape using different settings for a Conv layer.

This tutorial and also these visualizations might make things clearer.


This is what I want.
Thanks so much !!!

If this is of any help, I wrote a post on ConvTranspose1d which is extendable to 2d simply:

If anyone in the community sees a misunderstanding I’ll be greateful about pointers :smiley:

1 Like