快速鍵

torch.bincount

torch.bincount(input, weights=None, minlength=0) Tensor

計算非負整數陣列中每個值的頻率。

bin (大小為 1 的區間) 的數量比 input 中的最大值大 1,除非 input 為空,在這種情況下,結果會是一個大小為 0 的張量。如果指定了 minlength,則 bin 的數量至少為 minlength,並且如果 input 為空,則結果會是一個大小為 minlength 的張量,並以零填充。如果 n 是位置 i 的值,則若指定了 weights,則 out[n] += weights[i],否則 out[n] += 1

注意

當在 CUDA 裝置上給定張量時,此操作可能會產生非確定性的梯度。 請參閱再現性以獲取更多信息。

參數
  • input (Tensor) – 1 維整數張量

  • weights (Tensor) – 可選項,輸入張量中每個值的權重。 應該與輸入張量的大小相同。

  • minlength (int) – 可選項,最小 bin 數量。 應為非負數。

返回

如果 input 為非空,則形狀為 Size([max(input) + 1]) 的張量,否則為 Size(0)

返回類型

output (Tensor)

範例

>>> input = torch.randint(0, 8, (5,), dtype=torch.int64)
>>> weights = torch.linspace(0, 1, steps=5)
>>> input, weights
(tensor([4, 3, 6, 3, 4]),
 tensor([ 0.0000,  0.2500,  0.5000,  0.7500,  1.0000])

>>> torch.bincount(input)
tensor([0, 0, 0, 2, 2, 0, 1])

>>> input.bincount(weights)
tensor([0.0000, 0.0000, 0.0000, 1.0000, 1.0000, 0.0000, 0.5000])

文件

獲取 PyTorch 的全面開發者文檔

查看文檔

教程

獲取針對初學者和高級開發者的深入教程

查看教程

資源

查找開發資源並獲得問題解答

查看資源