I’m trying to move from following tutorials to building my own simple Pytorch model and have been struggling (for hours!) with a size mismatch error.
Here is my code
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable

dataset = pd.read_csv('data/02_Medical_Appointment_NoShows_Simplified_1000.csv')
x = dataset[['Gender','Difference','Age','NeighbourhoodCode','Scholarship','Hypertension','Diabetes','Alcoholism','Handicap','SMS_received']]
y = dataset['No-show']

x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=0.2)

x_train = x_train.as_matrix()
x_test = x_test.as_matrix()
y_train = y_train.as_matrix()
y_test = y_test.as_matrix()

x_train = Variable(torch.from_numpy(x_train))
x_test = Variable(torch.from_numpy(x_test))
y_train = Variable(torch.from_numpy(y_train))
y_test = Variable(torch.from_numpy(y_test))

x_train = x_train.float()
x_test = x_test.float()
y_train = y_train.float()
y_test = y_test.float()

class Model(torch.nn.Module): 
    
    def __init__(self):
        super(Model, self).__init__()
        self.layer1 = nn.Linear(1600,10)
        self.layer2 = nn.Linear(10, 4)
        self.layer3 = nn.Linear(4, 1)
        
        self.sigmoid = torch.nn.Sigmoid()
            
    def forward (self, x):
        out1 = self.sigmoid(self.layer1(x))
        out2 = self.sigmoid(self.layer2(out1))
        y_pred = self.sigmoid(self.layer3(out2))
        return y_pred
    
model = Model()

criterion = torch.nn.BCELoss(size_average=True)
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)

for epoch in range(100):
    y_pred = model(x_train)
    loss = criterion(y_pred, y_train)
    print(epoch, loss.data[0])
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

and here is the traceback of the error
RuntimeError                              Traceback (most recent call last)
<ipython-input-159-cdcb8e69034e> in <module>()
      1 for epoch in range(100):
----> 2     y_pred = model(x_train)
      3     loss = criterion(y_pred, y_train)
      4     print(epoch, loss.data[0])
      5     optimizer.zero_grad()

~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    355             result = self._slow_forward(*input, **kwargs)
    356         else:
--> 357             result = self.forward(*input, **kwargs)
    358         for hook in self._forward_hooks.values():
    359             hook_result = hook(self, input, result)

<ipython-input-156-f36ce110d0f7> in forward(self, x)
     10 
     11     def forward (self, x):
---> 12         out1 = self.sigmoid(self.layer1(x))
     13         out2 = self.sigmoid(self.layer2(out1))
     14         y_pred = self.sigmoid(self.layer3(out2))

~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    355             result = self._slow_forward(*input, **kwargs)
    356         else:
--> 357             result = self.forward(*input, **kwargs)
    358         for hook in self._forward_hooks.values():
    359             hook_result = hook(self, input, result)

~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/linear.py in forward(self, input)
     53 
     54     def forward(self, input):
---> 55         return F.linear(input, self.weight, self.bias)
     56 
     57     def __repr__(self):

~/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py in linear(input, weight, bias)
    833     if input.dim() == 2 and bias is not None:
    834         # fused op is marginally faster
--> 835         return torch.addmm(bias, input, weight.t())
    836 
    837     output = input.matmul(weight.t())

RuntimeError: size mismatch, m1: [1600 x 10], m2: [1600 x 10] at /opt/conda/conda-bld/pytorch_1518243271935/work/torch/lib/TH/generic/THTensorMath.c:1434

My data has the following shape
print(x_train.shape)
print(y_train.shape)
print(x_test.shape)
print(y_test.shape)

torch.Size([1600, 10])
torch.Size([1600])
torch.Size([400, 10])
torch.Size([400])


Any help would be greatly appreciated!!
Thank you
Edit
Wow, realised my mistake. Changed self.layer1 = nn.Linear(1600,10) to (10,15)… working fine now