scatter_reduce(input, dim, index, reduce, *, output_size=None) → Tensor¶
Reduces all values from the
inputtensor to the indices specified in the
indextensor. For each value in
input, its output index is specified by its index in
dimension != dimand by the corresponding value in
dimension = dim. The applied reduction for non-unique indices is defined via the
"amin"). For non-existing indices, the output will be filled with the identity of the applied reduction (1 for
"prod"and 0 otherwise).
It is also required that
index.size(d) == input.size(d)for all dimensions
d. Moreover, if
output_sizeis defined the the values of
indexmust be between
output_size - 1inclusive.
For a 3-D tensor with
reduce="sum", the output is given as:
out[index[i][j][k]][j][k] += input[i][j][k] # if dim == 0 out[i][index[i][j][k]][k] += input[i][j][k] # if dim == 1 out[i][j][index[i][j][k]] += input[i][j][k] # if dim == 2
This out-of-place operation is similar to the in-place versions of
scatter_add_(), in which the output tensor is automatically created according to the maximum values in
indexand filled based on the identity of the applied reduction.
This operation may behave nondeterministically when given tensors on a CUDA device. See Reproducibility for more information.
input (Tensor) – the input tensor
dim (int) – the axis along which to index
index (LongTensor) – the indices of elements to scatter and reduce.
src (Tensor) – the source elements to scatter and reduce
reduce (str) – the reduction operation to apply for non-unique indices (
>>> input = torch.tensor([1, 2, 3, 4, 5, 6]) >>> index = torch.tensor([0, 1, 0, 1, 2, 1]) >>> torch.scatter_reduce(input, 0, index, reduce="sum", output_size=3) tensor([4, 12, 5])