RuntimeError: size mismatch, m1: [32 x 5184], m2: [400 x 120] at /opt/conda/conda-bld/pytorch_1570910687230/work/aten/src/THC/generic/THCTensorMathBlas.cu:290

I don’t know whats wrong with my LeNet,I can not understand why it could be wrong

class LeNet(nn.Module):
    def __init__(self):
        super(LeNet,self).__init__()
        self.conv1=nn.Conv2d(3,6,5)
        self.conv2=nn.Conv2d(6,16,5)
        self.fc1=nn.Linear(16*5*5,120)
        self.fc2=nn.Linear(120,84)
        self.fc3=nn.Linear(84,48)
    def forward(self,x):
        out = F.relu(self.conv1(x))
        out = F.max_pool2d(out, 2)
        out = F.relu(self.conv2(out))
        out = F.max_pool2d(out, 2)
        out = out.view(out.size(0), -1)
        out = F.relu(self.fc1(out))
        out = F.relu(self.fc2(out))
        out = self.fc3(out)
        return x
Traceback (most recent call last):
  File "/home/xiu/Desktop/mediacompute/ImageRecognition/main1.py", line 97, in <module>
    main(config)
  File "/home/xiu/Desktop/mediacompute/ImageRecognition/main1.py", line 48, in main
    solver.train()
  File "/home/xiu/Desktop/mediacompute/ImageRecognition/solver.py", line 135, in train
    outputs =self.model(inputs)
  File "/home/xiu/anaconda3/envs/torch1.0/lib/python3.6/site-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/xiu/Desktop/mediacompute/ImageRecognition/Lenet.py", line 17, in forward
    out = F.relu(self.fc1(out))
  File "/home/xiu/anaconda3/envs/torch1.0/lib/python3.6/site-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/xiu/anaconda3/envs/torch1.0/lib/python3.6/site-packages/torch/nn/modules/linear.py", line 87, in forward
    return F.linear(input, self.weight, self.bias)
  File "/home/xiu/anaconda3/envs/torch1.0/lib/python3.6/site-packages/torch/nn/functional.py", line 1370, in linear
    ret = torch.addmm(bias, input, weight.t())
RuntimeError: size mismatch, m1: [32 x 5184], m2: [400 x 120] at /opt/conda/conda-bld/pytorch_1570910687230/work/aten/src/THC/generic/THCTensorMathBlas.cu:290

It seems the flattened output of self.conv2 and F.max_pool seems to be [batch_size, 5184], while self.fc1 expects an input of [batch_size, 400].
Which input size are you using?

1 Like

Hy sir am getting same type of error and the error is

i cannot be able to understand this that why it is occuring?
Or either i changed the num of workers and batch size is it due to that one? kindly guide me how to solve it.

And the relevent part of the code is

Could you post your model definition as well as the input shapes?
Please post the code by wrapping it into three backticks ``` instead of pictures, as this is easier to debug.

1 Like

hy sir actually i gave three features in my original model but am trying to update it in one feature but its giving this error wich i posted kindly guide me that how can i post model and training file so that you can guide me?

Moreover sir i cant be able to upload the model because here one can just upload the jpg kindly guide me how can i upload my model and the new one so that you may help me to debug it