RuntimeError: Found dtype Double but expected Float

Viewed 739

I'm writing a code for reinforcement learning using Python 3 and 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().

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?

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
1 Answers

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_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 ...
  
Related