torch.gather¶
-
torch.
gather
(input, dim, index, out=None, sparse_grad=False) → Tensor¶ Gathers values along an axis specified by dim.
For a 3-D tensor the output is specified by:
out[i][j][k] = input[index[i][j][k]][j][k] # if dim == 0 out[i][j][k] = input[i][index[i][j][k]][k] # if dim == 1 out[i][j][k] = input[i][j][index[i][j][k]] # if dim == 2
If
input
is an n-dimensional tensor with size anddim = i
, thenindex
must be an -dimensional tensor with size where andout
will have the same size asindex
.- Parameters
Example:
>>> t = torch.tensor([[1,2],[3,4]]) >>> torch.gather(t, 1, torch.tensor([[0,0],[1,0]])) tensor([[ 1, 1], [ 4, 3]])