RuntimeError: size mismatch, m1: [4 x 512], m2: [1024 x 512]

Tehreem: <p>Hi,<br>
I am facing this issue, how to fix this?<br>
This is my forward function:</p>
<pre><code>def forward(self, input, time_window=100):
    c1_mem = c1_spike = torch.zeros(batch_size, 16, 28, 28, device=device)
    print(c1_mem.shape)
    c2_mem = c2_spike = torch.zeros(batch_size, 32, 14, 14, device=device)
    print(c2_mem.shape)
    c3_mem = c3_spike = torch.zeros(batch_size, 64, 7, 7, device=device)
    print(c3_mem.shape)

    h1_mem = h1_spike = h1_sumspike = torch.zeros(batch_size, 512, device=device)
    print(h1_mem.shape)
    h2_mem = h2_spike = h2_sumspike = torch.zeros(batch_size, 10, device=device)
    print(h2_mem.shape)

    for step in range(time_window):  # simulation time steps

        # x = input.expand(input.data.shape[0], 3, 28, 28)
        # print(x.shape)
        x = input &gt; torch.rand(input.size(), device=device)  # prob. firing
        # print(x.shape)

        c1_mem, c1_spike = mem_update(self.conv1, x.float(), c1_mem, c1_spike)
        # print(c1_mem.shape)

        x = F.avg_pool2d(c1_spike, 2)

        c2_mem, c2_spike = mem_update(self.conv2, x, c2_mem, c2_spike)

        x = F.avg_pool2d(c2_spike, 2)

        c3_mem, c3_spike = mem_update(self.conv3, x, c3_mem, c3_spike)
        # print(c3_mem.shape)

        x = F.avg_pool2d(c3_spike, 4)

        x = x.view(-1,512)
        # print(x.shape)

        h1_mem, h1_spike = mem_update(self.fc1, x, h1_mem, h1_spike)
        h1_sumspike += h1_spike
        h2_mem, h2_spike = mem_update(self.fc2, h1_spike, h2_mem, h2_spike)
        h2_sumspike += h2_spike

    outputs = h2_sumspike / time_window
    return outputs</code></pre>

Usama_Hasan: <p>The error is quite clear, it should be m1 : [4 x 512] , m2:[512 x 1024].<br>
Your model Fully connected layer has input 1024, while conv layers output 512.<br>
Share your model too.</p>

Tehreem: <p>This is my model:<br>
class SCNN(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(SCNN, self).<strong>init</strong>()</p>
<pre><code>    self.conv1 = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, stride=1, padding=1, bias=False)

    self.conv2 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1, bias=False)

    self.conv3 = nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, stride=1, padding=1, bias=False)

    self.fc1 = nn.Linear(128, 1024, bias=False)

    self.fc2 = nn.Linear(1024, 10)</code></pre>

Usama_Hasan: <aside class="quote no-group" data-username="Tehreem" data-post="1" data-topic="89847">
<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/tehreem/48/41058_2.png" class="avatar"> Tehreem:</div>
<blockquote>
<p><code>        x = x.view(-1,512)</code></p>
</blockquote>
</aside>
<p>a better way to write this since it flatten the output of conv layer is</p>
<pre><code class="lang-auto">shape = torch.prod(torch.tensor(x.shape[1:])).item()
x =  x.view(-1, shape)
</code></pre>
<p>Also</p>
<aside class="quote no-group" data-username="Tehreem" data-post="3" data-topic="89847">
<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/tehreem/48/41058_2.png" class="avatar"> Tehreem:</div>
<blockquote>
<p><code>    self.fc1 = nn.Linear(128, 1024, bias=False)</code></p>
</blockquote>
</aside>
<p>should be</p>
<pre><code class="lang-auto">self.fc1 = nn.Linear(Number of Flattened Features from last layer , 1024,...)
</code></pre>
<p>Further, read this post for better understanding.</p>
<aside class="quote quote-modified" data-post="1" data-topic="5983">
  <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/ngap_wei_tham/48/8558_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/flatten-layer-of-pytorch-build-by-sequential-container/5983">Flatten layer of PyTorch build by sequential container</a> 
  </div>
  <blockquote>
    I am trying to build a cnn by sequential container of PyTorch, my problem is I cannot figure out how to flatten the layer. 
main = nn.Sequential()
self._conv_block(main, 'conv_0', 3, 6, 5)
main.add_module('max_pool_0_2_2', nn.MaxPool2d(2,2))
self._conv_block(main, 'conv_1', 6, 16, 3)
main.add_module('max_pool_1_2_2', nn.MaxPool2d(2,2)) 
main.add_module('flatten', make_it_flatten)

What should I put in the “make_it_flatten”? I tried to flatten the main but it do not work, main do not exist someth…
  </blockquote>
</aside>