Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mat1 and mat2 shapes cannot be multiplied #269

Open
yash2002vardhan opened this issue Sep 4, 2023 · 3 comments
Open

mat1 and mat2 shapes cannot be multiplied #269

yash2002vardhan opened this issue Sep 4, 2023 · 3 comments

Comments

@yash2002vardhan
Copy link

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.

@snimu
Copy link
Contributor

snimu commented Sep 15, 2023

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.

@vargasa
Copy link

vargasa commented Oct 5, 2023

@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

@TylerYep
Copy link
Owner

TylerYep commented Oct 5, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants