[Solved] RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x120 and 33708x120)

davidweb: <p>Hi everyone,</p>
<p>I know this topic has been posted several times - but I still cannot fix it and I just don’t find the mistake in my implementation…</p>
<p>Image Size: 224x224 RGB</p>
<pre><code class="lang-auto">class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        # input img: (32, 3, 224, 224) -&gt; batch, color, height, width
        self.conv1 = nn.Conv2d(3, 6, 5) #input, output, kernel_size
        self.conv2 = nn.Conv2d(6, 12, 5)
        
        self.fc1 = nn.Linear(12*53*53, 120) # creates a matrix with x by 120
        self.fc2 = nn.Linear(120, 60)
        self.out = nn.Linear(60, 10)
        
    def forward(self, t):
        #convlayer
        t = self.conv1(t)
        print('conv1', t.shape)
        t = F.relu(t)
        t = F.max_pool2d(t, kernel_size = 2, stride = 2)
        print('conv1pool', t.shape)
        
        t = self.conv2(t)
        print('conv2', t.shape)
        t = F.relu(t)
        t = F.max_pool2d(t, kernel_size = 2, stride = 2)
        print('conv2pool', t.shape)
        
        #fc
        t = torch.flatten(t, start_dim=1)
        #t = t.reshape(-1, 12*53*53) #flatten tensor
        print('flattened t', t.shape)
        t = self.fc1(t)
        t = F.relu(t)
        
        t = self.fc1(t)
        t = F.relu(t)
        
        t = self.out(t)
        #softmax missing for class probabilities
        
        return t
</code></pre>
<p>I then create a network instance:</p>
<pre><code class="lang-auto">cnn = CNN()
</code></pre>
<p>Load a batch of data and give them to the cnn:</p>
<pre><code class="lang-auto">images, labels = next(iter(trainloader))
images.shape

[222]: torch.Size([32, 3, 224, 224])

cnn(images)
</code></pre>
<p>Tensor shapes as printed:</p>
<pre><code class="lang-auto">conv1 torch.Size([32, 6, 220, 220])
conv1pool torch.Size([32, 6, 110, 110])
conv2 torch.Size([32, 12, 106, 106])
conv2pool torch.Size([32, 12, 53, 53])
flattened t torch.Size([32, 33708])
</code></pre>
<p>Happy to hear your thoughts.</p>

samsja: <p>where in the forward method does the runtime error appear ? and what is the shape of your inputs ?</p>

davidweb: <p>Here is the full error message:</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-225-79567a9d92ae&gt; in &lt;module&gt;
----&gt; 1 cnn(images)

/Applications/anaconda3/envs/masterthesis/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    887             result = self._slow_forward(*input, **kwargs)
    888         else:
--&gt; 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

&lt;ipython-input-222-a9d57d96c2fa&gt; in forward(self, t)
     31         t = F.relu(t)
     32 
---&gt; 33         t = self.fc1(t)
     34         t = F.relu(t)
     35 

/Applications/anaconda3/envs/masterthesis/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    887             result = self._slow_forward(*input, **kwargs)
    888         else:
--&gt; 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

/Applications/anaconda3/envs/masterthesis/lib/python3.8/site-packages/torch/nn/modules/linear.py in forward(self, input)
     92 
     93     def forward(self, input: Tensor) -&gt; Tensor:
---&gt; 94         return F.linear(input, self.weight, self.bias)
     95 
     96     def extra_repr(self) -&gt; str:

/Applications/anaconda3/envs/masterthesis/lib/python3.8/site-packages/torch/nn/functional.py in linear(input, weight, bias)
   1751     if has_torch_function_variadic(input, weight):
   1752         return handle_torch_function(linear, (input, weight), input, weight, bias=bias)
-&gt; 1753     return torch._C._nn.linear(input, weight, bias)
   1754 
   1755 

RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x120 and 33708x120)
</code></pre>
<p>So the error appears when<code> t = self.fc1(t)</code> is called - so after I flattened the tensor.</p>
<p>The shape of my batch of images: (32, 3, 224, 224)</p>

RaLo4: <p>There is a copy/paste error:</p>
<aside class="quote no-group" data-username="davidweb" data-post="1" data-topic="114564">
<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/davidweb/48/35715_2.png" class="avatar"> davidweb:</div>
<blockquote>
<pre><code class="lang-auto">        t = self.fc1(t)
        t = F.relu(t)
        
        t = self.fc1(t)
        t = F.relu(t)
        
        t = self.out(t)
        #softmax missing for class probabilities
        
        return t
</code></pre>
</blockquote>
</aside>
<p>you are using self.fc1 twice instead of self.fc2</p>

davidweb: <p>No way - a simple mistake held me up for days… I guess I should be more precise with coding and bug fixing…</p>
<p>Thanks!</p>