快捷方式

torch.Tensor.index_copy_

Tensor.index_copy_(dim, index, tensor) Tensor

透過選取 index 中給定的順序的索引,將 tensor 的元素複製到 self 張量中。 例如,如果 dim == 0index[i] == j,則 tensor 的第 i 行會複製到 self 的第 j 行。

dim 維度 tensor 的大小必須與 index (必須是向量) 的長度相同,並且所有其他維度必須與 self 匹配,否則會引發錯誤。

注意

如果 index 包含重複的條目,則來自 tensor 的多個元素將被複製到 self 的相同索引。 結果是不確定的,因為它取決於哪個副本最後發生。

參數
  • dim (int) – 索引的維度

  • index (LongTensor) – 要從 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 的綜合開發者文件

查看文件

教學課程

獲取針對初學者和高級開發者的深入教學課程

查看教學課程

資源

尋找開發資源並獲取您的問題解答

查看資源