PyTorch torch.scatter_add function
PyTorch torch reference manual
torch.scatter_add is a function in PyTorch used to add values from a source tensor to specified positions. It adds src values according to index specified positions to input it.
Function definition
torch.scatter_add(input, dim, index, src)
Parameters:
input(Tensor): the input tensor.dim(int): the dimension along which to scatter.index(Tensor): index tensor specifying where to add the values of src to input.src(Tensor): The source tensor, the values to add.
Return Value:
torch.Tensor: Returns the modified tensor.
Usage examples
Example
import
torch
Create an input tensor
input
=
torch.
zeros
(
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
]
]
)
Scatter and accumulate along dim=0
output
=
torch.
scatter_add
(
input
,
dim
=
0
,
index
=
index
,
src
=
src
)
(
"Input:"
)
(
input
)
(
"nIndices:"
)
(
index
)
(
"nSource:"
)
(
src
)
(
"nResult:"
)
(
output
)
The output is:
输入:
tensor([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])
索引:
tensor([[0, 1, 2, 0, 0],
[1, 2, 0, 1, 2],
[2, 0, 1, 2, 0]])
源:
tensor([[1., 1., 1., 1., 1.],
[2., 2., 2., 2., 2.],
[3., 3., 3., 3., 3.]])
结果:
tensor([[4., 1., 2., 4., 4.],
[2., 1., 2., 2., 2.],
[3., 3., 1., 3., 3.]])
Example
import
torch
Use dim=1
input
=
torch.
zeros
(
3
,
5
)
index
=
torch.
tensor
(
[
[
0
,
1
,
2
,
1
,
0
]
,
[
1
,
2
,
0
,
2
,
1
]
,
[
0
,
1
,
1
,
0
,
2
]
]
)
src
=
torch.
arange
(
1
,
6
)
.
float
(
)
output
=
torch.
scatter_add
(
input
,
dim
=
1
,
index
=
index
,
src
=
src
)
(
"Scatter along dim=1:"
)
(
output
)
The output is:
沿 dim=1 散布:
tensor([[ 6., 3., 3., 0., 0.],
[ 3., 6., 3., 0., 0.],
[ 2., 4., 5., 0., 0.]])
Example
import
torch
Use cases for aggregating values at multiple positions
For example, accumulating features of neighboring nodes in graph neural networks
Simulate initial features of 4 nodes
node_features
=
torch.
zeros
(
4
,
3
)
Simulate edge connections (source node points to target node)
edge_index
=
torch.
tensor
(
[
0
,
1
,
2
,
3
,
0
,
1
]
)
Source nodes of the edges
edge_weights
=
torch.
tensor
(
[
1.0
,
2.0
,
3.0
,
1.5
,
2.5
,
0.5
]
)
Create weighted values of source node features for each edge
src_features
=
torch.
randn
(
6
,
3
)
- edge_weights.
unsqueeze
(
1
)
Accumulate features into target nodes (simplified here; in practice the target node of each edge is needed)
target_nodes
=
torch.
tensor
(
[
0
,
0
,
1
,
1
,
2
,
3
]
)
index
=
target_nodes
output
=
torch.
scatter_add
(
node_features
,
0
,
index.
unsqueeze
(
1
)
.
expand_as
(
src_features
)
,
src_features
)
(
"Node feature shape:"
,
node_features.
shape
)
(
"Features after accumulation:"
,
output
)
Note:torch.scatter_add does not modify the original input tensor but returns a new tensor. Multiple indices can point to the same position, and values will be accumulated. This function is the torch.gather inverse operation.