Is the calculated output correct?
1 Answers
For a convolution with default "configuration", your result is correct.
Note that there are other factors defining the behaviour of a convolution apart from the input and kernel, like padding, stride, dilation, etc. For convolutions, typical defaults are padding = 0 and stride = 1. With those values, your result is correct. Here's the code I used to check it:
import torch
import torch.nn.functional as F
x = torch.tensor(
[[[0, 0, 1, 0, 0],
[0, 1, 1, 0, 0],
[1, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0]]])
kernel = torch.tensor(
[[0, 1, 0],
[0, 1, 0],
[0, 1,0]]).unsqueeze(0).unsqueeze(0)
F.conv2d(x, kernel, padding=0, stride=1)
If we set other values for padding and stride, we obtain different results. For instance, with F.conv2d(x, kernel, padding=1, stride=2) we obtain:
[[[0, 2, 0],
[1, 3, 0],
[0, 2, 0]]]
- 26,410
- 1
- 46
- 76
-
Thanks Noe for your answer. I really appreciate it. – PeterBe Sep 01 '23 at 14:49
