How many filters in Conv2d?

111353: <p>I couldn’t understand how many filters used in Conv2d.<br>
This is my code, and please see the picuture.<br>
At first I thought fig1 was correct, but when I looked at the code, fig2 seems to be correct.<br>
Can someone give me a reference on this matter?<br>
Thank you for reading to the last.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/1/4168219c998b3494672cbd1a8c27e68c5150f738.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/4168219c998b3494672cbd1a8c27e68c5150f738" title="IMG_0089"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/4/1/4168219c998b3494672cbd1a8c27e68c5150f738_2_666x500.jpeg" alt="IMG_0089" data-base62-sha1="9kC8qdcJVS9oLviHspQNz0ItK5O" width="666" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/4/1/4168219c998b3494672cbd1a8c27e68c5150f738_2_666x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/4/1/4168219c998b3494672cbd1a8c27e68c5150f738_2_999x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/4/1/4168219c998b3494672cbd1a8c27e68c5150f738_2_1332x1000.jpeg 2x" data-dominant-color="B9B7B5"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">IMG_0089</span><span class="informations">4032×3024 428 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<pre><code class="lang-auto">import torch.nn as nn
import torch

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(in_channels = 2, out_channels = 2, kernel_size = 3)

    def forward(self, x):
        x = self.conv1(x)
        return x
net = Net()
print(net)

params = list(net.parameters())
print(params[0])
print(params[0].size())


======================================
~~ output on my colab, and I can see "4" (3*3)tensors. I thougt that are filters~~

Net(
  (conv1): Conv2d(2, 2, kernel_size=(3, 3), stride=(1, 1))
)
Parameter containing:
tensor([[[[-0.1645, -0.2205,  0.0995],
          [ 0.2017, -0.1659,  0.0161],
          [ 0.0099,  0.1740,  0.0792]],

         [[-0.1067,  0.1234,  0.0129],
          [-0.1366, -0.0107,  0.0756],
          [ 0.1778, -0.1056,  0.2191]]],


        [[[-0.0351,  0.0904, -0.1394],
          [-0.1006,  0.2080,  0.1312],
          [-0.1741, -0.0246, -0.0775]],

         [[-0.0482, -0.0906, -0.1982],
          [ 0.2164,  0.0711, -0.0212],
          [-0.0277, -0.0861, -0.1908]]]], requires_grad=True)
</code></pre>

PresidentDoggo: <p>I think figure 2 is trying to show how the convolution operation is happening inside the convolution layer.<br>
I made a diagram based off my understanding of convnets to try and help</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/b/db0598d4d7d2779418c0df7f988d7bd5ef3fff77.png" data-download-href="https://discuss.pytorch.org/uploads/default/db0598d4d7d2779418c0df7f988d7bd5ef3fff77" title="conv"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/b/db0598d4d7d2779418c0df7f988d7bd5ef3fff77_2_690x334.png" alt="conv" data-base62-sha1="vfyyzbyg3WgeDYWA8RZlDjeLkIT" width="690" height="334" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/b/db0598d4d7d2779418c0df7f988d7bd5ef3fff77_2_690x334.png, https://discuss.pytorch.org/uploads/default/original/3X/d/b/db0598d4d7d2779418c0df7f988d7bd5ef3fff77.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/d/b/db0598d4d7d2779418c0df7f988d7bd5ef3fff77.png 2x" data-dominant-color="CFDFDB"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">conv</span><span class="informations">851×412 11.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>There are two filters in the network as <code>out_channel = 2</code>.<br>
<code>in_channel = 2</code> and <code>kernel_size = 3</code> therefore filters are of size [3 x 3 x 2].</p>
<p>In my diagram it show 2 [3 x 3 x 2] filters performing the convolution operation on the same input image. You have 4 tensor outputs because there are 4 [3 x 3] kernels.</p>
<blockquote>
<p>EDIT:<br>
The output of this operation is a feature map of size:</p>
<p><code>(𝑊−𝐹+2𝑃)/𝑆+1</code></p>
<p>𝑊 is the input volume<br>
𝑃 is padding<br>
𝐹 is filter size<br>
𝑆 is stride</p>
</blockquote>
<p>Hope this helps!</p>

