torch.Tensor.index_copy_¶
- Tensor.index_copy_(dim, index, tensor) Tensor¶
透過選擇
index中給定的順序的索引,將tensor的元素複製到selftensor 中。例如,如果dim == 0且index[i] == j,則tensor的第i行將被複制到self的第j行。tensor在第dim維度上的大小必須與index的長度相同(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.]])