How to check if two Torch tensors or matrices are equal?

DavideChicco.it picture DavideChicco.it · Oct 7, 2015 · Viewed 53.1k times · Source

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.

Answer

yutseho picture yutseho · Oct 8, 2015

https://github.com/torch/torch7/blob/master/doc/maths.md#torcheqa-b

torch.eq(a, 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

torch.all(torch.eq(tens_a, tens_b))

or even simpler

torch.all(tens_a.eq(tens_b))