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

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 pytorch website

I keep getting the following RunTime Error:

RuntimeError                              Traceback (most recent call last)
<ipython-input-21-44fe8e062719> in <module>
     10 
     11         # forward + backward + optimize
---> 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):
-> 1051             return forward_call(*input, **kwargs)
   1052         # Do not call functions when jit is used
   1053         full_backward_hooks, non_full_backward_hooks = [], []

<ipython-input-20-ea37fecec224> in forward(self, x)
     18 #         x = self.pool(F.relu(self.conv2(x)))
     19         x = torch.flatten(x, 1) # flatten all dimensions except batch
---> 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):
-> 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) -> Tensor:
---> 96         return F.linear(input, self.weight, self.bias)
     97 
     98     def extra_repr(self) -> 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)
-> 1847     return torch._C._nn.linear(input, weight, bias)
   1848 
   1849 

RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x150528 and 802816x120)

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!

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) -> batch size, number of channels, height, width
        
        self.conv1 = nn.Conv2d(3, 6, 3) # input: 3 channels -> output: 6 channels
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 3) # input: 6 channels -> output: 16 channels
        self.fc1 = nn.Linear(16 * 224 * 224, 120) # 16 * 224 * 224 -> 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()
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')

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

1 Like

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