Shobana
Shobana

Reputation: 41

Runtime Error: mat1 and mat2 shapes cannot be multiplied in pytorch

I'm new to deep learning and I have created a model using the code below for the prediction of plant disease

class CNN_Model(nn.Module):
  def __init__(self):
    super(CNN_Model, self).__init__()
    self.cnn_model = nn.Sequential(
        nn.Conv2d(3, 16, 3),    
        nn.ReLU(),
        nn.MaxPool2d(2, 2),     
        nn.Conv2d(16, 32, 5),   
        nn.ReLU(),
        nn.MaxPool2d(2, 2),     
    )

    self.fc_model = nn.Sequential(
        nn.Flatten(),           
        nn.Linear(800, 300),    
        nn.ReLU(),
        nn.Linear(300, 38),     
        nn.Softmax(dim=1)
    )

  def forward(self, x):
      x = self.cnn_model(x)
      x = self.fc_model(x)

      return x
model = CNN_Model()

out = model(imgs)
out

When I'm trying to run the above code, I'm getting the error mat1 and mat2 cannot be multiplied. I have tried the answers posted for questions similar to this, but still, my issue is not solved.

RuntimeError                              Traceback (most recent call last)
/tmp/ipykernel_66/1768380315.py in <module>
----> 1 out = model(imgs)
      2 out

/opt/conda/lib/python3.7/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 = [], []

/tmp/ipykernel_66/1577403502.py in forward(self, x)
     26   def forward(self, x):
     27       x = self.cnn_model(x)
---> 28       x = self.fc_model(x)
     29 
     30       return x

/opt/conda/lib/python3.7/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 = [], []

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/container.py in forward(self, input)
    137     def forward(self, input):
    138         for module in self:
--> 139             input = module(input)
    140         return input
    141 

/opt/conda/lib/python3.7/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 = [], []

/opt/conda/lib/python3.7/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:

/opt/conda/lib/python3.7/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 (32x119072 and 800x300)

Please someone help me to solve these errors.

Upvotes: 4

Views: 44799

Answers (1)

Ivan
Ivan

Reputation: 40648

The size mismatch error is shown as 32x119072 and 800x300. The first shape refers to the input tensor, while the second is the parameter of the layer. If you look into your model definition you will see that it matches the first fully connected layer, the one following the flatten. Indeed, nn.Linear(800, 300) was expecting 800-feature tensors, but got 119072-feature tensors instead.

You need to modify this linear layer to match the incoming tensor flattened spatial shape. But notice how this value will depend on the image that is fed to the CNN, ultimately this will dictate the size of the tensor fed to the classifier. The general way to solve this is to use adaptive layers: such as nn.AdaptiveMaxPool2d which will always provide the same output shape regardless of the input dimension size.

Upvotes: 15

Related Questions