torch.take_along_dim¶
- torch.take_along_dim(input, indices, dim=None, *, out=None) Tensor¶
沿著給定的
dim,根據indices中的一維索引從input中選取值。如果
dim為 None,則將輸入陣列視為已展平為一維。返回沿某個維度的索引的函式,例如
torch.argmax()和torch.argsort(),被設計為與此函式一起使用。請參見下面的示例。注意
此函式類似於 NumPy 的 take_along_axis。另請參閱
torch.gather()。- 引數
- 關鍵字引數
out (Tensor, optional) – 輸出張量。
示例
>>> t = torch.tensor([[10, 30, 20], [60, 40, 50]]) >>> max_idx = torch.argmax(t) >>> torch.take_along_dim(t, max_idx) tensor([60]) >>> sorted_idx = torch.argsort(t, dim=1) >>> torch.take_along_dim(t, sorted_idx, dim=1) tensor([[10, 20, 30], [40, 50, 60]])