TylerYep / torchinfo

View model summaries in PyTorch!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mat1 and mat2 shapes cannot be multiplied

yash2002vardhan opened this issue · comments

Describe the bug
I have built a cnn architecture using conv1d and activation layers for a regression task at hand. Now when I am trying to print the summary of the architecture it is showing me the following error:
mat1 and mat2 shapes cannot be multiplied (1x1280 and 640x1)

To Reproduce
My architecture is as follows :
class Regression(nn.Module):
def init(self):
super().init()
self.model = nn.Sequential(
nn.Conv1d(1, 32, 2).float(),
nn.ReLU(),
nn.Conv1d(32, 64, 2).float(),
nn.ReLU(),
nn.Conv1d(64, 128, 2).float(),
nn.ReLU(),
nn.Flatten(start_dim = 0),
nn.Linear(128*5, 1)
)

def forward(self, x):
return self.model(x)

Expected behaviour
I should get the summary of the model showing me the output shapes and the number of parameters.

This has nothing to do with torchinfo; the shapes of your convolutions don't work together with the input size. For example, I tried your model on a tensor of shape (8, 1, 201), and it gives a similar error:

>>> from torch import nn
>>> class Regression(nn.Module):
...     def __init__(self):
...         super().__init__()
...         self.model = nn.Sequential(
...             nn.Conv1d(1, 32, 2).float(),
...             nn.ReLU(),
...             nn.Conv1d(32, 64, 2).float(),
...             nn.ReLU(),
...             nn.Conv1d(64, 128, 2).float(),
...             nn.ReLU(),
...             nn.Flatten(start_dim = 0),
...             nn.Linear(128*5, 1)
...         )
...     def forward(self, x):
...         return self.model(x)
... 
>>> r = Regression()
>>> x = torch.randn(8, 1, 201)
>>> r(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/sebastianmuller/anaconda3/envs/neuralsort/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<stdin>", line 15, in forward
  File "/Users/sebastianmuller/anaconda3/envs/neuralsort/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sebastianmuller/anaconda3/envs/neuralsort/lib/python3.11/site-packages/torch/nn/modules/container.py", line 217, in forward
    input = module(input)
            ^^^^^^^^^^^^^
  File "/Users/sebastianmuller/anaconda3/envs/neuralsort/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sebastianmuller/anaconda3/envs/neuralsort/lib/python3.11/site-packages/torch/nn/modules/linear.py", line 114, in forward
    return F.linear(input, self.weight, self.bias)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x202752 and 640x1)

torchinfo simply makes a forward call with your data. In this case, that forward call doesn't seem to work.

@snimu I experienced the same error although my architecture is different

In my case, torchsummary does well the job but when wanted to move to torchinfo (to keep packages updated) I get the [mat1 and mat2 shapes cannot be multiplied] changing nothing except from:

from torchsummary import summary

to

from torchinfo import summary

Can you post a reproducible example here? From the original comment on this thread, it seems to be a problem with the original model or input rather than an issue with torchinfo.