One padding for the first layer


I would like to use 1- padding instead of 0.
The default value is : padding_mode=‘zeros’, but I would it to be ‘ones’, it doesn’t exist, how could i do it ?
I think it’s not possible because mainly it is a bad idea because the value 1 has no meaning, but in my case, for the first layer, it does. Either i find a nice solution to do it like that or i change my first layer to : x=1-x

Thanks !


You can use explicit padding before passing to conv layers (I assumed you are using conv2d). For this, you can use this snippet:

import torch.nn.functonal as F
import torch

x = torch.randn(1, 1, 3, 3)
x = F.pad(x, pad=(1,1,1,1), mode='constant', value=1)
x  # shape [1, 1, 5, 5]

Then you can pass it to a conv layer by not adding any paddings.


Thanks Nik for taking the time to reply.
So I use that only in the forward right ? and i feed it to a conv2D

You are welcome, Actually, you have not provided much information about what you want to do. But if your problem is padding as you mentioned previously, you can do it using the snippet. But yes, you can now feed it in forward function to a conv2d.

I use this padding for the first conv layer of a Go IA.
Thanks Nik, I’ll try next week (I first have to learn pybind11 to reuse C++ code) you’re solution :slight_smile:

1 Like