快捷方式

torch.Tensor.index_copy_

Tensor.index_copy_(dim, index, tensor) Tensor

通过选择 index 中给定的顺序的索引,将 tensor 的元素复制到 self tensor 中。例如,如果 dim == 0index[i] == j,则 tensor 的第 i 行将被复制到 self 的第 j 行。

tensor 在第 dim 维度上的大小必须与 index 的长度相同(index 必须是向量),并且所有其他维度必须与 self 匹配,否则将引发错误。

注意

如果 index 包含重复的条目,则 tensor 中的多个元素将被复制到 self 的同一个索引处。由于结果取决于最后发生的复制操作,因此结果是不确定的。

参数
  • dim (int) – 进行索引操作的维度

  • index (LongTensor) – 从 tensor 中选择元素的索引

  • tensor (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.]])

文档

查阅 PyTorch 的完整开发者文档

查看文档

教程

获取针对初学者和高级开发者的深入教程

查看教程

资源

查找开发资源并获得问题解答

查看资源