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
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