Pytorch Torch Scatter_Add
* * 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 the values of `src` to `input` at the positions specified by `index`.
### 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): The index tensor, specifying the positions in `input` to which the values in `src` will be added.
* `src` (Tensor): The source tensor, containing the values to be added.
**Return Value**:
* `torch.Tensor`: Returns the modified tensor.
* * *
## Usage Examples
## Example
import torch
# Create input tensor
input= torch.zeros(3,5)
# Create index 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 add along dim=0
output = torch.scatter_add(input, dim=0, index=index, src=src)
print("Input:")
print(input)
print("nIndex:")
print(index)
print("nSource:")
print(src)
print("nResult:")
print(output)
Output result:
Input: tensor([[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.]])Index: tensor([[0, 1, 2, 0, 0], [1, 2, 0, 1, 2], [2, 0, 1, 2, 0]])Source: tensor([[1., 1., 1., 1., 1.], [2., 2., 2., 2., 2.], [3., 3., 3., 3., 3.]])Result: tensor([[4., 1., 2., 4., 4.], [2., 1., 2., 2., 2.], [3., 3., 1., 3., 3.]])
## Example
import torch
# Using 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)
print("Scatter along dim=1:")
print(output)
Output result:
Scatter along dim=1: tensor([[ 6., 3., 3., 0., 0.], [ 3., 6., 3., 0., 0.], [ 2., 4., 5., 0., 0.]])
## Example
import torch
# Application scenario for aggregating values from multiple positions
# For example, accumulating neighbor node features in graph neural networks
# Simulate initial features of nodes
node_features = torch.zeros(4,3)
# Simulate edge connections (source nodes pointing to target nodes)
edge_index = torch.tensor([0,1,2,3,0,1])# Source nodes of edges
edge_weights = torch.tensor([1.0,2.0,3.0,1.5,2.5,0.5])
# Create weighted source node features for each edge
src_features = torch.randn(6,3) * edge_weights.unsqueeze(1)
# Accumulate features to target nodes (simplified here, actual implementation needs to follow edge target nodes)
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)
print("Node feature shape:", node_features.shape)
print("Accumulated features:", 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 their values will be accumulated. This function is the inverse operation of `torch.gather`.
* * Pytorch torch Reference Manual](#)
YouTip