torch.Tensor.index_copy_¶
- Tensor.index_copy_(dim, index, tensor) Tensor ¶
Copies the elements of
tensor
into theself
tensor by selecting the indices in the order given inindex
. For example, ifdim == 0
andindex[i] == j
, then thei
th row oftensor
is copied to thej
th row ofself
.The
dim
th dimension oftensor
must have the same size as the length ofindex
(which must be a vector), and all other dimensions must matchself
, or an error will be raised.Note
If
index
contains duplicate entries, multiple elements fromtensor
will be copied to the same index ofself
. The result is nondeterministic since it depends on which copy occurs last.- Parameters:
Example:
>>> x = torch.zeros(5, 3) >>> t = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=torch.float) >>> index = torch.tensor([0, 4, 2]) >>> x.index_copy_(0, index, t) tensor([[ 1., 2., 3.], [ 0., 0., 0.], [ 7., 8., 9.], [ 0., 0., 0.], [ 4., 5., 6.]])