快捷方式

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 的完整開發者文件

檢視文件

教程

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

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源