捷徑

torch.mode

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

傳回一個 namedtuple (values, indices),其中 values 是給定維度 diminput 張量每一列的眾數(mode)值,也就是在該列中出現最頻繁的值,而 indices 是找到的每個眾數值的索引位置。

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

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

注意

此函數尚未針對 torch.cuda.Tensor 定義。

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

  • dim (int) – 要縮減的維度。

  • keepdim (bool) – 輸出張量是否保留 dim

關鍵字參數

out (tuple, optional) – 兩個輸出張量 (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]))

文件

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources