1
votes

Thank you for you attention. I hopes to use nn.ConvTranspose2d to expand dimension of a tensor in PyTorch.(from (N,C,4,4) to (N,C,8,8)) However, I find that if I want to keep the kernel size to 3 and stride to 2. I need to set the padding to [[0,0],[0,0],[0,1],[0,1]](only one side to H and W), which is not a square.

I learnt that in tensorflow we can set the padding like [[0,0],[0,0],[0,1],[0,1]], but in torch, the padding can only be a value for each dimension, which means the padding is on both sides.

So, I am wondering is there any way to do this in PyTorch?

Here's some code of details

import torch
import torch.nn as nn

a = torch.rand([100, 80, 4, 4])
a.shape
nn.ConvTranspose2d(80, 40, kernel_size=3, stride=2, padding=0)(a).shape
nn.ConvTranspose2d(80, 40, kernel_size=3, stride=2, padding=1)(a).shape
nn.ConvTranspose2d(80, 40, kernel_size=3, stride=2, padding=(0,1,0,1))(a).shape
>>> torch.Size([100, 80, 4, 4])
>>> torch.Size([100, 40, 9, 9])
>>> torch.Size([100, 40, 7, 7])
>>> RuntimeError: expected padding to be a single integer value or a list of 2 values to match the convolution dimensions, but got padding=[0, 1, 0, 1]
1

1 Answers

1
votes

You can use F.pad from functional API.

b= nn.ConvTranspose2d(80, 40, kernel_size=3, stride=2, padding=1)(a)
c = F.pad(b, (0,1,0,1),"constant", 0)