harsha_g: <aside class="quote no-group quote-modified" data-username="PresidentDoggo" data-post="2" data-topic="86886">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/presidentdoggo/48/25223_2.png" class="avatar"> PresidentDoggo:</div>
<blockquote>
<p>[input image height x input image width x out channels]</p>
</blockquote>
</aside>
<p>You meant <code>[output image height x output image width x out channels]</code>. Didn’t you?</p>

PresidentDoggo: <p>It’s the same dimensions as the input image is what I meant.</p>

111353: <p>Thank you sooo much, Mr PresidentDoggo.<br>
I’m sorry but can I ask  more?<br>
I check circle in your figure, and Does this part add up? Or is it the norm or other<img src="https://discuss.pytorch.org/uploads/default/original/3X/d/5/d5b55fd82d96143eb19e5e3dd4d4b9d43af79e9a.jpeg" alt="Inkeddb0598d4d7d2779418c0df7f988d7bd5ef3fff77_2_690x334_LI" data-base62-sha1="uuyiek8BzRq8Jn2CHrxP7e3cwQO" width="690" height="334"></p>

111353: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/4/3439b2942eadea97711a71dbd87b65627629e4dd.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/3439b2942eadea97711a71dbd87b65627629e4dd" title="IMG_0091"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/4/3439b2942eadea97711a71dbd87b65627629e4dd_2_666x500.jpeg" alt="IMG_0091" data-base62-sha1="7s0rAmLuAY5aA34o6v6VyEfPQ2V" width="666" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/4/3439b2942eadea97711a71dbd87b65627629e4dd_2_666x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/3/4/3439b2942eadea97711a71dbd87b65627629e4dd_2_999x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/3/4/3439b2942eadea97711a71dbd87b65627629e4dd_2_1332x1000.jpeg 2x" data-dominant-color="B7B4AD"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">IMG_0091</span><span class="informations">3024×2268 207 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

harsha_g: <p>That’s only in the case of padding.</p>

111353: <p>Mr PresidentDoggo<br>
Thank you very much for your kindness.<br>
Your help was very useful to me.</p>
<p>I couldn’t understand to merge two layer in one layer yet,<br>
Mr harsha_g, thank you for replying,<br>
Could I ask detail?</p>

PresidentDoggo: <p><a class="mention" href="/u/111353">@111353</a> you’re welcome <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>To answer your question about merging the two layers its summed and then the result is offset by the bias. [source: <a href="https://cs231n.github.io/convolutional-networks/" rel="nofollow noopener">Stanford CS convnets</a>] (Also a great place to learn more!)<br>
Although I have seen some literature do an average.</p>
<p><a class="mention" href="/u/harsha_g">@harsha_g</a> is right, I made a slight mistake with the output!</p>
<p>The output of the conv operation is not the size of the input image<br>
We can calculate the change in dimensionality from the CONV operation using this equation:</p>
<p><code>(𝑊−𝐹+2𝑃)/𝑆+1</code></p>
<p>𝑊 is the input volume<br>
𝑃 is padding<br>
𝐹 is filter size<br>
𝑆 is stride</p>

harsha_g: <p><a class="mention" href="/u/111353">@111353</a> see if this <a href="https://youtu.be/KTB_OFoAQcc" rel="nofollow noopener">video</a> can help clarify your doubt.</p>

111353: <p>Mr <a class="mention" href="/u/presidentdoggo">@PresidentDoggo</a><br>
Thank you very much for giving me a source and helpful your information.<br>
Mr <a class="mention" href="/u/harsha_g">@harsha_g</a><br>
Thank you very much for giving me a  nice video and joining this topics.</p>