torch.Tensor.index_copy_¶
- Tensor.index_copy_(dim, index, tensor) Tensor ¶
通过选择
index
中给定的顺序的索引,将tensor
的元素复制到self
tensor 中。例如,如果dim == 0
且index[i] == j
,则tensor
的第i
行将被复制到self
的第j
行。tensor
在第dim
维度上的大小必须与index
的长度相同(index
必须是向量),并且所有其他维度必须与self
匹配,否则将引发错误。注意
如果
index
包含重复的条目,则tensor
中的多个元素将被复制到self
的同一个索引处。由于结果取决于最后发生的复制操作,因此结果是不确定的。示例
>>> 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.]])