Hi Circa!



 circa:

r1 = torch.randn(5)



It is likely that not all of the elements of r1 have the same sign.
Try replacing your original first line with:
r1 = torch.abs (torch.randn(5))



dp+=(r1[i]*r2[i])




Let’s say that r1[0] is positive.  Then after the first iteration, dp will be
equal to -inf.  If r1[1] happens to be negative, in the second iteration
you will be calculating (-inf) + inf = nan, as it should be.  (And once
dp becomes nan, it stays nan.)


dp

Output
tensor(nan)



Note, if you run your test many times (with different random values
for r1, you will occasionally get -inf and inf for the result, instead
of nan.


So it seems adding -inf multiple times gives a nan.


The short answer is that you are not adding -inf multiple times.
You are usually adding -inf to inf somewhere in your loop.
Best.
K. Frank