Expected object of scalar type Float but got scalar type Double

I’m writing a regression program in pytorch and when it runs, the model gives the following error:

Expected object of scalar type Float but got scalar type Double for argument #4

The error occurs at this location:

class LinearRegression(torch.nn.Module):
def init(self):
super(LinearRegression, self).init()
self.linear = torch.nn.Linear(1, 1)
def forward(self, x):
y_pred = self.linear(x) <---- ERROR OCCURS HERE
return y_pred

The full program and data can be downloaded here: https://drive.google.com/open?id=1Ct9hsYoLNeBhua6-Ch4e5xS4Mds1oyWR

Any help would be greatly appreciated.

This error is raised, if the module parameters (weight and bias) do not have the same type as the input data.
I guess your linear layer has float() parameters, while you are passing double() tensors into it.
Try to call x = x.float() before feeding the data into the model.

This error is often raised, if you created the tensors from numpy arrays, as numpy uses double as the default array type, while PyTorch uses float.