Compare with a float number in Pytorch returns weird results

I have a simple code that generates a random tensor (float32). Then I compare each of the values in the tensor equal to the first number in the tensor. I got weird result where all True values are returning.

import torch
torch.manual_seed(1)
a = torch.rand(2, 10)
tensor([[0.7576, 0.2793, 0.4031, 0.7347, 0.0293, 0.7999, 0.3971, 0.7544, 0.5695,
0.4388], [0.6387, 0.5247, 0.6826, 0.3051, 0.4635, 0.4550, 0.5725, 0.4980, 0.9371,
0.6556]])

a != 0.7576

tensor([[True, True, True, True, True, True, True, True, True, True], [True, True, True, True, True, True, True, True, True, True]])

Meanwhile If I changed to something like this, I got the right returning boolean values.

a != a[0][0]

tensor([[False, True, True, True, True, True, True, True, True, True],
[ True, True, True, True, True, True, True, True, True, True]])

I am curious why this happens? Thanks!

Hi Hoang!

Here a is only printed out to four decimal digits. That is, a[0][0]
is a number close to, but not actually equal to 0.7576. Perhaps
a[0][0] = 0.75758379.

Here you compare a[0][0] (as well as the rest of a) with 0.7576.
But a[0][0] is only approximately equal 0.7576 (equal only to four
decimal digits). Because it is not exactly equal, the inequality (!=)
test returns True (as it should).

Now you are comparing a to a[0][0] (in our hypothetical example, to
0.75758379). a[0][0] is, after all, exactly equal to a[0][0], so the
inequality test returns False, as you expect.

Best.

K. Frank

1 Like

Thank you Frank for your detailed explanation. Truly appreciate it!