Skip to content

Indexing with list of booleans differs from numpy #6773

@fmassa

Description

@fmassa

In numpy, indexing with a list of booleans is equivalent to indexing with a boolean array, which means it performs masking.

In PyTorch, the list of booleans is cast to a long tensor.

I believe this discrepancy should be fixed.

a = torch.rand(3, 3)
print(a)

 0.1041  0.6888  0.7988
 0.9398  0.9151  0.7642
 0.5340  0.4715  0.8128
[torch.FloatTensor of size (3,3)]

print(a[[True, False, False]]

 0.9398  0.9151  0.7642
 0.1041  0.6888  0.7988
 0.1041  0.6888  0.7988
[torch.FloatTensor of size (3,3)]

aa = a.numpy()
print(aa[[True, False, False]])
array([[0.10411686, 0.6887991 , 0.7988465 ]], dtype=float32)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions