RuntimeError: mat1 dim 1 must match mat2 dim 0 CNN

Hello, i am getting the error when i am trying to train a CNN model using custom dataset.
here is my model:

class CNNMnist(nn.Module):
    def __init__(self, args):
        super(CNNMnist, self).__init__()
        self.conv1 = nn.Conv2d(args.num_channels, 10, kernel_size=5)
        self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
        self.conv2_drop = nn.Dropout2d()
        self.fc1 = nn.Linear(720, 50)
        self.fc2 = nn.Linear(128, args.num_classes)

    def forward(self, x):
        x = F.relu(F.max_pool2d(self.conv1(x), 2))
        x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
        x = x.view(-1, x.shape[1]*x.shape[2]*x.shape[3])
        x = F.relu(self.fc1(x))
        x = F.dropout(x,

        x = self.fc2(x)
        return F.log_softmax(x, dim=1)

i reshape my images while loading with transforms.Resize(256),transforms.CenterCrop(224)
i initialize my model:

global_model = CNNMnist(args=args)

for my custom dataset, the number of class is 6 and the number of channel is 3.
The error is thrown at x = F.relu(self.fc1(x)).
After spending hours trying to find out the problem, my guess is the values used in the following lines are not correct: self.fc1 = nn.Linear(720, 50) self.fc2 = nn.Linear(128, args.num_classes)
Since i actually have no idea how these values are calculated, I would really appreciate some help on this.
Thank you

Flatten should remove all but the batch dimensions of your model and produce a (N, D) tensor. D should match the number of input dimensions to the first layer (if it is not 720, this value should be changed). Also, the output of fc1 will have 50 dimensions, so the input dimensions of fc2 should be changed to 50 (or the output of fc1 to 128).

Thanks for the reply. Are you suggesting that i should use flatten? And how do i calculate D?

It looks like you are already printing the shape so you should be able to see what N, and D are here.
Flatten can work, but rather than reshaping to (-1, something), you should reshape to (batch_size, -1).

thanks. changing the value of D did the trick. one small question, how important it is to reshape(batch_size, -1) than reshape (-1, something). How does it affect my training?

It is very important as otherwise you are mixing different examples that are in the same batch together. However, if you set the reshape to (-1, [flat_size]/batch_size) then the two are equivalent.