Same output size of Conv2d

I’m working with dilated temporal 2d convolutions and in order for the output to have the same shape as the input, I need to add left padding (same as the dilatation). I looked at nn.Conv2d and it only accepts symmetric padding, which is not idea in my case. Also I’m trying to avoid the manual padding, since the code became hard to read. Are there any other solutions that I am not aware of?

Hi @razvanc92,

I dont know a better way than manual padding. This wrapper should leave the code readable:

class LeftPaddedConv2D(nn.Module):
    def __init__(self, in_channels, out_channels, kernel_size, stride, left_padding, dilation, bias):
        super().__init__()
        self.pad = nn.ZeroPad2d((left_padding, 0, 0, 0))
        self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride, 0, dilation, bias=bias)
    def forward(x):
        x = self.pad(x)
        return self.conv(x)
1 Like

Thanks for the answer. I ended up doing the same thing.

1 Like