Configure a convolutional layer so the padding can have 4 parameters/dimensions

I’d like to create a Conv2d with different values for the 4 dimensions of the padding. For example, I’d like to do something like this:

m = nn.Conv2d(16, 33, (3, 5), stride=(1, 1), padding=(0,0, 1,1))

According to the official documentation that’s not currently possible, but I’d like to know if there’s any way or workaround to achieve this.

Thanks in advance.


The simplest solution I can think of is that you create a wrapper class around available nn.Conv2d but instead of passing any paddings to nn.Conv2d object, you use explicit padding by using torch.nn.functional.pad.

Following post explains how to use explicit padding and wrapping it into another class that contains nn.Conv2d would pretty much easy.