# Boolean Indexing

**URL:** <https://discuss.pytorch.org/t/boolean-indexing/39626>\
**Category:** Uncategorized\
**Created:** [March 12, 2019, 12:48pm UTC](https://discuss.pytorch.org/t/boolean-indexing/39626 "2019-03-12T12:48:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![samuel](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/34f0e0/32.png) [@samuel](https://discuss.pytorch.org/u/samuel)\
**Post date:** [March 12, 2019, 12:48pm UTC](https://discuss.pytorch.org/t/boolean-indexing/39626/1 "2019-03-12T12:48:23Z")

</div>

I found a behavior that I could not completely explain in boolean indexing. While it works fine with a tensor

> \>\>\> a = torch.tensor([[1,2],[3,4]])  
> \>\>\> a[torch.tensor([[True,False],[False,True]])]  
> tensor([1, 4])

It does not work with a list of booleans

> \>\>\> a[[[True,False],[False,True]]]  
> tensor([3, 2])

My best guess is that in the second case the bools are cast to long and treated as indexes. Is this behavior wanted or a bug?

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [March 12, 2019, 1:06pm UTC](https://discuss.pytorch.org/t/boolean-indexing/39626/2 "2019-03-12T13:06:28Z")

</div>

Hi,

If you pass a bool tensor, it is interpretet as a mask and will return the entries where True is given.  
Isn’t it interpretting the list of bools as a list of int with False=0 and True=1? Wrapping this in a `torch.ByteTensor()` will recover the mask behavior.

---

<div class="post-metadata">

**Author:** ![imaluengo](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/imaluengo/32/9847_2.png) [@imaluengo](https://discuss.pytorch.org/u/imaluengo)\
**Post date:** [March 12, 2019, 1:58pm UTC](https://discuss.pytorch.org/t/boolean-indexing/39626/3 "2019-03-12T13:58:34Z")

</div>

I think pytorch here is following same numpy behaviour, as @albanD mentioned:

1- When a boolean tensor / array is passed to index, it will perform a mask behaviour.

2- Both in pytorch and numpy, when providing a Python List it will assume as coordinates to grab:

```auto
>>> import numpy as np
>>> a = np.array([[1, 2], [3, 4]])
>>> a[[[1, 0], [0, 1]]]
array([3, 2])

```

Which reads as:  
1- From row 1 pick column 0  
2- From row 0 pick column 1

The only thing that differs from pytorch and numpy here is that pytorch is interpereting `True = 1` and `False = 0` (as suggested by @albanD)

---

<div class="post-metadata">

**Author:** ![samuel](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/34f0e0/32.png) [@samuel](https://discuss.pytorch.org/u/samuel)\
**Post date:** [March 12, 2019, 2:15pm UTC](https://discuss.pytorch.org/t/boolean-indexing/39626/4 "2019-03-12T14:15:08Z")

</div>

Yes, this is also how I understood it. But this is a problem isn’t it? Isn’t it very misleading to interpret a list of bool not as a mask differently but a tensor of uint8?

---

<div class="post-metadata">

**Author:** ![imaluengo](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/imaluengo/32/9847_2.png) [@imaluengo](https://discuss.pytorch.org/u/imaluengo)\
**Post date:** [March 12, 2019, 2:30pm UTC](https://discuss.pytorch.org/t/boolean-indexing/39626/5 "2019-03-12T14:30:11Z")

</div>

Yup I agree, I would also find it unexpected behaviour. I was not trying to justify but trying to give an explanation.

Whether is the correct behaviour or not is up to Pytorch Devs 🙂
