Size Mismatch using pytorch when trying to train data
I am really new to pytorch and just trying to use my own dataset to do a simple Linear Regression Model. I am only using the numbers values as inputs, too.



I have imported the data from the CSV

dataset = pd.read_csv('mlb_games_overview.csv')


I have split the data into four parts X_train, X_test, y_train, y_test

X = dataset.drop(['date', 'team', 'runs', 'win'], 1)
y = dataset['win']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=True)


I have converted the data to pytorch tensors

X_train = torch.from_numpy(np.array(X_train))
X_test = torch.from_numpy(np.array(X_test))
y_train = torch.from_numpy(np.array(y_train))
y_test = torch.from_numpy(np.array(y_test))


I have created a LinearRegressionModel

class LinearRegressionModel(torch.nn.Module):
    def __init__(self):
        super(LinearRegressionModel, self).__init__()
        self.linear = torch.nn.Linear(1, 1)
    def forward(self, x):
        y_pred = self.linear(x)
        return y_pred


I have initialized the optimizer and the loss function

criterion = torch.nn.MSELoss(reduction='sum')
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)


Now when I start to train the data I get the runtime error mismatch

EPOCHS = 500
for epoch in range(EPOCHS):
    pred_y = model(X_train) # RUNTIME ERROR HERE
    loss = criterion(pred_y, y_train)
    optimizer.zero_grad() # zero out gradients to update parameters correctly
    loss.backward() # backpropagation
    optimizer.step() # update weights
    print('epoch {}, loss {}'. format(epoch, loss.data[0]))


Error Log:

RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-40-c0474231d515&gt; in &lt;module&gt;
  1 EPOCHS = 500
  2 for epoch in range(EPOCHS):
----&gt; 3     pred_y = model(X_train)
  4     loss = criterion(pred_y, y_train)
  5     optimizer.zero_grad() # zero out gradients to update parameters correctly
RuntimeError: size mismatch, m1: [3540 x 8], m2: [1 x 1] at 
C:\w\1\s\windows\pytorch\aten\src\TH/generic/THTensorMath.cpp:752


In your Linear Regression model, you have:

self.linear = torch.nn.Linear(1, 1)


But your training data (X_train) shape is 3540 x 8 which means you have 8 features representing each input example. So, you should define the linear layer as follows.

self.linear = torch.nn.Linear(8, 1)


A linear layer in PyTorch has parameters, W and b. If you set the in_features to 8 and out_features to 1, then the shape of the W matrix will be 1 x 8 and the length of b vector will be 1.

Since your training data shape is 3540 x 8, you can perform the following operation.

linear_out = X_train W_T + b


I hope it clarifies your confusion.
