RuntimeError: mat1 and mat2 shapes cannot be multiplied (4x21632 and 1152x120)

I ammfacing this error from a long time, anyone can tell where is the problem attached the code below
class NetModel(nn.Module):

def __init__(self):

    super().__init__()

    self.conv_1 = nn.Conv2d(3, 32, 3, padding=1)

    self.conv_2 = nn.Conv2d(32, 64, 3)

    self.maxpool_1 = nn.MaxPool2d(2,2)

    self.conv_3 = nn.Conv2d(64, 128, 3)

    self.bn = nn.BatchNorm2d(128)

    self.fc1 = nn.Linear(128*3*3, 120)

    self.fc2 = nn.Linear(120, 84)

    self.fc3 = nn.Linear(84, 10)

def forward(self, x):

    x = F.relu(self.conv_1(x))

    x = F.relu(self.conv_2(x))

    x = self.maxpool_1(x)

    x = F.relu(self.conv_3(x))

    x = self.bn(x)

    x = torch.flatten(x, 1)

    x = F.relu(self.fc1(x))

    x = F.relu(self.fc2(x))

    x = self.fc3(x)

    return x

Based on the error message, I guess the activation input to self.fc1 has 21632 features, while self.fc1 is using in_features=1152.
Use:

self.fc1 = nn.Linear(21632, 120)

and it should work.

Did you calculate dimensions of your convnet? Because it’s probably wrong.

General formula with input vector dimensions = HxWxC is floor((W−K+2*P) / S) + 1
P - padding
S- stride
K- kernel size
If W <> H then calculations need to be done for both dimensions: Height and Width.

Plus there is 2x2 maxpool which will reduce the size of each dimension twice.

What is the dimension of your input?

The dimension of the input image os 32x32x3

What you need to do is to flatten your data after the last convolutional layer - look at the script below - and also correct the dimension of your first linear layer

import torch
from torch.nn import Conv2d, MaxPool2d, BatchNorm2d, Linear

input = torch.rand((1, 3, 32, 32)) # Batch size, number of channels, Heigth, Width

conv_1 = Conv2d(3, 32, 3, padding=1)
t1 = conv_1(input)
t1.size()
>>> torch.Size([1, 32, 32, 32])

conv_2 = Conv2d(32, 64, 3)
t2 = conv_2(t1)
t2.size()
>>> torch.Size([1, 64, 30, 30])

maxpool_1 = MaxPool2d(2,2)
t3 = maxpool_1(t2)
t3.size()
>>> torch.Size([1, 64, 15, 15])

conv_3 = Conv2d(64, 128, 3)
t4 = conv_3(t3)
t4.size()
>>> torch.Size([1, 128, 13, 13])

bn = BatchNorm2d(128) # doesn't change the size
t5 = bn(t4)
t5.size()
>>> torch.Size([1, 128, 13, 13])

t6 = t5.reshape(1, 128*13*13) # flat the output for the input to nn
t6.size()
>>> torch.Size([1, 21632])

fc1 = Linear(128*13*13, 120)
t7 = fc1(t6)
# Looks fine :-)