RuntimeError: mat1 dim 1 must match mat2 dim 0 3DCNN

Iused pytorch to create 3DCNN
My code works with 2 layers of conv. But does not work with 3 layers of conv.
input image size:120120120
Result: RuntimeError: mat1 dim 1 must match mat2 dim 0
I think the problem is with this line out = self.fc1(out)
can you help me please ?
thank you in advance!

class CNNModel(nn.Module):
    def __init__(self):
        super(CNNModel, self).__init__() # héritage
        
        self.conv_layer1 = self._conv_layer_set(3, 32) #The first dimension of Pytorch Convolution should always be
                                                        #the the number of channels (3)
        self.conv_layer2 = self._conv_layer_set(32, 64) 
        self.conv_layer3 = self._conv_layer_set(64, 128)
        self.fc1 = nn.Linear(128*28*28*28, 2) # couche linéaire entièrement connectée,
        self.fc2 = nn.Linear(2809856, num_classes) ###### couche linéaire entièrement connectée 
        self.relu = nn.LeakyReLU()
        self.batch=nn.BatchNorm1d(2)
        self.drop=nn.Dropout(p=0.15, inplace = True)   
        
    def _conv_layer_set(self, in_c, out_c):
        conv_layer = nn.Sequential(
        nn.Conv3d(in_c, out_c, kernel_size=(3, 3, 3), padding=0),
        nn.LeakyReLU(),
        nn.MaxPool3d((2, 2, 2)),
        )
        return conv_layer
    

    def forward(self, x):
        # Set 1
        out = self.conv_layer1(x)
        out = self.conv_layer2(out)
        out = self.conv_layer3(out)
        #out = out.view(out.size(0), -1) #regler l'output pour devenir l'input de Flly conncted layer 
        out = out.view(out.size(0), -1)
        out = self.fc1(out)
        out = self.relu(out)
        out = self.batch(out) # batchnormalization pour 
        out = self.drop(out)
        out = F.softmax(out, dim=1) # softmax dim = 1 puisque 1 sig. batch size  )
        return out

Could you add a print statement before the usage of fc1 and check the shape of the activation tensor?

print(out.shape)
out = self.fc1(out)

I guess that out.shape doesn’t match the expected input features as 128*28*28*28 so that you might have to adjust this value in self.fc1.

Thank you so much @ptrblck