92

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.

MBT
  • 21,733
  • 19
  • 84
  • 102
DavideChicco.it
  • 3,318
  • 13
  • 56
  • 84
  • 1
    To allow for floating point differences, see [Check if PyTorch tensors are equal within epsilon](https://stackoverflow.com/q/53374928/5353461). – Tom Hale Nov 19 '18 at 13:09

6 Answers6

103
torch.eq(a, b)

eq() implements the == operator comparing each element in a with b (if b is a value) or each element in a with its corresponding element in b (if b is a tensor).


Alternative from @deltheil:

torch.all(tens_a.eq(tens_b))
iacob
  • 20,084
  • 6
  • 92
  • 119
yutseho
  • 1,639
  • 1
  • 15
  • 27
  • 6
    As mentioned in other answers, with present torch, `.eq` returns a tensor whereas `.equal` actually returns a bool. – fuzzyTew Sep 05 '21 at 01:15
  • Is there an approximate version of this? I would like something to compute the norm `||A - B||` and check if that's small (which is not the same as `A.allclose(B)`). – a06e Nov 12 '22 at 07:09
96

This below solution worked for me:

torch.equal(tensorA, tensorB)

From the documentation:

True if two tensors have the same size and elements, False otherwise.

Tom Hale
  • 40,825
  • 36
  • 187
  • 242
Erik
  • 961
  • 6
  • 2
  • 11
    This answer should be the only one used for this issue as this function match the exact behaviour wanted by the OP + it is the most effective, if the tensors are not of same shape no computation is made. – Louis Lac Apr 09 '20 at 13:46
30

To compare tensors you can do element wise:

torch.eq is element wise:

torch.eq(torch.tensor([[1., 2.], [3., 4.]]), torch.tensor([[1., 1.], [4., 4.]]))
tensor([[True, False], [False, True]])

Or torch.equal for the whole tensor exactly:

torch.equal(torch.tensor([[1., 2.], [3, 4.]]), torch.tensor([[1., 1.], [4., 4.]]))
# False
torch.equal(torch.tensor([[1., 2.], [3., 4.]]), torch.tensor([[1., 2.], [3., 4.]]))
    # True

But then you may be lost because at some point there are small differences you would like to ignore. For instance floats 1.0 and 1.0000000001 are pretty close and you may consider these are equal. For that kind of comparison you have torch.allclose.

torch.allclose(torch.tensor([[1., 2.], [3., 4.]]), torch.tensor([[1., 2.000000001], [3., 4.]]))
# True

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 and dt2 you get number of elements of dt1 as dt1.nelement()

And with this formula you get the percentage:

print(torch.sum(torch.eq(dt1, dt2)).item()/dt1.nelement())
The Thonnu
  • 3,578
  • 2
  • 8
  • 30
prosti
  • 42,291
  • 14
  • 186
  • 151
10

Try this if you want to ignore small precision differences which are common for floats

torch.all(torch.lt(torch.abs(torch.add(tens_a, -tens_b)), 1e-12))
tworec
  • 4,409
  • 2
  • 29
  • 34
  • 12
    alternatively you may use [`torch.allclose()`](https://pytorch.org/docs/master/torch.html?highlight=allclose#torch.allclose). – irudyak Aug 25 '19 at 18:37
1

This solution also works fine for me and seems more natural.

torch.all(tensorA == tensorB)

gives output as:

if equivalent gives output as tensor(1, device='cuda:0', dtype=torch.uint8) else : tensor(0, device='cuda:0', dtype=torch.uint8)

Gaurav Shrivastava
  • 905
  • 12
  • 19
-1

You can convert the two tensors to numpy arrays:

local tens_a = torch.Tensor((9,8,7,6));
local tens_b = torch.Tensor((9,8,7,6));

a=tens_a.numpy()
b=tens_b.numpy()

and then something like

np.sum(a==b)
4

would give you a fairly good idea of how equals are they.