Select specific columns of each row in a torch Tensor
There’s probably a simple way to do this, but owing to my noobness I do not know who to do this in PyTorch. Basically, let’s say I have a torch tensor like so: m = Variable(torch.randn(4,2)) Furthermore, I have a bunch of indices, given by inds, where inds is: <SCODE>inds
Variable containing
 1
 1
 0
 0
 [torch.LongTensor of size 4]
<ECODE> How can I do that? Thanks!
Kalamaya
<SCODE>m = torch.randn(4,2)
ids = torch.Tensor([1,1,0,0]).long()
print(m.gather(1, ids.view(-1,1)))
<ECODE>
fmassa
Thank you that’s exactly what I needed. Why cant we use normal slicing as in numpy?
Kalamaya
fmassa
apaszke
fmassa
Not really, this would require passing in a 2D list.
apaszke
magnus_w
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?
Qiqin_Dai
Yes indexing is fully differentiable in PyTorch! 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.
Jan_Konopka
How would you do the same when assigning to an existing tensor? Example: <SCODE>tensor.gather(-1, indices) = new_values_tensor
<ECODE>
Enamex
usman095