Pytorch Torch Index_Copy
* * *
[ Pytorch torch Reference Manual](https://example.com/pytorch/pytorch-torch-ref.html)
`torch.index_copy` is a function in PyTorch used to copy values from a source tensor into specified index positions. It copies the values of `source` into the `input` tensor along dimension `dim` at the indices specified by `index`.
Unlike `torch.index_add`, `index_copy` performs an overwrite operation rather than accumulation.
### Function Definition
torch.index_copy(input, dim, index, source)
**Parameters**:
* `input` (Tensor): The input tensor.
* `dim` (int): The dimension along which to index.
* `index` (Tensor): A one-dimensional integer tensor specifying the indices to copy to.
* `source` (Tensor): The source tensor containing values to be copied.
**Returns**:
* `torch.Tensor`: Returns the modified tensor.
* * *
## Usage Examples
## Example
import torch
# Create input tensor
input= torch.randn(4,5)
# Create index 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)
print("Input:")
print(input)
print("nSource:")
print(source)
print("nResult after copying to positions [0, 2, 3]:")
print(output)
Output result:
Input: 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]])Source: 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]])Result after copying to positions [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)
print("Input:")
print(input)
print("nSource:")
print(source)
print("nResult after copying to positions [1, 3]:")
print(output)
Output result:
Input: tensor([[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.]])Source: tensor([[ 10., 20., 30., 40., 50.], [ 60., 70., 80., 90., 100.]])Result after copying to positions [1, 3]: tensor([[ 0., 10., 0., 20., 0.], [ 0., 60., 0., 70., 0.], [ 0., 0., 0., 0., 0.]])
## Example
import torch
# Construct large tensor
# Assume we need to merge results from multiple small batches into one large batch
# Target tensor
batch_size =8
feature_dim =4
output = torch.zeros(batch_size, feature_dim)
# Simulate results from multiple small batches
mini_batches =[
torch.randn(2, feature_dim),
torch.randn(3, feature_dim),
torch.randn(1, feature_dim)
]
# Positions where each batch should be placed
indices =[0,2,5]
# Copy each batch sequentially
for idx, batch in zip(indices, mini_batches):
# Create index of corresponding size
index = torch.arange(idx, idx + len(batch))
output = torch.index_copy(output, dim=0, index=index, source=batch)
print("Final output shape:", output.shape)
print(output)
Output result:
Final output shape: 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` does not modify the original input tensor; instead, it returns a new tensor. `index_copy` is an overwrite operation, different from the accumulation behavior of `torch.index_add`.
* * *
[ Pytorch torch Reference Manual](https://example.com/pytorch/pytorch-torch-ref.html)
YouTip