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

Sushant_Patankar: <p>Hi, I’m getting the runtime error.</p>
<pre><code class="lang-auto">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()
</code></pre>
<pre><code class="lang-auto">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)
&lt;ipython-input-10-73c225dfd454&gt; in &lt;module&gt;
     46 # training the model
     47 for epoch in range(n_epochs):
---&gt; 48     train(epoch)
     49 
     50 plt.plot(train_losses, label='Training loss')

&lt;ipython-input-10-73c225dfd454&gt; in train(epoch)
     19 
     20     # prediction for training and validation set
---&gt; 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:
--&gt; 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

&lt;ipython-input-5-55e15a8cc91f&gt; in forward(self, x)
     36 
     37         out = out.view(x.shape[0], -1)
---&gt; 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:
--&gt; 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) -&gt; Tensor:
---&gt; 93         return F.linear(input, self.weight, self.bias)
     94 
     95     def extra_repr(self) -&gt; 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
-&gt; 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

</code></pre>

ptrblck: <p>The error is raised since the number of features in <code>out</code> doesn’t match the <code>in_features</code> in <code>self.fc</code>.<br>
Add a print statement as <code>print(out.shape)</code> after the <code>view</code> operation and before passing it to <code>self.fc</code>.<br>
This would give you the expected number of features for <code>self.fc</code>, which you could then fix.</p>
<p>Also, your <code>forward</code> method doesn’t return the <code>logit</code>, so you might want to add the <code>return</code> statement.</p>