Pytorch RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x246016 and 3136x1000)
I'm building a CNN on Pytorch.
I'm a little confused about the inputs.
I'm getting the following error:
RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x246016 and 3136x1000)
Images are 250 x 250 in grayscale.
Can anyone take a look at my constructor and tell me where I'm going wrong?
More points if you can explain to me why I was wrong and the why your answer is right! ;)
class CNN(nn.Module):

# Contructor
def __init__(self):
    super(CNN, self).__init__()
    self.cnn1 = nn.Conv2d(1, 32, kernel_size=5, stride=1, padding=2)
    self.conv1_bn = nn.BatchNorm2d(32)

    self.maxpool1=nn.MaxPool2d(kernel_size=2, stride=2)
    
    self.cnn2 = nn.Conv2d(32, 64, kernel_size=5,stride=1, padding=2)
    self.conv2_bn = nn.BatchNorm2d(64)
    self.maxpool2=nn.MaxPool2d(kernel_size=2, stride=2)  
    
    self.drop_out1 = nn.Dropout()
    self.fc1 = nn.Linear(7 * 7 * 64, 1000)
    self.bn_fc1 = nn.BatchNorm2d(1000)
    
    self.fc2 = nn.Linear(1000, 1)
    

    

# Prediction
def forward(self, x):
    x = self.cnn1(x)
    x = self.conv1_bn(x)
    x = torch.relu(x)
    x = self.maxpool1(x)
    x = self.cnn2(x)
    x = self.conv2_bn(x)
    x = torch.relu(x)
    x = self.maxpool2(x)
    x = x.view(x.size(0), -1)
    x = self.drop_out1(x)
    x = self.fc1(x)
    x = self.bn_fc1(x)
    x = torch.relu(x)
    x = self.fc2(x)
    x = torch.sigmoid(x)
   
    return x


Your fc1 layer is expecting a tensor of shape (-1, 7*7*64) but you are passing it a tensor of shape [-1, 246016] (-1 being the batch size).
To calculate output size of conv nets see this post or any neural networks textbook.
