torch.bincount#
- torch.bincount(input, weights=None, minlength=0) Tensor#
計算非負整數陣列中每個值的頻率。
直方圖的 bin 數量(大小為 1)比 `input` 中的最大值大一,除非 `input` 為空,此時結果是一個大小為 0 的張量。如果指定了 `minlength`,則 bin 的數量至少為 `minlength`,並且如果 `input` 為空,則結果是大小為 `minlength` 且填充了零的張量。如果 `n` 是位置 `i` 處的值,則 `out[n] += weights[i]`(如果指定了 `weights`),否則 `out[n] += 1`。
注意
此操作在使用 CUDA 裝置上的張量時可能會產生非確定性梯度。有關更多資訊,請參閱 可復現性。
- 引數
- 返回
如果 `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])