torch.Tensor.index_copy_¶
- Tensor.index_copy_(dim, index, tensor) Tensor ¶
将
tensor
中的元素复制到self
张量中,通过选择index
中的索引顺序。例如,如果dim == 0
和index[i] == j
,则i
的第 6#行被复制到j
的第 8#行。第
dim
维度tensor
必须与index
的长度相同(index
必须是一个向量),并且所有其他维度必须与self
匹配,否则将引发错误。注意
如果
index
包含重复项,tensor
的多个元素将被复制到self
的相同索引处。由于它取决于哪个复制操作最后发生,因此结果是不可预测的。- 参数:
dim(int)- 指定索引的维度
index(LongTensor)- 要从中选择的
tensor
的索引张量(Tensor)- 包含要复制的值的张量
示例:
>>> 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.]])