Mat1 and mat2 shapes cannot be multiplied (61200x3 and 61200x42)

Hi!

I’m new at PyTorch and getting “mat1 and mat2 shapes cannot be multiplied (61200x3 and 61200x42)” on “x = self.fc(x)”.

My 3000 input tensors had the shape (1, 51, 3) and I want 3000 output tensords each of the shape (1, 42, 3)

import torch.nn as nn

class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=1, out_channels=12, kernel_size=3, stride=1, padding=1)
        self.conv2 = nn.Conv2d(in_channels=12, out_channels=12, kernel_size=3, stride=1, padding=1)
        self.conv3 = nn.Conv2d(in_channels=12, out_channels=24, kernel_size=3, stride=1, padding=1)
        self.drop = nn.Dropout2d(p=0.2)
        self.fc = nn.Linear(61200, 42)

    def forward(self, x):
        print(x.shape)
        x = F.relu(self.conv1(x))
        print(x.shape)
        x = F.relu(self.conv2(x))
        print(x.shape)
        x = F.relu(self.drop(self.conv3(x)))
        print(x.shape)
        x = F.dropout(x, training=self.training)
        print(x.shape)
        x = self.fc(x)
        return F.softplus(x, dim=2)

The layers shapes change like that
torch.Size([50, 1, 51, 3])
torch.Size([50, 12, 51, 3])
torch.Size([50, 12, 51, 3])
torch.Size([50, 24, 51, 3])
torch.Size([50, 24, 51, 3])
before the “x = self.fc(x)”

Will be thankfull for help

It seems you are forgetting to flatten the activation via: x = x.view(x.size(0), -1) or an nn.Flatten layer before passing it to the self.fc layer.
Once this is done you will run into another shape mismatch error since the activation has 24 * 51 * 3 = 3672 features, so set the in_features argument of self.fc to 3672.

1 Like

This works, thank you!