RuntimeError: Found dtype Double but expected Float - PyTorch

Ramzy_Karam: <p>I am new to pytorch and I am working on DQN for a timeseries using Reinforcement Learning and I needed to have a complex observation of timeseries and some sensor readings, so I merged two neural networks and I am not sure if that’s what is ruining my loss.backward or something else.<br>
I know there is multiple questions with the same title but none worked for me, maybe I am missing something.<br>
First of all, this is my network:</p>
<pre><code class="lang-auto">class DQN(nn.Module):
  def __init__(self, list_shape, score_shape, n_actions):
    super(DQN, self).__init__()

    self.FeatureList =  nn.Sequential(
            nn.Conv1d(list_shape[1], 32, kernel_size=8, stride=4),
            nn.ReLU(),
            nn.Conv1d(32, 64, kernel_size=4, stride=2),
            nn.ReLU(),
            nn.Conv1d(64, 64, kernel_size=3, stride=1),
            nn.ReLU(),
            nn.Flatten()
        )
    
    self.FeatureScore = nn.Sequential(
            nn.Linear(score_shape[1], 512),
            nn.ReLU(),
            nn.Linear(512, 128)
        )
    
    t_list_test = torch.zeros(list_shape)
    t_score_test = torch.zeros(score_shape)
    merge_shape = self.FeatureList(t_list_test).shape[1] + self.FeatureScore(t_score_test).shape[1]
    
    self.FinalNN =  nn.Sequential(
            nn.Linear(merge_shape, 512),
            nn.ReLU(),
            nn.Linear(512, 128),
            nn.ReLU(),
            nn.Linear(128, n_actions),
    )
    
  def forward(self, list, score):
    listOut = self.FeatureList(list)
    scoreOut = self.FeatureScore(score)
    MergedTensor = torch.cat((listOut,scoreOut),1)
    return self.FinalNN(MergedTensor)
</code></pre>
<p>I have a function called calc_loss, and at its end it return the MSE loss as below</p>
<pre><code class="lang-auto">  print(state_action_values.dtype)
  print(expected_state_action_values.dtype) 
  return nn.MSELoss()(state_action_values, expected_state_action_values)
</code></pre>
<p>and the print shows float32 and float64 respectively.<br>
I get the error when I run the loss.backward() as below</p>
<pre><code class="lang-auto">LEARNING_RATE = 0.01
optimizer = optim.Adam(net.parameters(), lr=LEARNING_RATE)

for i in range(50):
  optimizer.zero_grad()
  loss_v = calc_loss(sample(obs, 500, 200, 64), net, tgt_net)
  print(loss_v.dtype)
  print(loss_v)
  loss_v.backward()
  optimizer.step()
</code></pre>
<p>and the print output is as below:<br>
torch.float64<br>
tensor(1887.4831, dtype=torch.float64, grad_fn=)</p>
<p>Update 1:<br>
I tried using a simpler model, yet the same issue, when I tried to cast the inputs to Float, I got an error:</p>
<pre><code class="lang-auto">RuntimeError: expected scalar type Double but found Float
</code></pre>
<p>What makes the model expects double ?</p>

Ramzy_Karam: <p>Update 2:<br>
I tried to add the below line on top after the torch import but same issue of <code>RuntimeError: Found dtype Double but expected Float</code></p>
<pre><code class="lang-auto">&gt;&gt;&gt; torch.set_default_tensor_type(torch.FloatTensor)
</code></pre>
<p>But when I used the DoubleTensor I got:<br>
<code>RuntimeError: Input type (torch.FloatTensor) and weight type (torch.DoubleTensor) should be the same or input should be a MKLDNN tensor and weight is a dense tensor</code></p>