Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I select single indices over a dimension in pytorch?

Tags:

tensor

pytorch

Assume I have a tensor sequences of shape [8, 12, 2]. Now I would like to make a selection of that tensor for each first dimension which results in a tensor of shape [8, 2]. The selection over dimension 1 is specified by indices stored in a long tensor indices of shape [8].

I tried this, however it selects each index in indices for each first dimension in sequences instead of only one.

sequences[:, indices]

How can I make this query without a slow and ugly for loop?

like image 905
Chris Avatar asked Dec 31 '18 10:12

Chris


1 Answers

sequences[torch.arange(sequences.size(0)), indices]
like image 113
Lock-not-gimbal Avatar answered Oct 06 '22 00:10

Lock-not-gimbal