RuntimeError: mat1 and mat2 shapes cannot be multiplied (4x73034 and 200x120)
Building a Neural Network layers for Skin detection dataset, and got a error here. I know i have done some mistake but cannot figure it out. Error is am getting is after taking image size 224*224 and channels 3: RuntimeError: mat1 and mat2 shapes cannot be multiplied (4x73034 and 200x120)
import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):
  def __init__(self):
    super(Net, self).__init__()
    self.conv1 = nn.Conv2d(3, 16, 5)
    self.pool = nn.MaxPool2d(2, 2)
    self.conv2 = nn.Conv2d(16, 26, 5)
    self.fc1 = nn.Linear(8 * 5 * 5, 120)
    self.fc2 = nn.Linear(120, 86)
    self.fc3 = nn.Linear(86, 2)

  def forward(self, x):
    x = self.pool(F.relu(self.conv1(x)))
    x = self.pool(F.relu(self.conv2(x)))
    x = torch.flatten(x,1)
    x = F.relu(self.fc1(x))
    x = F.relu(self.fc2(x))
    x = self.fc3(x)
    return x

net = Net().to(device)
print(net)

These are the layers and Net module
&lt;ipython-input-41-8c9bafb31c44&gt; in forward(self, x)
     16     x = self.pool(F.relu(self.conv2(x)))
     17     x = torch.flatten(x,1)
---&gt; 18     x = F.relu(self.fc1(x))
     19     x = F.relu(self.fc2(x))
     20     x = self.fc3(x)

Can anyone help me solve this.

As Anant said, you need to match the flattened conv2 dimension (73034) to be the input dimension for the fc1 layer.
self.fc1 = nn.Linear(73034, 120)

The formula to calculate the output of each conv layer:
[(height or width) - kernel size + 2*padding] / stride + 1

For the following I will use the dimensions (Channels, Height, Width)
Input (3,224,224) -&gt; conv1 -&gt; (16,220,220) -&gt; pool -&gt; (16,110,110) -&gt; conv2 -&gt; (26,106,106) -&gt; pool -&gt; (26,53,53) -&gt; flatten -&gt; (73034)
It seems your batch size is 4, which refers to the &quot;4&quot; in (4x73034). If you print the dimensions of the output of conv1 or conv2 layers, the format will be (Batch, Channels, Height, Width).
