简体   繁体   English

RuntimeError: Found dtype Double 但预期 Float - PyTorch

[英]RuntimeError: Found dtype Double but expected Float - PyTorch

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.我是 pytorch 的新手,我正在使用强化学习为时间序列研究 DQN,我需要对时间序列和一些传感器读数进行复杂的观察,所以我合并了两个神经网络,我不确定这是否会破坏我的损失.backward 或其他东西。 I know there is multiple questions with the same title but none worked for me, maybe I am missing something.我知道有多个标题相同的问题,但没有一个对我有用,也许我错过了一些东西。
First of all, this is my network:首先,这是我的网络:

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)

I have a function called calc_loss, and at its end it return the MSE loss as below我有一个名为 calc_loss 的 function,最后它返回 MSE 损失,如下所示

  print(state_action_values.dtype)
  print(expected_state_action_values.dtype) 
  return nn.MSELoss()(state_action_values, expected_state_action_values)

and the print shows float32 and float64 respectively.打印分别显示 float32 和 float64。
I get the error when I run the loss.backward() as below当我运行 loss.backward() 时出现错误,如下所示

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()

and the print output is as below:打印 output 如下:
torch.float64火炬.float64
tensor(1887.4831, dtype=torch.float64, grad_fn=)张量(1887.4831,dtype=torch.float64,grad_fn=)

Update 1:更新1:
I tried using a simpler model, yet the same issue, when I tried to cast the inputs to Float, I got an error:我尝试使用更简单的 model,但同样的问题,当我尝试将输入转换为 Float 时,出现错误:

RuntimeError: expected scalar type Double but found Float

What makes the model expects double?是什么让 model 期望翻倍?

Update 2:更新 2:
I tried to add the below line on top after the torch import but same issue of RuntimeError: Found dtype Double but expected Float我尝试在 Torch 导入后在顶部添加以下行,但同样的问题RuntimeError: Found dtype Double but expected Float

>>> torch.set_default_tensor_type(torch.FloatTensor)

But when I used the DoubleTensor I got: 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但是当我使用 DoubleTensor 时,我得到: 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

The issue wasn't in the input to the network but the criterion of the MSELoss, so it worked fine after casting the criterion to float as below问题不在于网络的输入,而是 MSELoss 的标准,因此在将标准转换为浮动后它工作正常,如下所示

return nn.MSELoss()(state_action_values.float(), expected_state_action_values.float())

I decided to leave the answer for beginners like me who might be stuck and didn't expect to check the datatype of the loss criterion我决定把答案留给像我这样可能会被卡住并且没想到会检查损失标准的数据类型的初学者

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pytorch 得到 RuntimeError: Found dtype Double but expected Float - Pytorch getting RuntimeError: Found dtype Double but expected Float 运行时错误:发现 dtype Double 但预期为 Float - RuntimeError: Found dtype Double but expected Float PyTorch | 得到“RuntimeError: Found dtype Long but expected Float” - PyTorch | getting "RuntimeError: Found dtype Long but expected Float" Pytorch CNN 训练中的“RuntimeError: expected scalar type Double but found Float” - “RuntimeError: expected scalar type Double but found Float” in Pytorch CNN training Pytorch:RuntimeError:预期 dtype Float 但得到 dtype Long - Pytorch: RuntimeError: expected dtype Float but got dtype Long RuntimeError:找到 dtype Char 但预期 Float - RuntimeError: Found dtype Char but expected Float RuntimeError('dot: 期望两个向量具有相同的 dtype,但发现 Double 和 Float - RuntimeError('dot : expected both vectors to have same dtype, but found Double and Float 为什么不转换 Tensor 的 dtype 修复“运行时错误:预期标量类型 Double 但发现 Float”? - Why doesn't converting the dtype of a Tensor fix "RuntimeError: expected scalar type Double but found Float"? Pytorch 为什么这里需要 is.float() 来解决 RuntimeError:预期标量类型 Float 但发现 Double - Pytorch why is .float() needed here for RuntimeError: expected scalar type Float but found Double RuntimeError:预期的标量类型 Double 但发现 Float - RuntimeError: expected scalar type Double but found Float
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM