.scatter_reduce()
Anonymous contributor
Published Jan 23, 2025
Contribute to Docs
In PyTorch, the .scatter_reduce()
function reduces all values in the source tensor using the given reduction method (sum
, prod
, mean
, amax
, amin
) and scatters the result to the input tensor.
Syntax
torch.scatter_reduce(input, dim, index, src, reduce, *, include_self=True)
input
: The input tensor.dim
: The dimension along which to perform the reduction.index
: A tensor that specifies the indices for reduction.src
: The source tensor containing the values to be reduced.reduce
: The reduction method to perform (sum
,prod
,mean
,amax
,amin
).include_self
: IfTrue
(default), the values from theself
tensor (i.e.,input
) are used in the reduction.
Example
The following example demonstrates the usage of the .scatter_reduce()
function:
import torch# Create an input tensorinput = torch.tensor([21, 22, 23, 24])# Create a source tensor containing the valuessrc = torch.tensor([11, 12, 13, 14, 15, 16])# Create an index tensor containing the indicesindex = torch.tensor([0, 1, 2, 2, 1, 0])# Reduce the values along dimension 0 in 'src' and scatter the result to 'input'res = torch.scatter_reduce(input, 0, index, src, reduce="sum")# Print the resultant tensorprint(res)
The above code produces the following output:
tensor([48, 49, 50, 24])
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PyTorch on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly90 hours - Free course
Intro to PyTorch and Neural Networks
Learn how to use PyTorch to build, train, and test artificial neural networks in this course.Intermediate3 hours