快捷方式

torch.bincount

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

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

除非 input 為空,否則 bin 的數量(大小為 1)比 input 中的最大值大一;如果 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 開發者文件

檢視文件

教程

獲取面向初學者和高階開發者的深度教程

檢視教程

資源

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

檢視資源