torch.Tensor.index_copy_#
- Tensor.index_copy_(dim, index, tensor) Tensor #
将
tensor
中的元素按index
提供的顺序复制到self
张量中。例如,如果dim == 0
且index[i] == j
,则tensor
的第i
行将被复制到self
的第j
行。tensor
的dim
维的大小必须与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.]])