Pytorch vsion size mismatch, m1
im trying to run a simple linear regression but i have error when i try to train.

The size of images is the shapes of data train print(dataset_train[0][0].shape) shows me torch.Size([3, 227, 227])


size_of_image=3*227*227

class linearRegression(nn.Module):
    def __init__(self, inputSize, outputSize):
        super(linearRegression, self).__init__()
        self.linear = nn.Linear(inputSize, outputSize)

    def forward(self, x):
        out = self.linear(x)
        return out

model = linearRegression(size_of_image, 1)
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
criterion = torch.nn.CrossEntropyLoss()         
trainloader = DataLoader(dataset = dataset_train, batch_size = 1000)
for epoch in range(5):
    for x, y in trainloader:
        yhat = model(x)
        loss = criterion(yhat, y)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()            


I tried to unserstand what its the mean of error but i dont found a solution, can anyone help me?

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-44-6f00f9272a22&gt; in &lt;module&gt;
      1 for epoch in range(5):
      2     for x, y in trainloader:
----&gt; 3         yhat = model(x)
      4         loss = criterion(yhat, y)
      5         optimizer.zero_grad()

~/PycharmProjects/estudios/venv/lib/python3.8/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    548             result = self._slow_forward(*input, **kwargs)
    549         else:
--&gt; 550             result = self.forward(*input, **kwargs)
    551         for hook in self._forward_hooks.values():
    552             hook_result = hook(self, input, result)

&lt;ipython-input-21-d20eb6e0c349&gt; in forward(self, x)
      5 
      6     def forward(self, x):
----&gt; 7         out = self.linear(x)
      8         return out

~/PycharmProjects/estudios/venv/lib/python3.8/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    548             result = self._slow_forward(*input, **kwargs)
    549         else:
--&gt; 550             result = self.forward(*input, **kwargs)
    551         for hook in self._forward_hooks.values():
    552             hook_result = hook(self, input, result)

~/PycharmProjects/estudios/venv/lib/python3.8/site-packages/torch/nn/modules/linear.py in forward(self, input)
     85 
     86     def forward(self, input):
---&gt; 87         return F.linear(input, self.weight, self.bias)
     88 
     89     def extra_repr(self):

~/PycharmProjects/estudios/venv/lib/python3.8/site-packages/torch/nn/functional.py in linear(input, weight, bias)
   1610         ret = torch.addmm(bias, input, weight.t())
   1611     else:
-&gt; 1612         output = input.matmul(weight.t())
   1613         if bias is not None:
   1614             output += bias

Im RuntimeError: size mismatch, m1: [681000 x 227], m2: [154587 x 1] at /pytorch/aten/src/TH/generic/THTensorMath.cpp:41



In linearRegression, you have defined the linear transformation as: nn.Linear(3*227*227, 1) which means the Linear layer expects 3*227*227 input features and it will output 1 feature.

However, you feed a 4D tensor of shape [1000, 3, 227, 227] (batch-channel-height-width) to the Linear layer which considers the last dimension as the feature dimension. It means Linear layer is getting 227 input features instead of 3*227*227. So, you are getting the following error.

RuntimeError: size mismatch, m1: [681000 x 227], m2: [154587 x 1]


Note that, Linear layers are associated with a weight matrix of shape in_features x out_features (in your case, it is [154587 x 1]). 
And the input to a Linear layer is flattened to a 2D tensor, in your case, it is [1000*3*227 x 227] = [681000 x 227]. 

So, an attempt to perform matrix multiplication of two tensors with shape [681000 x 227] and [154587 x 1] results in the above error.
