How can I get the same size output?

Using 2x2 conv, how to get the same size output as input?

nn.Conv2d(input, output, kernel_size=2, stride=1, padding=padding)

Like above, when I set stride=1, padding must be 0.5 but it’s impossible.

Thank you.


I do not know the input size, but if 0.5 padding size is the answer, it is impossible, as we do not have half-pixel. That is one of the reasons that odd size kernels are preferred. Also, odd size kernels have a single pixel as the origin cell, which enables better weighting.

But if you like one sided padding, you can do this by explicitly padding your input then applying conv2d. For padding you can use torch.nn.functiona.pad function. See this post for more information:

The reason I suggested one side padding is that, PyTorch by default uses two sided paddings so I think that is why you ended up with padding size of 0.5. S, other choice could be padding only one side.