How to solve size mismatch error in Python (Pytorch)
I am currently working on multivariate linear regression using PyTorch and I am getting the following error, I did search a lot about this error and the only thing I got to know is that there is a size mismatch between data and labels. But how to solve this error. Please help me or show me the right way to solve this problem.

size mismatch, m1: [824 x 1], m2: [8 x 8]

import torch
import torch.nn as nn
import numpy as np


Xtr = np.loadtxt("TrainData.csv")
Ytr = np.loadtxt("TrainLabels.csv")


X_train = torch.FloatTensor(Xtr)
Y_train = torch.FloatTensor(Ytr)

#### MODEL ARCHITECTURE #### 

class Model(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.linear = torch.nn.Linear(8,8)
        self.lin2 = torch.nn.Linear(8,1)

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

model = Model()

loss_func = nn.MSELoss(size_average=False)
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
#print(len(list(model.parameters())))
def count_params(model): 
    return sum(p.numel() for p in model.parameters() if p.requires_grad)

### TRAINING 
for epoch in range(2):
    y_pred = model(X_train)

    loss = loss_func(y_pred, Y_train)

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    count = count_params(model)
    print(count)

test_exp = torch.FloatTensor([[6.0]])


It looks like the order of operations in your forward pass is incorrect. The short answer is swap them as shown below. More context on the various shapes below.  

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


Should be: 

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


Assuming that you have 8 features and some batch size N your input data to the forward pass will have size (N x 8). After you pass it through lin2 it will have shape (N x 1).  The linear node expects an input with shape (N x 8) but it's getting (N x 1) hence the error. 
