From CNN, 2D Convolution output size is (Input height - Kernel height + 1) * (Input width - Kernel width + 1), where padding = 0 and stride = 1. I was trying to find how to compute a convolution operation with "matrix multiplication", and the solution was to use a doubly block circulant matrix. Reading a few posts about how to implement doubly block circulant matrix, I realize that they have used a different way in computing the convolution size: (Input height + Kernel height - 1) * (Input width + Kernel width - 1). Can you give me an insight into why they use the different computation for the convolution output size?
1 Answer
Using a quote from wikipedia
In mathematics, convolution is a mathematical operation on two functions ($f$ and $g$) that produces a third function ($f ∗ g$).
If you look carefully, you'll find that there are no real boundaries in the mathematical definition of a convolution — i.e., theoretically there is an output for every pixel index (cf. infinite zero-padding). However, since most of these outputs would be zero anyway (the kernel does not overlap with the actual image), these are generally ignored in the context of signal processing.
Now, in a general signal processing context, the output of the convolution is considered to be every output for which the value is non-zero. This corresponds to padding the input with $K-1$ zeros. In the context of neural networks, on the other hand, a lot of non-zero outputs are effectively ignored by using no (or little) input padding.
The sources you linked to talk about convolutions in the context of signal processing, not in the context of neural networks, which explains the discrepancy.
-
$\begingroup$ Thank you for your answer Mr. Tsjolder. Can I ask a question about your answer? Why every non-zero output corresponds to padding with the input with K-1 zeros. Shouldn't it be 2*(K-1) because zero padding has to be added to top and bottom (not considering left and right for now). Let's say we have two matrices: f:3x3 and g:2x2 . The non-zero output shouldn't be 5x5 instead of 4x4? $\endgroup$– KayCommented Aug 18, 2021 at 7:47
-
$\begingroup$ Yes, you have to pad both sides. Normally, in the context of neural networks, padding is symmetric, so I just specified the padding for one side (e.g., see cs231) $\endgroup$ Commented Aug 18, 2021 at 7:58