Shortcuts

torch.bincount

torch.bincount(input, weights=None, minlength=0) 张量

计算数组中每个非负整数值的频率。

除非 input 为空,否则箱数(大小为1)比 input 中的最大值大1,在这种情况下,结果是一个大小为0的张量。如果指定了 minlength,则箱数至少为 minlength,如果 input 为空,则结果是大小为 minlength 的张量,并用零填充。如果 n 是位置 i 处的值,则如果指定了 weights,则 out[n] += weights[i],否则 out[n] += 1

注意

当在CUDA设备上给定张量时,此操作可能会产生不确定的梯度。更多信息请参见可重复性

Parameters
  • 输入 (张量) – 1维整数张量

  • weights (Tensor) – 可选,输入张量中每个值的权重。 应与输入张量大小相同。

  • minlength (int) – 可选,最小箱数。应为非负数。

Returns

如果 input 非空,则为形状为 Size([max(input) + 1]) 的张量,否则为 Size(0)

Return type

输出 (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])
优云智算