Conv2d expects the inputs as its parameters

Ron3: <p>Hello<br>
I am currently still trying to get a custom conv2d implementation going and I thought the creation of it is going well, but there is a bug I just can’t get my head around. Whenever I try to run the convolution I get the following error:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "C:\Users\Ron Rödel\PycharmProjects\lbcnnn\main.py", line 76, in &lt;module&gt;
    p(r)
  File "C:\Users\Ron Rödel\PycharmProjects\lbcnnn\venv\lib\site-packages\torch\nn\modules\module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "C:\Users\Ron Rödel\PycharmProjects\lbcnnn\main.py", line 63, in forward
    x = F.sigmoid(self.conv[j](x))
  File "C:\Users\Ron Rödel\PycharmProjects\lbcnnn\venv\lib\site-packages\torch\nn\modules\module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "C:\Users\Ron Rödel\PycharmProjects\lbcnnn\venv\lib\site-packages\torch\nn\modules\conv.py", line 463, in forward
    return self._conv_forward(input, self.weight, self.bias)
  File "C:\Users\Ron Rödel\PycharmProjects\lbcnnn\venv\lib\site-packages\torch\nn\modules\conv.py", line 459, in _conv_forward
    return F.conv2d(input, weight, bias, self.stride,
RuntimeError: Given groups=1, weight of size [3, 2, 3, 3], expected input[2, 1, 5, 5] to have 2 channels, but got 1 channels instead

Process finished with exit code 1

</code></pre>
<p>If I were to change the input to [2,2,5,5]<br>
Nothing meaning full changes (I mention this, because to my understanding the second input changes the amount of channels, but if I do so the dims just get weirder)</p>
<p>Here is how I currently build my custom conv2d</p>
<pre><code class="lang-auto">    def __init__(self, i, o, m):
        super(Lbcnn, self).__init__()
        self.conv = nn.ModuleList([nn.Conv2d(i, o, m, bias=False)for y in range(m*m-1)])

        #self.conv = self.conv.weight
        self.conv.requires_grad = False
        #print(self.conv)
        for z in range(o):
            for s in range(i):
                for u in range(m*m-1):
                    with torch.no_grad():
                        self.conv[u].weight[z][s][math.floor(m/2)][math.floor(m/2)] = -1
                    for t in range(m * m - 1):
                        itemp = getIfromU(t, m)
                        jtemp = getJfromU(t, m)
                        with torch.no_grad():
                            self.conv[u].weight[z][s][itemp][jtemp] = 0
                    itemp = getIfromU(u, m)
                    jtemp = getJfromU(u, m)
                    with torch.no_grad():
                        self.conv[u].weight[z][s][itemp][jtemp] = 1
                    print(u)
                    print(itemp)
                    print(jtemp)
                    print(self.conv[u].weight)
                    print('')
        masks = m*m-1
        self.comb = nn.Conv2d(masks*i, o, 1)
</code></pre>
<p>What it does properly is to manipulate the weights how I want them to, but just cannot find the dimensioning error.</p>
<p>(In case it is needed the forward function and how I call the function):</p>
<pre><code class="lang-auto">    def forward(self, x):
        m = self.conv[0].kernel_size[0]
        # print(m)
        for j in range(m * m - 1):
            x = F.sigmoid(self.conv[j](x))
        x = self.comb(x)
        # print(self.comb.weight)
        return x
        #print(x)
        x = self.comb
        print(self.comb.weight)

        return x


p = Lbcnn(2, 3, 3)
r = torch.ones(2, 2, 5, 5)
p(r)

</code></pre>
<p>Any help would be greatly appreciated</p>

Ron3: <p>One quick thing that I noticed, if I make p = Lbcnn(2,2,3) and r= torch.ones(2,2,8,8) I can get a different error alltogether, namely the following:<br>
RuntimeError: Calculated padded input size per channel: (2 x 2). Kernel size: (3 x 3). Kernel size can’t be greater than actual input size.<br>
The reace back is still the same</p>

ptrblck: <aside class="quote no-group" data-username="Ron3" data-post="1" data-topic="189223">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/9e8a1a/48.png" class="avatar"> Ron3:</div>
<blockquote>
<p>If I were to change the input to [2,2,5,5]<br>
Nothing meaning full changes</p>
</blockquote>
</aside>
<p>This shouldn’t be the case and it seems you are indeed fixing the first error causing the next one:</p>
<aside class="quote no-group" data-username="Ron3" data-post="2" data-topic="189223">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/9e8a1a/48.png" class="avatar"> Ron3:</div>
<blockquote>
<p>if I make p = Lbcnn(2,2,3) and r= torch.ones(2,2,8,8) I can get a different error alltogether, namely the following:<br>
RuntimeError: Calculated padded input size per channel: (2 x 2). Kernel size: (3 x 3). Kernel size can’t be greater than actual input size</p>
</blockquote>
</aside>
<p>As the error message describes the spatial size of the activation input is too small for the kernel size of the conv layer so either increase the spatial size of the input of decrease it of the kernel.</p>

Ron3: <p>I apolgize for the late answer.<br>
Your answer helped a bit, but I now found the actual error that is at least currently happening.<br>
Due to the way I do the forward function it (now in hindsight obviously) just iterates over the input reducing the size each time.<br>
So I need to look for a way to proberly store the input in an array or something, so that I can access the original input multiple times</p>