<h2 dir="auto"><g-emoji class="g-emoji" alias="bug" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f41b.png">🐛</g-emoji> Bug</h2>
<p dir="auto">Unmatched behavior of numpy and pytorch on indexing with a list of bool value.</p>
<h2 dir="auto">To Reproduce</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import torch
x = torch.randn(3,4)
print(x[[True,False,True]])
print(x.numpy()[[True, False, True]])"><pre class="notranslate"><code class="notranslate">import torch
x = torch.randn(3,4)
print(x[[True,False,True]])
print(x.numpy()[[True, False, True]])
</code></pre></div>
<p dir="auto">With output</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; import torch
&gt;&gt;&gt; x = torch.randn(3,4)
&gt;&gt;&gt; x[[True,False,True]]
tensor([[ 0.9176,  0.6378,  1.1367, -1.3373],
        [ 0.4389,  0.2105, -0.7689, -0.5038],
        [ 0.9176,  0.6378,  1.1367, -1.3373]])
&gt;&gt;&gt; x.numpy()[[True, False, True]]
array([[ 0.43885437,  0.21049356, -0.76887   , -0.5038339 ],
       [-0.7998242 ,  0.7215161 ,  0.5523575 , -1.6851963 ]],
      dtype=float32)"><pre class="notranslate"><code class="notranslate">&gt;&gt;&gt; import torch
&gt;&gt;&gt; x = torch.randn(3,4)
&gt;&gt;&gt; x[[True,False,True]]
tensor([[ 0.9176,  0.6378,  1.1367, -1.3373],
        [ 0.4389,  0.2105, -0.7689, -0.5038],
        [ 0.9176,  0.6378,  1.1367, -1.3373]])
&gt;&gt;&gt; x.numpy()[[True, False, True]]
array([[ 0.43885437,  0.21049356, -0.76887   , -0.5038339 ],
       [-0.7998242 ,  0.7215161 ,  0.5523575 , -1.6851963 ]],
      dtype=float32)
</code></pre></div>
<h2 dir="auto">Expected behavior</h2>
<p dir="auto">pytorch should behave the same as tensor.</p>
<p dir="auto">In fact, indexing with byte tensor works:<br>
<code class="notranslate">x[torch.Tensor([True,False,True]).byte()]</code> works</p>
<h2 dir="auto">Environment</h2>
<p dir="auto">Collecting environment information...<br>
PyTorch version: 1.0.0a0+7df6650<br>
Is debug build: No<br>
CUDA used to build PyTorch: 9.0.176</p>
<p dir="auto">OS: Ubuntu 16.04.3 LTS<br>
GCC version: (Ubuntu 5.4.0-6ubuntu1~16.04.5) 5.4.0 20160609<br>
CMake version: version 3.12.0</p>
<p dir="auto">Python version: 3.6<br>
Is CUDA available: Yes<br>
CUDA runtime version: 9.0.176<br>
GPU models and configuration:<br>
GPU 0: Tesla K40c<br>
GPU 1: Tesla K40c</p>
<p dir="auto">Nvidia driver version: 390.25<br>
cuDNN version: Probably one of the following:<br>
/usr/lib/x86_64-linux-gnu/libcudnn.so.7.0.5<br>
/usr/lib/x86_64-linux-gnu/libcudnn_static_v7.a</p>
<p dir="auto">Versions of relevant libraries:<br>
[pip] numpy (1.15.0)<br>
[pip] numpydoc (0.7.0)<br>
[pip] torch (1.0.0a0+7df6650)<br>
[pip] torchtext (0.2.1)<br>
[pip] torchvision (0.2.1)<br>
[conda] magma-cuda90              2.3.0                         1    pytorch<br>
[conda] torch                     0.5.0a0+6f6a1f2           <br>
[conda] torch                     0.5.0a0+c425d03           <br>
[conda] torch                     0.5.0a0+d2f98fc           <br>
[conda] torch                     0.5.0a0+2f311be           <br>
[conda] torch                     1.0.0a0+7df6650           <br>
[conda] torchtext                 0.2.1                     <br>
[conda] torchvision               0.2.1                     </p>
<p dir="auto">In numpy, indexing with a list of booleans is equivalent to indexing with a boolean array, which means it performs masking.</p>
<p dir="auto">In PyTorch, the list of booleans is cast to a long tensor.</p>
<p dir="auto">I believe this discrepancy should be fixed.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="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)"><pre class="notranslate"><span class="pl-s1">a</span> <span class="pl-c1">=</span> <span class="pl-s1">torch</span>.<span class="pl-en">rand</span>(<span class="pl-c1">3</span>, <span class="pl-c1">3</span>)
<span class="pl-en">print</span>(<span class="pl-s1">a</span>)

 <span class="pl-c1">0.1041</span>  <span class="pl-c1">0.6888</span>  <span class="pl-c1">0.7988</span>
 <span class="pl-c1">0.9398</span>  <span class="pl-c1">0.9151</span>  <span class="pl-c1">0.7642</span>
 <span class="pl-c1">0.5340</span>  <span class="pl-c1">0.4715</span>  <span class="pl-c1">0.8128</span>
[<span class="pl-s1">torch</span>.<span class="pl-v">FloatTensor</span> <span class="pl-s1">of</span> <span class="pl-s1">size</span> (<span class="pl-c1">3</span>,<span class="pl-c1">3</span>)]

<span class="pl-s1">print</span>(<span class="pl-s1">a</span>[[<span class="pl-c1">True</span>, <span class="pl-c1">False</span>, <span class="pl-c1">False</span>]]

 <span class="pl-c1">0.9398</span>  <span class="pl-c1">0.9151</span>  <span class="pl-c1">0.7642</span>
 <span class="pl-c1">0.1041</span>  <span class="pl-c1">0.6888</span>  <span class="pl-c1">0.7988</span>
 <span class="pl-c1">0.1041</span>  <span class="pl-c1">0.6888</span>  <span class="pl-c1">0.7988</span>
[<span class="pl-s1">torch</span>.<span class="pl-v">FloatTensor</span> <span class="pl-s1">of</span> <span class="pl-s1">size</span> (<span class="pl-c1">3</span>,<span class="pl-c1">3</span>)]

<span class="pl-s1">aa</span> <span class="pl-c1">=</span> <span class="pl-s1">a</span>.<span class="pl-en">numpy</span>()
<span class="pl-s1">print</span>(<span class="pl-s1">aa</span>[[<span class="pl-c1">True</span>, <span class="pl-c1">False</span>, <span class="pl-c1">False</span>]])
<span class="pl-s1">array</span>([[<span class="pl-c1">0.10411686</span>, <span class="pl-c1">0.6887991</span> , <span class="pl-c1">0.7988465</span> ]], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s1">float32</span>)</pre></div>