PyTorch torch.index_copy function
PyTorch torch reference manual
torch.index_copy A function in PyTorch for copying a source tensor to specified index positions. It copies along a specified dimension dim, in index at the specified index positions source value.
The difference between torch.index_add The difference is thatindex_copy it overwrites rather than accumulates.
Function definition
torch.index_copy(input, dim, index, source)
Parameters:
input(Tensor): the input tensor.dim(int): The dimension to index along.index(Tensor): A 1D integer tensor specifying the positions to copy to.source(Tensor): The source tensor, the values to copy.
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
)
Copy along dim=0
output
=
torch.
index_copy
(
input
,
dim
=
0
,
index
=
index
,
source
=
source
)
(
"Input:"
)
(
input
)
(
"nSource:"
)
(
source
)
(
"nResult after copying to positions [0, 2, 3]:"
)
(
output
)
The output is:
输入:
tensor([[ 0.3456, -0.1234, 0.5678, -0.2345, 0.8901],
[-0.5678, 0.1234, -0.6789, 0.2345, -0.1234],
[ 0.7890, -0.3456, 0.1234, -0.5678, 0.3456],
[-0.1234, 0.4567, -0.8901, 0.6789, -0.5678]])
源:
tensor([[-1.2345, 0.5678, -1.2345, 0.5678, -1.2345],
[ 1.5678, -0.6789, 1.5678, -0.6789, 1.5678],
[-0.8901, 1.2345, -0.8901, 1.2345, -0.8901]])
The result after copying to position [0, 2, 3]:
tensor([[-1.2345, 0.5678, -1.2345, 0.5678, -1.2345],
[-0.5678, 0.1234, -0.6789, 0.2345, -0.1234],
[ 1.5678, -0.6789, 1.5678, -0.6789, 1.5678],
[-0.8901, 1.2345, -0.8901, 1.2345, -0.8901]])
Example
import
torch
Copy along dim=1
input
=
torch.
zeros
(
3
,
5
)
index
=
torch.
tensor
(
[
1
,
3
]
)
source
=
torch.
tensor
(
[
[
10
,
20
,
30
,
40
,
50
]
,
[
60
,
70
,
80
,
90
,
100
]
]
)
output
=
torch.
index_copy
(
input
,
dim
=
1
,
index
=
index
,
source
=
source
)
(
"Input:"
)
(
input
)
(
"nSource:"
)
(
source
)
(
"nResult after copying to positions [1, 3]:"
)
(
output
)
The output is:
输入:
tensor([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])
源:
tensor([[ 10., 20., 30., 40., 50.],
[ 60., 70., 80., 90., 100.]])
Result after copying to position [1, 3]:
tensor([[ 0., 10., 0., 20., 0.],
[ 0., 60., 0., 70., 0.],
[ 0., 0., 0., 0., 0.]])
Example
import
torch
Build a large tensor
Suppose you need to merge the results of several small batches into one large batch
The target tensor
batch_size
=
8
feature_dim
=
4
output
=
torch.
zeros
(
batch_size
,
feature_dim
)
Simulate the results of several small batches
mini_batches
=
[
torch.
randn
(
2
,
feature_dim
)
,
torch.
randn
(
3
,
feature_dim
)
,
torch.
randn
(
1
,
feature_dim
)
]
The positions where each batch should be placed
indices
=
[
0
,
2
,
5
]
Copy each batch in turn
for
idx
,
batch
in
zip
(
indices
,
mini_batches
)
:
Create indices of the corresponding size
index
=
torch.
arange
(
idx
,
idx +
len
(
batch
)
)
output
=
torch.
index_copy
(
output
,
dim
=
0
,
index
=
index
,
source
=
batch
)
(
"Final output shape:"
,
output.
shape
)
(
output
)
The output is:
最终输出形状: torch.Size([8, 4])
tensor([[ 0.1234, -0.5678, 0.8901, -0.2345],
[ 0.6789, -0.1234, -0.5678, 0.3456],
[ 1.2345, -0.8901, 0.1234, -0.6789],
[-0.3456, 0.5678, -0.1234, 0.8901],
[ 1.5678, -0.2345, 0.6789, -0.1234],
[-0.8901, 0.3456, 0.5678, -0.8901],
[ 0.0000, 0.0000, 0.0000, 0.0000],
[ 0.0000, 0.0000, 0.0000, 0.0000]])
Note:torch.index_copy The original input tensor is not modified; instead, a new tensor is returned.index_copy is an overwrite operation, unlike torch.index_add which accumulates values.