• 文档 >
  • torch >
  • torch.bincount
快捷键

torch.bincount

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

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

箱子的数量(大小为 1)比 input 中的最大值大 1,除非 input 为空,此时结果为大小为 0 的张量。如果指定了 minlength ,则箱子的数量至少为 minlength ,如果 input 为空,则结果为大小为 minlength 且填充为零的张量。如果 n 是位置 i 的值, out[n] += weights[i] 如果指定了 weights ,否则为 out[n] += 1

注意

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

参数:
  • 输入(张量)- 1 维整型张量

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

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

返回值:

形状为 Size([max(input) + 1]) 如果 input 不为空,则为 Size(0)

返回类型:

输出(张量)

示例:

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

© 版权所有 PyTorch 贡献者。

使用 Sphinx 构建,主题由 Read the Docs 提供。

文档

PyTorch 的全面开发者文档

查看文档

教程

深入了解初学者和高级开发者的教程

查看教程

资源

查找开发资源并获得您的疑问解答

查看资源