Randomly getting weird output size in fc layer causing runtime size mismatch error

I have a simple CNN with one conv layer with relu and maxpool, followed by a linear layer to flatten. In training, after a few samples I get the error:

ret = torch.addmm(torch.jit._unwrap_optional(bias), input, weight.t())
RuntimeError: size mismatch, m1: [1 x 2380], m2: [2550 x 2] at c:\a\w\1\s\windows\pytorch\aten\src\th\generic/THTensorMath.cpp:940

I printed the outputs out from my model to check and it seems after a few samples gone through the model, I get a strange output size (2380 instead of the others 2550), as below:

x after layer 1:  torch.Size([1, 10, 15, 17])
x before linear layer:  torch.Size([1, 2550])

x after layer 1:  torch.Size([1, 10, 15, 17])
x before linear layer:  torch.Size([1, 2550])

x after layer 1:  torch.Size([1, 10, 14, 17])
x before linear layer:  torch.Size([1, 2380])
Traceback (most recent call last):
  File "C:\Users\USER\Desktop\ModelStuff\cnn_for_ppbp\src\my_experiments.py", line 87, in <module>
    outputs = model(images)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\module.py", line 489, in __call__
    result = self.forward(*input, **kwargs)
  File "C:\Users\USER\Desktop\ModelStuff\cnn_for_ppbp\src\my_models.py", line 25, in forward
    x = self.fc(x)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\module.py", line 489, in __call__
    result = self.forward(*input, **kwargs)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\linear.py", line 67, in forward
    return F.linear(input, self.weight, self.bias)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\functional.py", line 1352, in linear
    ret = torch.addmm(torch.jit._unwrap_optional(bias), input, weight.t())
RuntimeError: size mismatch, m1: [1 x 2380], m2: [2550 x 2] at c:\a\w\1\s\windows\pytorch\aten\src\th\generic/THTensorMath.cpp:940

Can you please tell me why this is so? And how to fix it? Something to do with pooling or strides? I don’t know :disappointed:

My model is here:

class ShallowCNN(nn.Module):
    
    def __init__(self):
        super(ShallowCNN, self).__init__()
        self.layer1 = nn.Sequential(nn.Conv2d(1, 10, 5, 1).double(),
                                   nn.ReLU(),
                                   nn.MaxPool2d(2, 2))
        self.fc = nn.Linear(2550, 2)
        
    def forward(self, x):
        x = self.layer1(x)
        print('\nx after layer 1: ', x.shape)
        x = x.reshape(x.size(0), -1).float()
        print('x before linear layer: ', x.shape)
        x = self.fc(x)
        return x

Silly me. Some of my images were of the wrong size (33x38 instead of 35*38) giving the strange outputs. I will have to fix my data. Sorry everybody.

If you don’t want to resize or “fix” your input data in some way, you could alternatively use an adaptive pooling layer so that its output size will always be the specified one.
This would make sure that your model works with different input shapes.

1 Like

I
Dear all, I have a simple highway CNN with three highway 1d conv layers, my input size is 22 dimensions, it views as x= x.view(batch, 1, seq_lng) before input for the first conv layer, where, batch szie is 128, seq_length is 142. To make the input szie equal to the output size, i used the padding with 1, num_filters or channel 10, filter_size is 3: The shape of tensors look like as:
128,1,242 for input x
128,10,242 after the first highway conv layer
128,10,242 after the second highway conv layer
128,10,242 after the third highway conv layer
Then i make to flatten output of the last highway conv layer to give as an input for the FCL as:
x = x.view(-1, channel x width)
size is 128 x 2420 as an input dimension for FCL and 1024 output dimension
However, when i run, the dimension mismatch occurred as follows:

File "/root/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py", line 1352, in linear
    ret = torch.addmm(torch.jit._unwrap_optional(bias), input, weight.t())
RuntimeError: size mismatch, m1: [128 x 2420], m2: [370 x 1024] at /opt/conda/conda-bld/pytorch_1549628766161/work/aten/src/THC/generic/THCTensorMathBlas.cu:266

Can you please tell me why this is happened? And How can i fix it?

It looks like the in_features of your linear layer are set to 1024.
Could this be the case?
If so, you should define them as in_features=2420, since that’s the number of features from your conv layers.

Thank you very much.
I tried it but it also gives the same RuntimeError:

File "/root/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py", line 1352, in linear
    ret = torch.addmm(torch.jit._unwrap_optional(bias), input, weight.t())
RuntimeError: size mismatch, m1: [128 x 2420], m2: [370 x 2420] at /opt/conda/conda-bld/pytorch_1549628766161/work/aten/src/THC/generic/THCTensorMathBlas.cu:266

Are you sure you didn’t mix up in_features and out_features?
This code works fine:

x = torch.randn(128, 2420)
lin = nn.Linear(in_features=2420, out_features=370)
output = lin(x)
print(output.shape)

yes, i set jut like this.

Is my code snippet working on your machine?
If I swap the in and out features, I’ll get the same error message you’ve posted.

I have tried your code also but it comes up with the same error. I do not know what happed?

If you run my code snippet alone in a new script, you’ll get the shape mismatch error?
Which PyTorch version are you using? This seems like a bug and I would like to debug it.

It is not working for me.
I used pytorch version 1.0.1.post2

Could you post the exact error message?
What happens if you swap the features:

x = torch.randn(128, 2420)
lin = nn.Linear(in_features=370, out_features=2420)
output = lin(x)

Which error message is thrown then?