PyTorch torch.index_add function
PyTorch torch reference manual
torch.index_add A function in PyTorch for adding the values of a source tensor to specified index positions. It adds along a specified dimension dim, in index at the specified index positions source value.
Function definition
torch.index_add(input, dim, index, source, *, alpha=1)
Parameters:
input(Tensor): the input tensor.dim(int): The dimension to index along.index(Tensor): A 1D integer tensor specifying the positions to add to.source(Tensor): The source tensor, the values to add.alpha(float, optional): A scaling factor for source, defaulting to 1.
Return Value:
torch.Tensor: Returns the modified tensor.
Usage examples
Example
import
torch
Create an input tensor
input
=
torch.
randn
(
4
,
5
)
Create the indices and source
index
=
torch.
tensor
(
[
0
,
2
,
3
]
)
source
=
torch.
randn
(
3
,
5
)
Add along dim=0
output
=
torch.
index_add
(
input
,
dim
=
0
,
index
=
index
,
source
=
source
)
(
"Input shape:"
,
input
.
shape
)
(
"Indices:"
,
index
)
(
"Source shape:"
,
source.
shape
)
(
"Result shape:"
,
output.
shape
)
(
"nResult:"
)
(
output
)
The output is:
输入形状: torch.Size([4, 5])
索引: tensor([0, 2, 3])
源形状: torch.Size([3, 5])
结果形状: torch.Size([4, 5])
结果:
tensor([[ 1.8435, 0.3463, -0.1024, 0.5678, 0.1234],
[-0.5678, 0.8901, -0.2345, 0.6789, -0.1234],
[ 2.3456, 0.4567, 0.7890, -0.3456, 0.5678],
[-0.7890, 1.2345, 0.3456, -0.8901, 0.2345]])
Example
import
torch
Use the alpha parameter to scale the source
input
=
torch.
zeros
(
5
)
index
=
torch.
tensor
(
[
0
,
2
,
4
]
)
source
=
torch.
tensor
(
[
10
,
20
,
30
]
)
alpha=2 means adding source * 2
output
=
torch.
index_add
(
input
,
dim
=
0
,
index
=
index
,
source
=
source
,
alpha
=
2
)
(
"Input:"
,
input
)
(
"Source:"
,
source
)
(
"Result with alpha=2:"
,
output
)
The output is:
输入: tensor([0., 0., 0., 0., 0.])
源: tensor([10., 20., 30.])
alpha=2 后的结果: tensor([20., 0., 40., 0., 60.])
Example
import
torch
Add along other dimensions
input
=
torch.
zeros
(
3
,
4
,
5
)
index
=
torch.
tensor
(
[
1
,
3
]
)
source
=
torch.
randn
(
2
,
4
,
5
)
Add along dim=1
output
=
torch.
index_add
(
input
,
dim
=
1
,
index
=
index
,
source
=
source
)
(
"Input shape:"
,
input
.
shape
)
(
"Index shape:"
,
index.
shape
)
(
"Source shape:"
,
source.
shape
)
(
"Result shape:"
,
output.
shape
)
The output is:
输入形状: torch.Size([3, 4, 5])
索引形状: torch.Size([2])
源形状: torch.Size([2, 4, 5])
结果形状: torch.Size([3, 4, 5])
Example
import
torch
Application in neural networks: the attention mechanism
Suppose there are multiple key-value pairs that need to be aggregated onto a query
Simulate the query and key-value
num_queries
=
2
num_kv
=
4
dim
=
3
The indices of the query
query_idx
=
torch.
tensor
(
[
0
,
1
]
)
The corresponding values
values
=
torch.
randn
(
num_queries
,
dim
)
10
Output
output
=
torch.
zeros
(
num_kv
,
dim
)
Add the values to the corresponding positions
output
=
torch.
index_add
(
output
,
dim
=
0
,
index
=
query_idx
,
source
=
values
)
(
"Query indices:"
,
query_idx
)
(
"Values:"
,
values
)
(
"Aggregated result:"
,
output
)
Note:torch.index_add Does not modify the original input tensor; instead, it returns a new tensor. If index contains duplicate indices, the values will be accumulated.alpha The parameter can be used to scale the source values.