RuntimeError: mat1 dim 1 must match mat2 dim 0

the input size of image is [3, 224, 224] and batch size is 64. It keeps showing this error:

mat1 dim 1 must match mat2 dim 0

class SimpleBNConv(nn.Module):

    # This constructor will initialize the model architecture
    def __init__(self):
        super(SimpleBNConv, self).__init__()

        self.cnn_layers = nn.Sequential(
            # Defining a 2D convolution layer
            nn.Conv2d(3, 8, kernel_size=3, stride=1, padding=1),
            # Putting a 2D Batchnorm after CNN layer
            nn.BatchNorm2d(8),
            # Adding Relu Activation
            nn.ReLU(inplace=True),
            nn.Conv2d(8, 8, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(8),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2, stride=2),

            nn.Conv2d(8, 16, kernel_size=3, stride=1, padding=1),
            # Putting a 2D Batchnorm after CNN layer
            nn.BatchNorm2d(16),
            # Adding Relu Activation
            nn.ReLU(inplace=True),
            nn.Conv2d(16, 16, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(16),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2, stride=2),

            nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1),
            # Putting a 2D Batchnorm after CNN layer
            nn.BatchNorm2d(32),
            # Adding Relu Activation
            nn.ReLU(inplace=True),
            nn.Conv2d(32, 32, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(32),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2, stride=2),

            nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
            # Putting a 2D Batchnorm after CNN layer
            nn.BatchNorm2d(64),
            # Adding Relu Activation
            nn.ReLU(inplace=True),
            nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(64),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2, stride=2),

            nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
            # Putting a 2D Batchnorm after CNN layer
            nn.BatchNorm2d(128),
            # Adding Relu Activation
            nn.ReLU(inplace=True),
            nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(128),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2, stride=2),

            nn.Flatten(),
            nn.Linear(10976, 6272),
            nn.ReLU(inplace=True),
            nn.Linear(6272, 7)
        )

    # Defining the forward pass

    def forward(self, x):

        # Forward Pass through the CNN Layers
        x = self.cnn_layers(x)
        return nn.functional.softmax(x)

I guess the activation shape doesn’t match the expected in_features in the linear layer, so you would have to check the real shape and adapt the nn.Linear module.
To do so, you could either add a custom Print module between the nn.Flatten and nn.Linear layers and print the shape of the activation or alternatively remove the last three layers:

            nn.Linear(10976, 6272),
            nn.ReLU(inplace=True),
            nn.Linear(6272, 7)

and print the output shape in the forward.

1 Like

Thank you ptrblck. I solved it!
:grinning:

how can, pls tell me :smile:

Hi all,
Interestingly, I am having the same issue and can’t solve it based on the solutions posted here.
Your prompt help is very much appreciated.

class LeNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 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) # flatten all dimensions except batch
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

This is the error :

  File "_MCADNNet_debug.py", line 292, in <module>
    main()
  File "_MCADNNet_debug.py", line 239, in main
    output = model(data)
  File "/home/ec2-user/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/ec2-user/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/torch/nn/parallel/data_parallel.py", line 159, in forward
    return self.module(*inputs[0], **kwargs[0])
  File "/home/ec2-user/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "_MCADNNet_debug.py", line 52, in forward
    x = F.relu(self.fc1(x))
  File "/home/ec2-user/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/ec2-user/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/torch/nn/modules/linear.py", line 93, in forward
    return F.linear(input, self.weight, self.bias)
  File "/home/ec2-user/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/torch/nn/functional.py", line 1690, in linear
    ret = torch.addmm(bias, input, weight.t())
RuntimeError: mat1 dim 1 must match mat2 dim 0

And this is my input tensor (data, label) in the training loop :
torch.Size([64, 3, 224, 224]) torch.Size([64])

The flattened activation has a shape of [batch_size, 44944], so you need to change the in_features of self.fc1 to self.fc1 = nn.Linear(16 * 53 * 53, 120) and it’ll work.

1 Like

Thank you Patrick - it worked. Do you mind if you tell me how to get that 44944? I might want to change some layers so try to understand how to calculate the new number. Thanks again !

You can print the shape of the flattened activation directly in the forward method and adapt the in_features argument afterwards:

    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) # flatten all dimensions except batch
        print(x.shape)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x
1 Like