# Recursive Equality

**URL:** <https://discuss.pytorch.org/t/recursive-equality/73907>\
**Category:** Uncategorized\
**Created:** [March 20, 2020, 9:45pm UTC](https://discuss.pytorch.org/t/recursive-equality/73907 "2020-03-20T21:45:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![joschnei](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/j/f0a364/32.png) [@joschnei](https://discuss.pytorch.org/u/joschnei)\
**Post date:** [March 20, 2020, 9:45pm UTC](https://discuss.pytorch.org/t/recursive-equality/73907/1 "2020-03-20T21:45:58Z")

</div>

If I have two tensors, I can check for equality between the tensors using `torch.all(a == b)` or `torch.all(torch.eq(a, b))` or something like that. The tensor == operator returns a tensor of bools, so you need to aggregate across the bools afterward.

If I have a datastructure whose values are tensors (like a list or a dict), how do I compare equality of these datastructures? The structures themselves are not aware of torch, and so don’t know that, when they recursively check equality of their members, they need to do something special for comparing tensors? I’d really like to avoid subclassing the builtin datatypes if possible.

As an aside, does anyone know why it was decided to implement == this way, instead of having == return a bool and having a different elementwise equality?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [March 22, 2020, 4:45am UTC](https://discuss.pytorch.org/t/recursive-equality/73907/2 "2020-03-22T04:45:02Z")

</div>

You could use e.g .`map` to compare the paired elements in both containers.  
Also, note that the `==` operation is usually not recommended for floating point numbers, to you should use `torch.allclose`, if applicable:

```python
lista = [torch.randn(1) for _ in range(5)]
listb = [a+1e-6 for a in lista]

all(map(lambda x, y: torch.allclose(x, y), lista, listb))
> True

```

> [@joschnei](#):
>
> As an aside, does anyone know why it was decided to implement == this way, instead of having == return a bool and having a different elementwise equality?

Most likely to stick to the numpy API, which yields the same output.
