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