简体   繁体   English

运行时错误:发现 dtype Double 但预期为 Float

[英]RuntimeError: Found dtype Double but expected Float

I'm writing a code for reinforcement learning using Python 3 and Pytorch 1.9.1.我正在使用 Python 3 和 Pytorch 1.9.1 编写用于强化学习的代码。

I post a question because I don't understand the error line.我发布了一个问题,因为我不明白错误行。 The error occurs on the line of the loss.mean().backward().错误发生在 loss.mean().backward() 的行上。

It is said that the dtype should have a float, but the double came in, but no matter how much the dtype is printed, it comes out as a float 32. What's the problem?据说dtype应该有float,但是double进来了,但是不管dtype打印多少,出来的都是float 32。有什么问题吗?

The code in question is as follows.有问题的代码如下。

def train_net_ap(self, idx):
    s, a, r, s_prime, done_mask, prob_a = self.make_batch(idx)
    print("a is ", a)

    for i in range(K_epoch):
        td_target = r + gamma * self.v_ap(s_prime) * done_mask
        delta = td_target - self.v_ap(s)
        delta = delta.detach().numpy()

        advantage_lst = []
        advantage = 0.0
        for delta_t in delta[::-1]:
            advantage = gamma * lmbda * advantage + delta_t[0]
            advantage_lst.append([advantage])
        advantage_lst.reverse()
        advantage = torch.tensor(advantage_lst, dtype=torch.float)

        pi = self.pi_ap(s, softmax_dim=1)
        pi_a = pi.gather(1, a)
        ratio = torch.exp(torch.log(pi_a) - torch.log(prob_a))  # a/b == exp(log(a)-log(b))

        surr1 = ratio * advantage
        surr2 = torch.clamp(ratio, 1 - eps_clip, 1 + eps_clip) * advantage
        loss = -torch.min(surr1, surr2) + F.smooth_l1_loss(self.v_ap(s), td_target.detach())

        print("loss is ", loss)
        print("loss dtype is ", loss.dtype)
        print("loss.mean() is ", loss.mean(), loss.mean().dtype)
        self.optimizer.zero_grad()
        loss.mean().backward()
        self.optimizer.step()

The printed phrase and error message are as follows.打印的短语和错误信息如下。

loss dtype is  torch.float32 
loss.mean() is  tensor(6.1353,   grad_fn=<MeanBackward0>) torch.float32


Traceback (most recent call last):
  main()
  model.train_net_ap(x)
  loss.mean().backward()
    
  torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs)
  allow_unreachable=True, accumulate_grad=True)  # allow_unreachable flag

RuntimeError: Found dtype Double but expected Float

The error says that it expected a Float data type, but it is receiving a Double type data, what you can do is change the variable type to the one required in this case do something similar to:该错误表示它期望 Float 数据类型,但它接收的是 Double 类型数据,您可以做的是将变量类型更改为在这种情况下所需的类型,执行类似于:

float(double_variable)

Or if you require a more precise float value or with a specific number of decimal places you could use this:或者,如果您需要更精确的浮点值或具有特定小数位数,您可以使用:

                                   (This is an example)
v1 = 0.00582811585976
import numpy as np
np.float32(v1)
float(np.float32(v1))  #Convert to 32bit and then back to 64bit
'%.14f'%np.float32(v1) #This rounds to v2 if you're printing 14 places of precision ...
  

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

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