Skip to content Skip to sidebar Skip to footer

Runtimeerror: Mat1 And Mat2 Shapes Cannot Be Multiplied (5376x28 And 784x512)

Basic Network class Baseline(nn.Module): def __init__(self): super().__init__() # 5 Hidden Layer Network self.fc1 = nn.Linear(28 * 28, 512) self

Solution 1:

I see one issue in the code. Linear layers do not accept matrices with a 4d shape that you passed into the model.

In order to pass data with torch.Size([64, 3, 28, 28]) through a nn.Linear() layers like you have in your model. You need to flatten the tensor in your forward function like:

# New code
x = x.view(x.size(0), -1)
#Your code
x = self.dropout(F.relu(self.fc1(x)))

...

This will probably help solve the weight matrix error you are getting.

Sarthak Jain

Solution 2:

I had to adjust the in_features and also flatten the input in the forward function

in_features = 3*28*28classBaseline(nn.Module):
    def__init__(self):
        super().__init__()
        # 5 Hidden Layer Network
        self.fc1 = nn.Linear(input_features, 512)
        self.fc2 = nn.Linear(512, 256)
        self.fc3 = nn.Linear(256, 128)
        self.fc4 = nn.Linear(128, 64)
        self.fc5 = nn.Linear(64, 3)

        # Dropout module with 0.2 probbability
        self.dropout = nn.Dropout(p=0.2)
        # Add softmax on output layer
        self.log_softmax = F.log_softmax

    defforward(self, x):
        x = x.view(x.size(0), -1)
        x = self.dropout(F.relu(self.fc1(x)))
        x = self.dropout(F.relu(self.fc2(x)))
        x = self.dropout(F.relu(self.fc3(x)))
        x = self.dropout(F.relu(self.fc4(x)))

        x = self.log_softmax(self.fc5(x), dim=1)

        return x

Post a Comment for "Runtimeerror: Mat1 And Mat2 Shapes Cannot Be Multiplied (5376x28 And 784x512)"