RuntimeError: mat1 and mat2 shapes cannot be multiplied (64x3072 and 1024x512)

Viewed 34

I'm trying to create a Pytorch Neural Network and keep getting this error

RuntimeError: mat1 and mat2 shapes cannot be multiplied (64x3072 and 1024x512)

Here is my code where I create the model:

# Define model
class NeuralNetwork(nn.Module):
    def __init__(self):
        super(NeuralNetwork, self).__init__()
        self.flatten = nn.Flatten()
        self.linear_relu_stack = nn.Sequential(
            nn.Linear(32*32, 512),
            nn.ReLU(),
            nn.Linear(512, 512),
            nn.ReLU(),
            nn.Linear(512, 10)
        )

    def forward(self, x):
        x = self.flatten(x)
        logits = self.linear_relu_stack(x)
        return logits

model = NeuralNetwork()
print(model)

An answer would be much appreciated

1 Answers

There can be two problems here:

  1. Either the tensor is not being flattened which is possible in cases such as 64x1x3076 sized tensor.

  2. The problem might be in code unrelated to your model but in the training loop of the code. Please add that part of the code as well.

Related