torch.Tensor.index_copy_
- Tensor.index_copy_(dim, index, tensor) → Tensor
-
根据
index
中给定的顺序选择索引,将tensor
中的元素复制到self
张量中。例如,如果dim == 0
且index[i] == j
,那么tensor
的第i
行将被复制到self
张量的第j
行。在
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.]])