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
為非空,則形狀為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])