torch.bincount
- torch.bincount(input, weights=None, minlength=0) → Tensor
-
统计非负整数数组中每个值出现的次数。
箱数(大小为 1)比
input
中的最大值大 1,除非input
是空的,在这种情况下结果是一个大小为 0 的张量。如果指定了minlength
,则箱数至少为minlength
;若input
空,则结果是大小为minlength
并填充零的张量。对于位置i
的值n
,如果指定了weights
,则执行out[n] += weights[i]
;否则执行out[n] += 1
。注意
当给定的张量位于CUDA设备上时,此操作可能会产生非确定性的梯度。更多详细信息请参见重现性。
- 参数
- 返回值
-
如果
input
不为空,则返回一个形状为Size([max(input) + 1])
的张量;否则返回Size(0)
- 返回类型
-
输出(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])