1
$\begingroup$

enter image description here

Is the calculated output correct?

$\endgroup$

1 Answer 1

3
$\begingroup$

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]]]
$\endgroup$
1
  • $\begingroup$ Thanks Noe for your answer. I really appreciate it. $\endgroup$
    – PeterBe
    Sep 1, 2023 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.