Question: I'm trying to build a neural network to predict per-capita-income for counties in US based on the education level of their citizens. X and y have the same dtype (I have checked this) but I'm getting an error. Here is my data:  county_FIPS state county per_capita_personal_income_2019 \ 0 51013 VA Arlington, VA 97629 per_capita_personal_income_2020 per_capita_personal_income_2021 \ 0 100687 107603 associate_degree_numbers_2016_2020 bachelor_degree_numbers_2016_2020 \ 0 19573 132394  And here is my network import torch import pandas as pd df = pd.read_csv("./input/US counties - education vs per capita personal income - results-20221227-213216.csv") X = torch.tensor(df[["bachelor_degree_numbers_2016_2020", "associate_degree_numbers_2016_2020"]].values) y = torch.tensor(df["per_capita_personal_income_2020"].values) X.dtype torch.int64 y.dtype torch.int64 import torch.nn as nn class BaseNet(nn.Module): def __init__(self, in_dim, hidden_dim, out_dim): super(BaseNet, self).__init__() self.classifier = nn.Sequential( nn.Linear(in_dim, hidden_dim, bias=True), nn.ReLU(), nn.Linear(feature_dim, out_dim, bias=True)) def forward(self, x): return self.classifier(x) from torch import optim import matplotlib.pyplot as plt in_dim, hidden_dim, out_dim = 2, 20, 1 lr = 1e-3 epochs = 40 loss_fn = nn.CrossEntropyLoss() classifier = BaseNet(in_dim, hidden_dim, out_dim) optimizer = optim.SGD(classifier.parameters(), lr=lr) def train(classifier, optimizer, epochs, loss_fn): classifier.train() losses = [] for epoch in range(epochs): out = classifier(X) loss = loss_fn(out, y) loss.backward() optimizer.step() optimizer.zero_grad() losses.append(loss/len(X)) print("Epoch {} train loss: {}".format(epoch+1, loss/len(X))) plt.plot([i for i in range(1, epochs + 1)]) plt.xlabel("Epoch") plt.ylabel("Training Loss") plt.show() train(classifier, optimizer, epochs, loss_fn)  Here is the full stack trace of the error that I am getting when I try to train the network: --------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) Input In [77], in <cell line: 39>() 36 plt.ylabel("Training Loss") 37 plt.show() ---> 39 train(classifier, optimizer, epochs, loss_fn) Input In [77], in train(classifier, optimizer, epochs, loss_fn) 24 losses = [] 25 for epoch in range(epochs): ---> 26 out = classifier(X) 27 loss = loss_fn(out, y) 28 loss.backward() File ~/opt/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py:1194, in Module._call_impl(self, *input, **kwargs) 1190 # If we don't have any hooks, we want to skip the rest of the logic in 1191 # this function, and just call forward. 1192 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks 1193 or _global_forward_hooks or _global_forward_pre_hooks): -> 1194 return forward_call(*input, **kwargs) 1195 # Do not call functions when jit is used 1196 full_backward_hooks, non_full_backward_hooks = [], [] Input In [77], in BaseNet.forward(self, x) 10 def forward(self, x): ---> 11 return self.classifier(x) File ~/opt/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py:1194, in Module._call_impl(self, *input, **kwargs) 1190 # If we don't have any hooks, we want to skip the rest of the logic in 1191 # this function, and just call forward. 1192 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks 1193 or _global_forward_hooks or _global_forward_pre_hooks): -> 1194 return forward_call(*input, **kwargs) 1195 # Do not call functions when jit is used 1196 full_backward_hooks, non_full_backward_hooks = [], [] File ~/opt/anaconda3/lib/python3.9/site-packages/torch/nn/modules/container.py:204, in Sequential.forward(self, input) 202 def forward(self, input): 203 for module in self: --> 204 input = module(input) 205 return input File ~/opt/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py:1194, in Module._call_impl(self, *input, **kwargs) 1190 # If we don't have any hooks, we want to skip the rest of the logic in 1191 # this function, and just call forward. 1192 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks 1193 or _global_forward_hooks or _global_forward_pre_hooks): -> 1194 return forward_call(*input, **kwargs) 1195 # Do not call functions when jit is used 1196 full_backward_hooks, non_full_backward_hooks = [], [] File ~/opt/anaconda3/lib/python3.9/site-packages/torch/nn/modules/linear.py:114, in Linear.forward(self, input) 113 def forward(self, input: Tensor) -> Tensor: --> 114 return F.linear(input, self.weight, self.bias) RuntimeError: mat1 and mat2 must have the same dtype  Updates I have tried casting X and y to float tensors but this comes up with the following error: expected scalar type Long but found Float. If someone who knows PyTorch could try running this notebook for themselves that would be a great help. I'm struggling to get off the ground with Kaggle and ML.

Answer: The reason for this is because the parameters dtype of nn.Linear doesn't match your input's dtype; the default dtype for nn.Linear is torch.float32 which is in your case different from your input data - float64. The solution to this question solves your problem and explains why @Anonymous answer works. In short, add self.double() at the end of your constructor and things should run.