1

enter image description here

Is the calculated output correct?

noe
  • 26,410
  • 1
  • 46
  • 76
PeterBe
  • 83
  • 2
  • 10

1 Answers1

3

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]]]
noe
  • 26,410
  • 1
  • 46
  • 76