title: PyTorch | getting "RuntimeError: Found dtype Long but expected Float"
body: <p>I'm trying to train a CNN on a custom dataset. Code:</p>
  <p><code>Dataset.py</code></p>
  <pre><code>class MyDataset(Dataset):
      def __init__(self, csv_file, root_dir):
          self.annotations = pd.read_csv(csv_file)
          self.root_dir = root_dir
          self.transform = transform
  
      def __len__(self):
          return len(self.annotations)
  
      def __getitem__(self, index):
          img_path = os.path.join(self.root_dir, self.annotations.iloc[index, 0])
          y_label = torch.tensor(int(self.annotations.iloc[index, 1]))
          img = cv2.imread(img_path)
  
          # resize
          res = cv2.resize(img, dsize=(50, 50), interpolation=cv2.INTER_CUBIC)
          
          # convert image to tensor
          res = torch.from_numpy(res)
  
          return (res, y_label)
  </code></pre>
  <p><code>Model.py</code></p>
  <pre><code>class ConvNet(torch.nn.Module):
      def __init__(self):
          super(ConvNet, self).__init__()
  
          f2 = 4
          self.layer2 = nn.Sequential(
              nn.Conv2d(50, f2, kernel_size=5, padding=2),
              nn.ReLU(),
              nn.BatchNorm2d(f2),
              nn.MaxPool2d(kernel_size=2, stride=2))
          self.fc1 = nn.Linear(100, 200)
          self.fc2 = nn.Linear(200, 20)
          self.fc3 = nn.Linear(20, 1)
  
      def forward(self, x):
          x = self.layer2(x.float())
          x = x.reshape(x.size(0), -1)
          x = self.fc1(x)
          x = self.fc2(x)
          x = self.fc3(x)
          return x
  </code></pre>
  <p>and here is my training code:</p>
  <pre><code>dataset = MyDataset(
      csv_file='dataset.csv',
      root_dir='tmp')
  
  train_set, test_set = torch.utils.data.random_split(dataset, lengths=[500, 70])
  
  train_loader = DataLoader(dataset=train_set, batch_size=16, shuffle=True)
  test_loader = DataLoader(dataset=test_set, batch_size=16, shuffle=True)
  
  
  model = ConvNet()
  criterion = nn.MSELoss()
  optimizer = torch.optim.Adam(model.parameters(), lr=0.0001)
  
  for epoch in range(20):
      losses = []
  
      for batch_idx, (data, targets) in enumerate(train_loader):
  
          data = data.to(device=device)
          targets = targets.to(device=device)
  
          # forward
          scores = model(data)
          loss = criterion(scores, targets)
          losses.append(loss.item())
  
          # backward
          optimizer.zero_grad()
          loss.backward()
  
          optimizer.step()
  
      print('Cost: {0} = {1}'.format(epoch, sum(losses)/len(losses)))
  </code></pre>
  <p>But I get <code>RuntimeError: Found dtype Long but expected Float</code>. This probably comes from the fact that I do <code>x = self.layer2(x.float())</code> to avoid overflow.</p>
  <p>I would like to know how to fix that error. It's difficult to pin-point where the exact problem comes from.</p>
  <p>How can I solve this?</p>