RuntimeError: mat1 dim 1 must match mat2 dim 0 for CNN

Hi, I’m getting the runtime error.

import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
from torch.nn import *


class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()


        self.conv1 = nn.Sequential(
            nn.Conv1d(1, 128, kernel_size=3, stride=3, padding=1),
            nn.BatchNorm1d(128),
            nn.ReLU())

        self.conv2= nn.Sequential(
        nn.Conv1d(128,128, kernel_size= 3, stride=3, padding=1),
        nn.BatchNorm1d(128),
        nn.MaxPool1d(3, stride=3))
        
        self.fc = nn.Linear(512,10)
        self.activation = nn.ReLU()
        
        
    def forward(self,x):
        print('inside forward')
        print("x1.",x.shape)
        x = x.view(x.shape[0], 1,-1)
        print("x2.",x.shape)
        out = self.conv1(x)
        print("out1.",out.shape)
        out = self.conv2(out)
        print("out2.",out.shape)

        out = out.view(x.shape[0], -1)
        logit = self.fc(out)
    

model = Net()
x1. torch.Size([107841, 1, 131])
x2. torch.Size([107841, 1, 131])
out1. torch.Size([107841, 128, 44])
out2. torch.Size([107841, 128, 5])
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-10-73c225dfd454> in <module>
     46 # training the model
     47 for epoch in range(n_epochs):
---> 48     train(epoch)
     49 
     50 plt.plot(train_losses, label='Training loss')

<ipython-input-10-73c225dfd454> in train(epoch)
     19 
     20     # prediction for training and validation set
---> 21     output_train = model(x_train)
     22     output_val = model(x_val)
     23 

/usr/local/anaconda3/latest/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

<ipython-input-5-55e15a8cc91f> in forward(self, x)
     36 
     37         out = out.view(x.shape[0], -1)
---> 38         logit = self.fc(out)
     39 
     40 

/usr/local/anaconda3/latest/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

/usr/local/anaconda3/latest/lib/python3.8/site-packages/torch/nn/modules/linear.py in forward(self, input)
     91 
     92     def forward(self, input: Tensor) -> Tensor:
---> 93         return F.linear(input, self.weight, self.bias)
     94 
     95     def extra_repr(self) -> str:

/usr/local/anaconda3/latest/lib/python3.8/site-packages/torch/nn/functional.py in linear(input, weight, bias)
   1688     if input.dim() == 2 and bias is not None:
   1689         # fused op is marginally faster
-> 1690         ret = torch.addmm(bias, input, weight.t())
   1691     else:
   1692         output = input.matmul(weight.t())

RuntimeError: mat1 dim 1 must match mat2 dim 0

The error is raised since the number of features in out doesn’t match the in_features in self.fc.
Add a print statement as print(out.shape) after the view operation and before passing it to self.fc.
This would give you the expected number of features for self.fc, which you could then fix.

Also, your forward method doesn’t return the logit, so you might want to add the return statement.