評價此頁

torch.mode#

torch.mode(input, dim=-1, keepdim=False, *, out=None)#

返回一個命名元組 (values, indices),其中 valuesinput 張量在給定維度 dim 下每行的眾數(即在該行中出現次數最多的值),而 indices 是找到的每個眾數的位置索引。

預設情況下,diminput 張量的最後一個維度。

如果 keepdimTrue,則輸出張量的大小與 input 相同,只是在 dim 維度上大小為 1。否則,dim 會被壓縮(參見 torch.squeeze()),導致輸出張量比 input 少一個維度。

注意

該函式尚未為 torch.cuda.Tensor 定義。

引數
  • input (Tensor) – 輸入張量。

  • dim (int, optional) – 要約簡的維度。

  • keepdim (bool, optional) – 輸出張量是否保留 dim。預設為 False

關鍵字引數

out (tuple, optional) – the result tuple of two output tensors (values, indices)

示例

>>> b = torch.tensor([[0, 0, 0, 2, 0, 0, 2],
...                   [0, 3, 0, 0, 2, 0, 1],
...                   [2, 2, 2, 0, 0, 0, 3],
...                   [2, 2, 3, 0, 1, 1, 0],
...                   [1, 1, 0, 0, 2, 0, 2]])
>>> torch.mode(b, 0)
torch.return_types.mode(
values=tensor([0, 2, 0, 0, 0, 0, 2]),
indices=tensor([1, 3, 4, 4, 2, 4, 4]))