Select specific columns of each row in a torch Tensor

Kalamaya: <p>There’s probably a simple way to do this, but owing to my noobness I do not know who to do this in PyTorch.</p>
<p>Basically, let’s say I have a torch tensor like so:</p>
<p><code>m = Variable(torch.randn(4,2))</code></p>
<p>Furthermore, I have a bunch of indices, given by inds, where inds is:</p>
<pre><code>inds
Variable containing
 1
 1
 0
 0
 [torch.LongTensor of size 4]
</code></pre>
<p>What I would like to do, is use the elements of inds, to index into the <em>columns</em> of m1. So I would like to get back a 4x1 vector, composed of m[0,1], m[1,1], m[2,0], m[3,0]</p>
<p>How can I do that?</p>
<p>Thanks!</p>

fmassa: <p>There is a <a href="http://pytorch.org/docs/torch.html#torch.gather"><code>gather</code> function</a> for that.</p>
<pre><code class="lang-python">m = torch.randn(4,2)
ids = torch.Tensor([1,1,0,0]).long()
print(m.gather(1, ids.view(-1,1)))
</code></pre>

Kalamaya: <p>Thank you that’s exactly what I needed. Why cant we use normal slicing as in numpy?</p>

fmassa: <p>I think we haven’t yet had the time to implement advanced indexing as the ones present in numpy. But <a class="mention" href="/u/apaszke">@apaszke</a> can better comment on that, might not be high in the priority list.</p>

apaszke: <p>How would you like to index it? We support indexing with LongTensors, and it works in the same way as numpy, but it’s not equivalent to <code>gather</code>, but rather to <code>index_select</code>.</p>

fmassa: <p><a class="mention" href="/u/apaszke">@apaszke</a> I think this is the first example in the <a href="https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html">Advanced Indexing from numpy</a></p>

apaszke: <p>Not really, this would require passing in a 2D list.</p>

magnus_w: <p><a href="https://docs.scipy.org/doc/numpy-1.13.0/user/basics.indexing.html#indexing-multi-dimensional-arrays" class="onebox" target="_blank" rel="nofollow noopener">https://docs.scipy.org/doc/numpy-1.13.0/user/basics.indexing.html#indexing-multi-dimensional-arrays</a></p>

Qiqin_Dai: <p>A follow up question: is the columns selection in Pytorch differentiable? Say the column index is obtained though a network, and the loss is computed based on the selected columns from the matrix. Will the back propagation go through?</p>

Jan_Konopka: <p>Yes indexing is fully differentiable in PyTorch!</p>
<p>You can see this if you print out a tensor that you just performed indexing (selecting) on: <code>tensor(..., grad_fn=&lt;SelectBackward&gt;)</code></p>
<p>Obviously this only back-propagates into the indexed elements with the other elements receiving zero gradient. This is necessary with some techniques such as Policy Gradients however.</p>
<p>Also thanks again to <a class="mention" href="/u/fmassa">@fmassa</a> for the clean answer!</p>

Enamex: <p>How would you do the same when assigning to an existing tensor?</p>
<p>Example:</p>
<pre><code class="lang-auto">tensor.gather(-1, indices) = new_values_tensor
</code></pre>
<p>Basically, need an index assign version of <code>gather</code>.</p>
<p>EDIT:<br>
It’s <a href="https://pytorch.org/docs/stable/tensors.html?highlight=scatter#torch.Tensor.scatter_" rel="nofollow noopener">scatter</a>.<br>
Still testing to figure out it’s usability in autograd graphs. I’m basically bootlegging a sparse representation with an occasional dense step in the middle.</p>

usman095: <p>Oh man, you’re a life saver. Been searching this for over an hour. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

niu_yuanzhuo: <p>Hi, Can the selected indexes of a tensor itself, but not the selected elements of indexes differentiable in PyTorch? So that I can select different indexes of a tensor by back propagation.</p>