RuntimeError mat1 and mat2 can't be multiplied when training NN

miguel_angel: <p>Hello! I’m very new to pytorch and I’m trying to write my first image classifier. I built my neural network based on the code from the <a href="https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html" rel="noopener nofollow ugc">pytorch website</a></p>
<p>I keep getting the following RunTime Error:</p>
<pre><code class="lang-auto">RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-21-44fe8e062719&gt; in &lt;module&gt;
     10 
     11         # forward + backward + optimize
---&gt; 12         outputs = classifier(inputs)
     13 #         loss = criterion(outputs, labels)
     14 #         loss.backward()

C:\Aeroapps\AnacondaP3x64-2020.11\envs\pytorch\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
   1049         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1050                 or _global_forward_hooks or _global_forward_pre_hooks):
-&gt; 1051             return forward_call(*input, **kwargs)
   1052         # Do not call functions when jit is used
   1053         full_backward_hooks, non_full_backward_hooks = [], []

&lt;ipython-input-20-ea37fecec224&gt; in forward(self, x)
     18 #         x = self.pool(F.relu(self.conv2(x)))
     19         x = torch.flatten(x, 1) # flatten all dimensions except batch
---&gt; 20         x = F.relu(self.fc1(x)) # THIS IS WHERE THE ERROR OCCURS
     21         x = F.relu(self.fc2(x))
     22         x = self.fc3(x)

C:\Aeroapps\AnacondaP3x64-2020.11\envs\pytorch\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
   1049         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1050                 or _global_forward_hooks or _global_forward_pre_hooks):
-&gt; 1051             return forward_call(*input, **kwargs)
   1052         # Do not call functions when jit is used
   1053         full_backward_hooks, non_full_backward_hooks = [], []

C:\Aeroapps\AnacondaP3x64-2020.11\envs\pytorch\lib\site-packages\torch\nn\modules\linear.py in forward(self, input)
     94 
     95     def forward(self, input: Tensor) -&gt; Tensor:
---&gt; 96         return F.linear(input, self.weight, self.bias)
     97 
     98     def extra_repr(self) -&gt; str:

C:\Aeroapps\AnacondaP3x64-2020.11\envs\pytorch\lib\site-packages\torch\nn\functional.py in linear(input, weight, bias)
   1845     if has_torch_function_variadic(input, weight):
   1846         return handle_torch_function(linear, (input, weight), input, weight, bias=bias)
-&gt; 1847     return torch._C._nn.linear(input, weight, bias)
   1848 
   1849 

RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x150528 and 802816x120)
</code></pre>
<p>I’ve gone through every line and I’m not sure where the bug is. Can someone help me with this? I posted the code below!</p>
<pre><code class="lang-auto">import torch.nn as nn
import torch.nn.functional as F

class Test_Network(nn.Module):
    def __init__(self):
        super().__init__()
        # input image: (32, 3, 224, 224) -&gt; batch size, number of channels, height, width
        
        self.conv1 = nn.Conv2d(3, 6, 3) # input: 3 channels -&gt; output: 6 channels
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 3) # input: 6 channels -&gt; output: 16 channels
        self.fc1 = nn.Linear(16 * 224 * 224, 120) # 16 * 224 * 224 -&gt; 16: output from convolution, 224: iamge size
        self.fc2 = nn.Linear(120, 84) # going from 120 sample to 84.
        self.fc3 = nn.Linear(84, 10) # going from 84 sample to 10

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = torch.flatten(x, 1) # flatten all dimensions except batch
        x = F.relu(self.fc1(x)) # THIS IS WHERE THE ERROR OCCURS
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x


classifier = Test_Network()
</code></pre>
<pre><code class="lang-auto">for epoch in range(2):  # loop over the dataset multiple times

    running_loss = 0.0
    for i, data in enumerate(trainloader, 0):
        # get the inputs; data is a list of [inputs, labels]
        inputs, labels = data

        # zero the parameter gradients
        optimizer.zero_grad()

        # forward + backward + optimize
        outputs = classifier(inputs)
#         loss = criterion(outputs, labels)
#         loss.backward()
#         optimizer.step()

#         # print statistics
#         running_loss += loss.item()
#         if i % 2000 == 1999:    # print every 2000 mini-batches
#             print('[%d, %5d] loss: %.3f' %
#                   (epoch + 1, i + 1, running_loss / 2000))
#             running_loss = 0.0

print('Finished Training')
</code></pre>

ptrblck: <p>Based on the error message the error is raised in <code>self.fc1</code>, which expects <code>16 * 224 * 224 = 802816</code> input features, while the incoming activation contains <code>150528</code> features.<br>
You can fix it by setting <code>in_features=150528</code> in <code>self.fc1</code>.</p>

Vrushank98: <p>Every time you use Maxpool2d with stride 2, it halves your image resolution.<br>
Here, You have used Maxpool2D twice, so your image size before passing it to the linear layer is (3 * 56 * 56),<br>
So considering output channels = 16, size of input tensor to the linear layer should be (16 * 3 * 56 * 56) = 150528.</p>