Value cannot be converted to type double without overflow: inf

In [1]: torch.autograd.Variable(torch.Tensor([float('inf')])).sum()
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-635-a58935204698> in <module>()
----> 1 torch.autograd.Variable(torch.Tensor([float('inf')])).sum()

RuntimeError: value cannot be converted to type double without overflow: inf

In [2]: torch.__version__
Out[636]: '0.3.0.post4'

But in Tensor is OK

In [3]: torch.Tensor([float('inf')]).sum()
Out[3]: inf

BTW, generally how to handle the inf in Variable or Tensor?

The issue here is that Tensor.sum returns a numeric value, while Variable.sum gives a Variable. Generally one doesn’t want inf to occur. What is your use case?

I think this is fixed in master. We should backport it to 0.3 for the next 0.3 release.

Thanks. In case that my result has a inf item in the following operation:

x = x.exp().add(1).log()

x.exp() will produce a inf.

but when I use x[x==float('inf')] = 0, the same exception occurs.

Thanks. And BTW generally how to handle the inf in Tensor ?

Hello I also have the same problem in here.

first = (tmp1*slope1)
second = (tmp2*slope2)
third = first + second
        
final_score = torch.sum(third)
final_score = final_score/total

–> 137 final_score = torch.sum(third)
138 final_score = final_score/total
139 return final_score

RuntimeError: value cannot be converted to type double without overflow: inf

all of them is Varibale. So how to solve this? @HT_Liu

Hi, It seems that the Variable cannot handle those operations with inf item.
But, I think you can try replace the inf with a larger value using torch.clamp() @herleeyandi