title: How to fix RuntimeError "Expected object of scalar type Float but got scalar type Double for argument"?
body: <p>I'm trying to train a classifier via PyTorch. However, I am experiencing problems with training when I feed the model with training data.
  I get this error on <code>y_pred = model(X_trainTensor)</code>:</p>
  
  <blockquote>
    <p>RuntimeError: Expected object of scalar type Float but got scalar type Double for argument #4 'mat1'</p>
  </blockquote>
  
  <p>Here are key parts of my code:</p>
  
  <pre class="lang-py prettyprint-override"><code># Hyper-parameters 
  D_in = 47  # there are 47 parameters I investigate
  H = 33
  D_out = 2  # output should be either 1 or 0
  </code></pre>
  
  <pre class="lang-py prettyprint-override"><code># Format and load the data
  y = np.array( df['target'] )
  X = np.array( df.drop(columns = ['target'], axis = 1) )
  X_train, X_test, y_train, y_test = train_test_split(X, y, train_size = 0.8)  # split training/test data
  
  X_trainTensor = torch.from_numpy(X_train) # convert to tensors
  y_trainTensor = torch.from_numpy(y_train)
  X_testTensor = torch.from_numpy(X_test)
  y_testTensor = torch.from_numpy(y_test)
  </code></pre>
  
  <pre class="lang-py prettyprint-override"><code># Define the model
  model = torch.nn.Sequential(
      torch.nn.Linear(D_in, H),
      torch.nn.ReLU(),
      torch.nn.Linear(H, D_out),
      nn.LogSoftmax(dim = 1)
  )
  </code></pre>
  
  <pre class="lang-py prettyprint-override"><code># Define the loss function
  loss_fn = torch.nn.NLLLoss() 
  </code></pre>
  
  <pre class="lang-py prettyprint-override"><code>for i in range(50):
      y_pred = model(X_trainTensor)
      loss = loss_fn(y_pred, y_trainTensor)
      model.zero_grad()
      loss.backward()
      with torch.no_grad():       
          for param in model.parameters():
              param -= learning_rate * param.grad
  </code></pre>