PyTorch torch.scatter_reduce function
PyTorch torch reference manual
torch.scatter_reduce is a function in PyTorch used to aggregate values from a source tensor at specified positions in a specified way. It supports multiple aggregation modes such as sum, product, maximum, minimum, and so on.
Function definition
torch.scatter_reduce(input, dim, index, src, reduce='sum', *, include_self=True)
Parameters:
input(Tensor): the input tensor.dim(int): the dimension along which to aggregate.index(Tensor): index tensor specifying where to aggregate the values of src into input.src(Tensor): The source tensor, the values to aggregate.reduce(str): aggregation mode, optional values are 'sum', 'prod', 'mean', 'amax', 'amin', 'multiply'. Defaults to 'sum'.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.
ones
(
3
,
5
)
Create the indices and source
index
=
torch.
tensor
(
[
[
0
,
1
,
2
,
0
,
0
]
,
[
1
,
2
,
0
,
1
,
2
]
,
[
2
,
0
,
1
,
2
,
0
]
]
)
src
=
torch.
tensor
(
[
[
1
,
1
,
1
,
1
,
1
]
,
[
2
,
2
,
2
,
2
,
2
]
,
[
3
,
3
,
3
,
3
,
3
]
]
)
Aggregate using sum
output
=
torch.
scatter_reduce
(
input
,
dim
=
0
,
index
=
index
,
src
=
src
,
reduce
=
'sum'
)
(
"Input:"
)
(
input
)
(
"nAggregation mode: sum"
)
(
"Result:"
)
(
output
)
The output is:
输入:
tensor([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
聚合方式: sum
结果:
tensor([[4., 2., 3., 4., 4.],
[2., 2., 2., 2., 2.],
[3., 3., 2., 3., 3.]])
Example
import
torch
Test different aggregation modes
input
=
torch.
zeros
(
3
,
3
)
index
=
torch.
tensor
(
[
[
0
,
0
,
0
]
,
[
1
,
1
,
1
]
,
[
2
,
2
,
2
]
]
)
src
=
torch.
tensor
(
[
[
2
,
3
,
4
]
,
[
5
,
6
,
7
]
,
[
8
,
9
,
10
]
]
)
Use prod (product)
output_prod
=
torch.
scatter_reduce
(
input
,
0
,
index
,
src
,
reduce
=
'prod'
)
(
"prod aggregation:"
)
(
output_prod
)
Use amax (maximum)
output_max
=
torch.
scatter_reduce
(
input
,
0
,
index
,
src
,
reduce
=
'amax'
)
(
"namax aggregation:"
)
(
output_max
)
Use amin (minimum)
output_min
=
torch.
scatter_reduce
(
input
,
0
,
index
,
src
,
reduce
=
'amin'
)
(
"namin aggregation:"
)
(
output_min
)
The output is:
prod 聚合:
tensor([[ 2., 3., 4.],
[ 5., 6., 7.],
[ 8., 9., 10.]])
amax 聚合:
tensor([[ 2., 3., 4.],
[ 5., 6., 7.],
[ 8., 9., 10.]])
amin 聚合:
tensor([[ 2., 3., 4.],
[ 5., 6., 7.],
[ 8., 9., 10.]])
Example
import
torch
Use the include_self parameter
input
=
torch.
tensor
(
[
1.0
,
2.0
,
3.0
]
)
index
=
torch.
tensor
(
[
0
,
0
,
0
]
)
src
=
torch.
tensor
(
[
10.0
,
20.0
,
30.0
]
)
Include self values (default)
output1
=
torch.
scatter_reduce
(
input
,
0
,
index
,
src
,
reduce
=
'sum'
,
include_self
=
True
)
(
"include_self=True:"
,
output1
)
Do not include self values
output2
=
torch.
scatter_reduce
(
input
,
0
,
index
,
src
,
reduce
=
'sum'
,
include_self
=
False
)
(
"include_self=False:"
,
output2
)
The output is:
include_self=True: tensor([66., 2., 3.])
include_self=False: tensor([60., 2., 3.])
Note:torch.scatter_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, such as avoiding double-counting a node's own features in graph neural networks.