評價此頁

torch.Tensor.index_copy_#

Tensor.index_copy_(dim, index, tensor) Tensor#

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

tensordim 維必須與 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.]])