快捷方式

torch.bucketize

torch.bucketize(input, boundaries, *, out_int32=False, right=False, out=None) Tensor

返回 input 中每个值所属桶的索引,桶的边界由 boundaries 设置。返回一个与 input 大小相同的新张量。如果 right 为 False(默认值),则左边界是开放的。请注意,此行为与 numpy.digitize 的行为相反。更正式地,返回的索引满足以下规则:

right

返回的索引满足

False

boundaries[i-1] < input[m][n]...[l][x] <= boundaries[i]

True

boundaries[i-1] <= input[m][n]...[l][x] < boundaries[i]

参数
  • input (TensorScalar) – 包含搜索值(或多个值)的 N-D 张量或标量。

  • boundaries (Tensor) – 1-D 张量,必须包含严格递增的序列,否则返回值是未定义的。

关键字参数
  • out_int32 (bool, 可选) – 指示输出数据类型。如果为 True 则为 torch.int32,否则为 torch.int64。默认值为 False,即默认输出数据类型为 torch.int64。

  • right (bool, 可选) – 决定对于 boundaries 中值的行为。请参阅上面的表格。

  • out (Tensor, 可选) – 输出张量,如果提供,必须与 input 的大小相同。

示例

>>> boundaries = torch.tensor([1, 3, 5, 7, 9])
>>> boundaries
tensor([1, 3, 5, 7, 9])
>>> v = torch.tensor([[3, 6, 9], [3, 6, 9]])
>>> v
tensor([[3, 6, 9],
        [3, 6, 9]])
>>> torch.bucketize(v, boundaries)
tensor([[1, 3, 4],
        [1, 3, 4]])
>>> torch.bucketize(v, boundaries, right=True)
tensor([[2, 3, 5],
        [2, 3, 5]])

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

获取针对初学者和高级开发者的深度教程

查看教程

资源

查找开发资源并获得问题解答

查看资源