torch.Tensor.index_copy_#
- Tensor.index_copy_(dim, index, tensor) Tensor#
將
tensor的元素複製到self張量中,透過在index中給定的順序選擇索引。例如,如果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.]])