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