.index_reduce_()
Published Dec 16, 2024
Contribute to Docs
In PyTorch, .index_reduce_()
performs an in-place reduction operation (such as sum, product, or mean) on a tensor along a specified dimension. It uses an index tensor to map input elements to positions in the output tensor, effectively aggregating values with the same index.
Syntax
Tensor.index_reduce_(dim, index, source, reduce, *, include_self=True)
dim
: The axis of the tensor along which the reduction is performed.index
: A 1D tensor containing indices that map the elements in the source tensor to specific positions in the current tensor.source
: The tensor whose values are reduced and added to the current tensor at positions specified byindex
.reduce
: Specifies the reduction operation to apply. Possible values include:'prod'
: Product of elements with the same index.'mean'
: Mean of elements with the same index.'amax'
: Maximum of elements with the same index.'amin'
: Minimum of elements with the same index.
include_self
(Optional): Determines whether the existing values in the current tensor are included in the reduction operation.- If
True
, the values already present in the tensor are included. If no value is provided for the parameter,include_self
defaults toTrue
. - If
False
, only thesource
tensor values contribute to the reduction.
- If
Example
The following example demonstrates the usage of the .index_reduce()
method:
import torch# Define the target tensortarget = torch.zeros(2)# Source tensorsource = torch.tensor([1.0, 2.0, 3.0, 4.0])# Indices mapping source to targetindex = torch.tensor([0, 1, 0, 1], dtype=torch.long) # Ensure index tensor is of type 'long'# Perform in-place reduction using 'mean' along the 0th dimension (rows)target.index_reduce_(dim=0, index=index, source=source, reduce='mean')print(target)
The above code produces the following output:
tensor([1.3333, 2.0000])
This code reduces the source
tensor along dimension 0 by averaging ('mean'
reduce) the values mapped to the same indices in the index
tensor, updating the target
tensor in place.
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
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 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