PyTorch torch.index_reduce function
PyTorch torch reference manual
torch.index_reduce A function in PyTorch for aggregating the values of a source tensor at specified index positions in a specified way. It aggregates along a specified dimension dim, in index at the specified index positions in the specified way source value.
Function definition
torch.index_reduce(input, dim, index, source, reduce='mean', *, include_self=True)
Parameters:
input(Tensor): the input tensor.dim(int): The dimension to index along.index(Tensor): A 1D integer tensor specifying the positions to aggregate to.source(Tensor): The source tensor, the values to aggregate.reduce(str): The aggregation mode, with optional values 'mean', 'prod', 'amax', 'amin'. Defaults to 'mean'.include_self(bool, optional): Whether to include the original value at the index position itself in the aggregation. Defaults to True.
Return Value:
torch.Tensor: Returns the aggregated tensor.
Usage examples
Example
import
torch
Create an input tensor
input
=
torch.
randn
(
3
,
3
)
Create the indices and source
index
=
torch.
tensor
(
[
0
,
0
,
0
]
)
source
=
torch.
tensor
(
[
1.0
,
2.0
,
3.0
]
)
Aggregate using mean
output
=
torch.
index_reduce
(
input
,
dim
=
0
,
index
=
index
,
source
=
source
,
reduce
=
'mean'
)
(
"Input:"
)
(
input
)
(
"nIndices:"
,
index
)
(
"Source:"
,
source
)
(
"nmean aggregation result:"
)
(
output
)
The output is:
输入:
tensor([[ 0.3456, -0.1234, 0.5678],
[-0.5678, 0.1234, -0.6789],
[ 0.7890, -0.3456, 0.1234]])
索引: tensor([0, 0, 0])
源: tensor([1., 2., 3.])
mean aggregated result:
tensor([[ 2.5237, 0.2931, 0.3374],
[-0.5678, 0.1234, -0.6789],
[ 0.7890, -0.3456, 0.1234]])
Example
import
torch
Test different aggregation modes
input
=
torch.
ones
(
3
)
index
=
torch.
tensor
(
[
0
,
0
,
0
]
)
source
=
torch.
tensor
(
[
2.0
,
4.0
,
6.0
]
)
prod aggregation
output_prod
=
torch.
index_reduce
(
input
,
0
,
index
,
source
,
reduce
=
'prod'
)
(
"prod aggregation:"
,
output_prod
)
amax aggregation
output_max
=
torch.
index_reduce
(
input
,
0
,
index
,
source
,
reduce
=
'amax'
)
(
"amax aggregation:"
,
output_max
)
amin aggregation
output_min
=
torch.
index_reduce
(
input
,
0
,
index
,
source
,
reduce
=
'amin'
)
(
"amin aggregation:"
,
output_min
)
The output is:
prod 聚合: tensor([48., 1., 1.])
amax 聚合: tensor([7., 1., 1.])
amin 聚合: tensor([3., 1., 1.])
Example
import
torch
The include_self parameter
input
=
torch.
tensor
(
[
1.0
,
10.0
,
100.0
]
)
index
=
torch.
tensor
(
[
0
,
0
]
)
source
=
torch.
tensor
(
[
5.0
,
5.0
]
)
Include self values (default)
output1
=
torch.
index_reduce
(
input
,
0
,
index
,
source
,
reduce
=
'mean'
,
include_self
=
True
)
(
"include_self=True:"
,
output1
)
Do not include self values
output2
=
torch.
index_reduce
(
input
,
0
,
index
,
source
,
reduce
=
'mean'
,
include_self
=
False
)
(
"include_self=False:"
,
output2
)
The output is:
include_self=True: tensor([ 3.6667, 10.0000, 100.0000])
include_self=False: tensor([ 5., 10., 100.])
Example
import
torch
Application to 2D tensors
input
=
torch.
zeros
(
3
,
4
)
index
=
torch.
tensor
(
[
0
,
2
,
2
]
)
source
=
torch.
randn
(
3
,
4
)
mean aggregation
output
=
torch.
index_reduce
(
input
,
dim
=
0
,
index
=
index
,
source
=
source
,
reduce
=
'mean'
)
(
"Input shape:"
,
input
.
shape
)
(
"Indices:"
,
index
)
(
"Source shape:"
,
source.
shape
)
(
"Result shape:"
,
output.
shape
)
(
"nResult:"
)
(
output
)
Note:torch.index_reduce Does not modify the original input tensor; instead, it returns a new tensor. Multiple indices can point to the same position, and values are aggregated in the specified way.include_self=False Useful in some scenarios, where you can exclude the original value and aggregate only the newly added values.