RuntimeError: mat1 and mat2 shapes cannot be multiplied (128x24576 and 16384x7)

Lohith_Kumaar: <p>I tried running this code and unable to complete it because of the runtime error can anyone suggest a way out for this.</p>
<pre><code class="lang-auto">class Net(nn.Module):
    def __init__(self, num_classes=7):
        super(Net,self).__init__()
        
        
        #Input shape = (64,3,256,256)
        
        self.conv1=nn.Conv2d(in_channels=3,out_channels=12,kernel_size=3,stride=1,padding=1)
        #output = (64,12,256,256)
        self.bn1=nn.BatchNorm2d(num_features=12)
        #output = (64,12,256,256)
        self.relu1=nn.ReLU()
        self.pool=nn.MaxPool2d(kernel_size=2)
        #Reduse the image by a factor 2
        #output = (64,12,128,128)
        
        
        self.conv2=nn.Conv2d(in_channels=12,out_channels=24,kernel_size=3,stride=1,padding=1)
        #output = (64,24,128,128)
        self.bn2=nn.BatchNorm2d(num_features=24)
        #output = (64,12,128,128)
        self.relu2=nn.ReLU()
        #output = (64,12,128,128)
        self.pool=nn.MaxPool2d(kernel_size=2)
        #Reduse the image by a factor 2
        #output = (64,12,64,64)
        
        
        self.conv3=nn.Conv2d(in_channels=24,out_channels=48,kernel_size=3,stride=1,padding=1)
        #output = (64,48,128,128)
        self.bn3=nn.BatchNorm2d(num_features=48)
        #output = (64,48,128,128)
        self.relu3=nn.ReLU()
        #output = (64,12,128,128)
        self.pool=nn.MaxPool2d(kernel_size=2)
        #Reduse the image by a factor 2
        #output = (64,48,32,32)
        
        
        self.conv4=nn.Conv2d(in_channels=48,out_channels=96,kernel_size=3,stride=1,padding=1)
        #output = (64,96,32,32)
        self.bn4=nn.BatchNorm2d(num_features=96)
        #output = (64,96,32,32)
        self.relu4=nn.ReLU()
        #output = (64,96,32,32)
        self.pool=nn.MaxPool2d(kernel_size=2)
        #Reduse the image by a factor 2
        #output = (64,96,16,16)
    
    
        self.fc=nn.Linear(in_features=64*16*16,out_features=num_classes)
        
        
    def forward(self, input):
            output=self.conv1(input)
            output=self.bn1(output)
            output=self.relu1(output)
            
            output=self.pool(output)
            
            output=self.conv2(output)
            output=self.bn2(output)
            output=self.relu2(output)
            
            output=self.conv3(output)
            output=self.bn3(output)
            output=self.relu3(output)
            
            
            output=self.pool(output)
            
            
            output=self.conv4(output)
            output=self.bn4(output)
            output=self.relu4(output)
            
            #Above ouput will be in the matrix form, with shape (64,96,16,16)
            
            output=output.view(-1, 96*16*16)
            
            output=self.fc(output)    
            
            return output
        
model = Net(num_classes=7).to(device)

#Optimizer and Loss Function

optimizer = optim.SGD(model.parameters(),lr=0.001, momentum=0.9)
loss_function=nn.CrossEntropyLoss()

num_epochs=5


best_accuracy=0.0

for epoch in range(num_epochs):
    
    #Evaluation and training on training dataset
    model.train()
    train_accuracy=0.0
    train_loss=0.0
    
    for i, (images,labels) in enumerate(train_loader):
        if torch.cuda.is_available():
            images=Variable(images.cuda())
            labels=Variable(labels.cuda())
            
        optimizer.zero_grad()
        
        outputs=model(images)
        loss=loss_function(outputs,labels)
        loss.backward()
        optimizer.step()
        
        
        train_loss+= loss.cpu().data*images.size(0)
        _,prediction=torch.max(outputs.data,1)
        
        train_accuracy+=int(torch.sum(prediction==labels.data))
        
    train_accuracy=train_accuracy/train_count
    train_loss=train_loss/train_count
    
    
    # Evaluation on testing dataset
    model.eval()
    
    test_accuracy=0.0
    for i, (images,labels) in enumerate(test_loader):
        if torch.cuda.is_available():
            images=Variable(images.cuda())
            labels=Variable(labels.cuda())
            
        outputs=model(images)
        _,prediction=torch.max(outputs.data,1)
        test_accuracy+=int(torch.sum(prediction==labels.data))
    
    test_accuracy=test_accuracy/test_count
    
    
    print('Epoch: '+str(epoch)+' Train Loss: '+str(train_loss)+' Train Accuracy: '+str(train_accuracy)+' Test Accuracy: '+str(test_accuracy))
    
    #Save the best model
    if test_accuracy&gt;best_accuracy:
        torch.save(model.state_dict(),'best_checkpoint.model')
        best_accuracy=test_accuracy
</code></pre>
<p>I got the Error</p>
<blockquote>
<hr>
</blockquote>
<p>RuntimeError                              Traceback (most recent call last)<br>
Input In [26], in &lt;cell line: 3&gt;()<br>
13     labels=Variable(labels.cuda())<br>
15 optimizer.zero_grad()<br>
—&gt; 17 outputs=model(images)<br>
18 loss=loss_function(outputs,labels)<br>
19 loss.backward()</p>
<p>File ~\anaconda3\envs\ownmodel\lib\site-packages\torch\nn\modules\module.py:1110, in Module._call_impl(self, *input, **kwargs)<br>
1106 # If we don’t have any hooks, we want to skip the rest of the logic in<br>
1107 # this function, and just call forward.<br>
1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks<br>
1109         or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1110     return forward_call(*input, **kwargs)<br>
1111 # Do not call functions when jit is used<br>
1112 full_backward_hooks, non_full_backward_hooks = <span class="chcklst-box fa fa-square-o fa-fw"></span>, <span class="chcklst-box fa fa-square-o fa-fw"></span></p>
<p>Input In [24], in Net.forward(self, input)<br>
77 <span class="hashtag-raw">#Above</span> ouput will be in the matrix form, with shape (64,96,16,16)<br>
79 output=output.view(-1, 96<em>16</em>16)<br>
—&gt; 81 output=self.fc(output)<br>
83 return output</p>
<p>File ~\anaconda3\envs\ownmodel\lib\site-packages\torch\nn\modules\module.py:1110, in Module._call_impl(self, *input, **kwargs)<br>
1106 # If we don’t have any hooks, we want to skip the rest of the logic in<br>
1107 # this function, and just call forward.<br>
1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks<br>
1109         or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1110     return forward_call(*input, **kwargs)<br>
1111 # Do not call functions when jit is used<br>
1112 full_backward_hooks, non_full_backward_hooks = <span class="chcklst-box fa fa-square-o fa-fw"></span>, <span class="chcklst-box fa fa-square-o fa-fw"></span></p>
<p>File ~\anaconda3\envs\ownmodel\lib\site-packages\torch\nn\modules\linear.py:103, in Linear.forward(self, input)<br>
102 def forward(self, input: Tensor) → Tensor:<br>
 → 103     return F.linear(input, self.weight, self.bias)</p>
<p>RuntimeError: mat1 and mat2 shapes cannot be multiplied (128x24576 and 16384x7)</p>

tom: <p>You declare fc with <code>64 * 16 * 16</code> = 16384 and use it with <code>96 * 16 * 16</code> = 24576. If you fix that, it should work.</p>
<p>Best regards</p>
<p>Thomas</p>

Lohith_Kumaar: <p>Thank you man<br>
I will try it</p>