How to index a two-dimension tensor with two single-dimension tensors

username: Coldmooon
cooked: <p>I want to index a two-dimension tensor like I do in numpy.</p>
<p>a = torch.rand(5,5)<br>
b = b = torch.LongTensor([4,3,2,1,0])</p>
<p>But typing <code>a[b,b]</code> gives an error:</p>
<pre><code class="lang-auto">TypeError: indexing a tensor with an object of type LongTensor. The only supported types are integers, slices, numpy scalars and torch.LongTensor or torch.ByteTensor as the only argument.
</code></pre>
<p>How do I index a two-dimension tensor using other two tensors?</p>

username: smth
cooked: <p>doing this is not supported yet.<br>
For now you have to do:</p>
<p><code>a[b, :][:, b]</code></p>
<p>We plan to tackle this soon.</p>

username: Coldmooon
cooked: <p>Thanks for your reply. But this still gives the same error:</p>
<pre><code class="lang-auto">&gt;&gt;&gt; a[b,:][:,b]
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
TypeError: indexing a tensor with an object of type LongTensor. The only supported types are integers, slices, numpy scalars and torch.LongTensor or torch.ByteTensor as the only argument.
</code></pre>
<p>My pytorch vision is:</p>
<pre><code class="lang-auto">$ conda list | grep torch  
pytorch                   0.1.10               py35_1cu80  [cuda80]  soumith
torchvision               0.1.6                   py35_19    soumith
</code></pre>

username: b64406620
cooked: <p>The answer you gave is not correct .<br>
a = torch.rand(5,5)<br>
b = b = torch.LongTensor([4,3,2,1,0])<br>
a[b, :][:, b]<br>
Traceback (most recent call last):<br>
File “”, line 1, in <br>
TypeError: indexing a tensor with an object of type LongTensor. The only supported types are integers, slices, numpy scalars and torch.LongTensor or torch.ByteTe</p>

username: YankuHorn
cooked: <p>Any news here?<br>
I have the same issue</p>

username: dblythe
cooked: <p><code>x[b].transpose(1, 0)[b].transpose(1, 0)</code></p>
<p>should do the trick.</p>
<p>Strange that the error message to <code>x[b, :]</code> reads:</p>
<blockquote>
<p>TypeError: indexing a tensor with an object of type torch.LongTensor. The only supported types are integers, slices, numpy scalars and torch.LongTensor or torch.ByteTensor as the only argument.</p>
</blockquote>
<p>Which seems self-contradictory</p>

username: dblythe
cooked: <p>Can also try adding something like:</p>
<pre><code class="lang-python">_oldgetitem = torch.FloatTensor.__getitem__


def _getitem(self, slice_):
    if type(slice_) is tuple and torch.LongTensor in [type(x) for x in slice_]:
        i = [j for j, ix in enumerate(slice_)
             if type(ix) == torch.LongTensor][0]
        return self.transpose(0, i)[slice_[i]].transpose(i, 0)
    else:
        return _oldgetitem(self, slice_ )


torch.FloatTensor.__getitem__ = _getitem
</code></pre>
<p>Then <code>x[b, :][:, b]</code> works as expected. Maybe a bit of a hack though.</p>
<p>Regarding <code>x[b, :][:, b]</code> vs. <code>x[b, b]</code> – this is something which has never been solved for <code>numpy</code> arrays.</p>

username: Tim2
cooked: <p>use gather</p>
<pre><code class="lang-auto"> A_idx = torch.LongTensor([0, 2]).unsqueeze(-1) # the index vector
 B = torch.LongTensor([[1, 2, 3], [4, 5, 6]])
 C = torch.gather(B, 1, A_idx).view(-1)
 print(C)
</code></pre>
<p>result:<br>
tensor([1, 6])</p>