Search code examples
pythonpytorchartificial-intelligenceconv-neural-network

mat1 and mat2 shapes cannot be multiplied


I am new to AI and python, I'm trying to build an architecture to train a set of images. and later to aim to overfit. but up till now, I couldn't understand how to get the inputs and outputs correctly. I keep seeing the error whenever I try to train the network:

mat1 and mat2 shapes cannot be multiplied (48x13456 and 16x64)

my network:

net2 = nn.Sequential(

nn.Conv2d(3,8, kernel_size=5, padding=0),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),


nn.Conv2d(8,16, kernel_size=5, padding=0),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),

nn.Flatten(),
nn.Linear(16,64),
nn.ReLU(),
nn.Linear(64,10)
)

this is a part of a task I'm working on and I really don't get why it's not running. any hints!


Solution

  • its because you have flattened your 2D cnn into 1D FC layers...
    & you have to manually calculate your changed input shape from 128 size to your Maxpool layer just before flattening layer ...In your case its 29*29*16 So your code must be rewritten as

    net2 = nn.Sequential(
    
    nn.Conv2d(3,8, kernel_size=5, padding=0),
    nn.ReLU(),
    nn.MaxPool2d(kernel_size=2, stride=2),
    
    
    nn.Conv2d(8,16, kernel_size=5, padding=0),
    nn.ReLU(),
    nn.MaxPool2d(kernel_size=2, stride=2),
    
    nn.Flatten(),
    
    nn.Linear(13456,64),
    nn.ReLU(),
    nn.Linear(64,10)
    )
    

    This should work

    EDIT: This is a simple formula to calculate output size :

      (((W - K + 2P)/S) + 1)
        Here W = Input size
        K = Filter size
        S = Stride
        P = Padding 
    

    So 1st conv block will make your output of size 124
    Then you do Maxpool which will make it half i.e 62
    2nd conv block will make your output of size 58
    Then your last Maxpool will make it 29...
    So final flattened output would be 29*29*16 where 16 is output channels