Answers.Body: <p>In a few words, <strong>we add <span class="math-container">$1$</span> to account for the initial position of the kernel</strong>.</p>
  <p>You can easily see this if you let <span class="math-container">$s = 1$</span> (unit stride) and <span class="math-container">$p = 0$</span> (i.e. no padding), so your formula simplifies to</p>
  <p><span class="math-container">\begin{align}
  W_2 = (W_1 - F) + 1, \label{1}\tag{1}
  \end{align}</span></p>
  <p>So, in this simplified case and, for simplicity, assuming squared inputs and kernels, the width (or height) of the output of the convolutional layer is the number of steps that we slide the kernel horizontally (or vertically, respectively), for example, starting from the top left of the input, plus the initial position of the kernel. In this case, <span class="math-container">$(W_1 - F)$</span> is the number of times we slide the kernel horizontally (or vertically) and <span class="math-container">$+1$</span> is to account for the initial position of the kernel.</p>
  <p>Here's an animation of this simplified case for <span class="math-container">$W_1 = 4$</span> and <span class="math-container">$F = 3$</span> (the green matrix is the output, while the blue one is the input).</p>
  <p><a href="https://i.sstatic.net/m1X4a.gif" rel="nofollow noreferrer"><img src="https://i.sstatic.net/m1X4a.gif" alt="enter image description here" /></a></p>
  <p>So, if we apply the formula \ref{1}, we should get <span class="math-container">$W_2 = 2$</span>, as we can see from the animation above, which produces a <span class="math-container">$2 \times 2$</span> matrix. In fact, <span class="math-container">$(4 - 3) + 1 = 2 = W_2$</span>. You can see from the animation that we slide the kernel only once in each of the axes, but the shape of the output matrix is <span class="math-container">$2 \times 2$</span> because we compute the dot product between the kernel and the submatrix of the input that corresponds to the initial position of the kernel.</p>
  <p>A detailed explanation of this (section 2.4, relationship 6) and other simpler formulas to compute the size of the output of a convolutional layer can be found in the report <a href="https://arxiv.org/pdf/1603.07285.pdf" rel="nofollow noreferrer">A guide to convolution arithmetic for deep learning</a>, which has also many images that illustrate the concepts (you can find animations <a href="https://github.com/vdumoulin/conv_arithmetic" rel="nofollow noreferrer">here</a> too).</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-04T13:03:30.183Z
Answers.LastActivityDate: 2021-10-08T12:15:45.037Z
Answers.LastEditDate: 2021-10-08T12:15:45.037Z
Answers.UserDisplayName: nbro
Body: <p>In the formula to calculate output shape of tensor after convolution operation
  <span class="math-container">$$
  W_2 = (W_1-F+2P)/S + 1,
  $$</span>
  where:</p>
  <ul>
  <li><span class="math-container">$W_2$</span> is the output shape of the tensor</li>
  <li><span class="math-container">$W_1$</span> is the input shape</li>
  <li><span class="math-container">$F$</span> is the filter size</li>
  <li><span class="math-container">$P$</span> is the padding</li>
  <li><span class="math-container">$S$</span> is the stride.</li>
  </ul>
  <p>Why do we add <span class="math-container">$1$</span>? It gets us to the correct answer, but how is this formula derived?</p>
  <p>Source: <a href="https://cs231n.github.io/convolutional-networks/#pool" rel="nofollow noreferrer">https://cs231n.github.io/convolutional-networks/#pool</a></p>
  
Tags: <convolutional-neural-networks><convolution><convolutional-layers><convolution-arithmetic>
Title: Why do we add 1 in the formula to calculate the shape of the output of the convolution?
UserDisplayName: adityagabbar
ThreadText: # Why do we add 1 in the formula to calculate the shape of the output of the convolution?
  
  (Asked by: username_0 on 2021-08-02)
  
  In the formula to calculate output shape of tensor after convolution operation
  $$
  W\_2 = (W\_1-F+2P)/S + 1,
  $$
  where:
  
  * $W\_2$ is the output shape of the tensor
  * $W\_1$ is the input shape
  * $F$ is the filter size
  * $P$ is the padding
  * $S$ is the stride.
  
  Why do we add $1$? It gets us to the correct answer, but how is this formula derived?
  
  Source: <https://cs231n.github.io/convolutional-networks/#pool>
  
  **Tags:** convolutional-neural-networks, convolution, convolutional-layers, convolution-arithmetic
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-08-04. Score: 2 | Accepted answer
  
  In a few words, **we add $1$ to account for the initial position of the kernel**.
  
  You can easily see this if you let $s = 1$ (unit stride) and $p = 0$ (i.e. no padding), so your formula simplifies to
  
  \begin{align}
  W\_2 = (W\_1 - F) + 1, \label{1}\tag{1}
  \end{align}
  
  So, in this simplified case and, for simplicity, assuming squared inputs and kernels, the width (or height) of the output of the convolutional layer is the number of steps that we slide the kernel horizontally (or vertically, respectively), for example, starting from the top left of the input, plus the initial position of the kernel. In this case, $(W\_1 - F)$ is the number of times we slide the kernel horizontally (or vertically) and $+1$ is to account for the initial position of the kernel.
  
  Here's an animation of this simplified case for $W\_1 = 4$ and $F = 3$ (the green matrix is the output, while the blue one is the input).
  
  [![enter image description here](https://i.sstatic.net/m1X4a.gif)](https://i.sstatic.net/m1X4a.gif)
  
  So, if we apply the formula \ref{1}, we should get $W\_2 = 2$, as we can see from the animation above, which produces a $2 \times 2$ matrix. In fact, $(4 - 3) + 1 = 2 = W\_2$. You can see from the animation that we slide the kernel only once in each of the axes, but the shape of the output matrix is $2 \times 2$ because we compute the dot product between the kernel and the submatrix of the input that corresponds to the initial position of the kernel.
  
  A detailed explanation of this (section 2.4, relationship 6) and other simpler formulas to compute the size of the output of a convolutional layer can be found in the report [A guide to convolution arithmetic for deep learning](https://arxiv.org/pdf/1603.07285.pdf), which has also many images that illustrate the concepts (you can find animations [here](https://github.com/vdumoulin/conv_arithmetic) too).
  
  
  ---
  
  