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

Hi everyone,

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…

Image Size: 224x224 RGB

class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        # input img: (32, 3, 224, 224) -> 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

I then create a network instance:

cnn = CNN()

Load a batch of data and give them to the cnn:

images, labels = next(iter(trainloader))
images.shape

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

cnn(images)

Tensor shapes as printed:

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])

Happy to hear your thoughts.

where in the forward method does the runtime error appear ? and what is the shape of your inputs ?

Here is the full error message:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-225-79567a9d92ae> in <module>
----> 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:
--> 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

<ipython-input-222-a9d57d96c2fa> in forward(self, t)
     31         t = F.relu(t)
     32 
---> 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:
--> 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) -> Tensor:
---> 94         return F.linear(input, self.weight, self.bias)
     95 
     96     def extra_repr(self) -> 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)
-> 1753     return torch._C._nn.linear(input, weight, bias)
   1754 
   1755 

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

So the error appears when t = self.fc1(t) is called - so after I flattened the tensor.

The shape of my batch of images: (32, 3, 224, 224)

There is a copy/paste error:

you are using self.fc1 twice instead of self.fc2

No way - a simple mistake held me up for days… I guess I should be more precise with coding and bug fixing…

Thanks!