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])