
Gather
torch_gather(self, dim, index, sparse_grad = FALSE)
(Tensor) the source tensor
(int) the axis along which to index
(LongTensor) the indices of elements to gather
(bool,optional) If TRUE
, gradient w.r.t. input
will be a sparse 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
dim = i
, then index
must be an out
will have the same size as index
.
if (torch_is_installed()) {
t = torch_tensor(matrix(c(1,2,3,4), ncol = 2, byrow = TRUE))
torch_gather(t, 2, torch_tensor(matrix(c(1,1,2,1), ncol = 2, byrow=TRUE), dtype = torch_int64()))
}
Run the code above in your browser using DataLab