I need a Torch command that checks if two tensors have the same content, and returns TRUE if they have the same content.
For example:
local tens_a = torch.Tensor({9,8,7,6});
local tens_b = torch.Tensor({9,8,7,6});
if (tens_a EQUIVALENCE_COMMAND tens_b) then ... end
What should I use in this script instead of EQUIVALENCE_COMMAND
?
I tried simply with ==
but it does not work.
To compare tensors you can do element wise:
torch.eq
is element wise:Or
torch.equal
for the whole tensor exactly:But then you may be lost because at some point there are small differences you would like to ignore. For instance floats
1.0
and1.0000000001
are pretty close and you may consider these are equal. For that kind of comparison you havetorch.allclose
.At some point may be important to check element wise how many elements are equal, comparing to the full number of elements. If you have two tensors
dt1
anddt2
you get number of elements ofdt1
asdt1.nelement()
And with this formula you get the percentage:
You can convert the two tensors to numpy arrays:
and then something like
would give you a fairly good idea of how equals are they.
This below solution worked for me:
From the documentation:
https://github.com/torch/torch7/blob/master/doc/maths.md#torcheqa-b
Implements == operator comparing each element in a with b (if b is a number) or each element in a with corresponding element in b.
UPDATE
from @deltheil
or even simpler
Try this if you want to ignore small precision differences which are common for floats