The default padding mode seems not to be zero?

I’m new to pytorch and I found that the padding mode of conv2d is not zero padding in the following code:

import torch
import torch.nn.functional as F
a = torch.tensor([[[[1,2,3,4],[4,5,6,7],[7,8,9,10]]]])
weight = torch.ones((1,1,3,3)).to(torch.long)
F.conv2d(a, weight, stride=1, padding=1)

The output is:

tensor([[[[12, 21, 27, 20],
          [27, 45, 54, 39],
          [24, 39, 45, 32]]]])

When without padding, the output is:

tensor([[[[45, 54]]]])

I read from the document that the default padding mode is “zero”, which means use zero to pad output tensors. So is there anything that changes the mode?

The default padding value is 0. and you can manually verify it:

import torch
import torch.nn.functional as F

a = torch.tensor([[[[1,2,3,4],[4,5,6,7],[7,8,9,10]]]])
weight = torch.ones((1,1,3,3)).to(torch.long)
ref = F.conv2d(a, weight, stride=1, padding=1)

a_pad = F.pad(a, pad=(1, 1, 1, 1), value=0.)
print(a_pad)

out = F.conv2d(a_pad, weight, stride=1, padding=0)

print((ref - out).abs().max())
# tensor(0)

The output will of course not contain zeros at the border since your kernel has a spatial size of 3x3 and will thus process the zero padded border as well as values from the input.