torch.bucketize¶
- torch.bucketize(input, boundaries, *, out_int32=False, right=False, out=None) 张量 ¶
返回输入
input
中每个值所属的桶的索引,其中桶的边界由boundaries
设置。返回一个与input
大小相同的新张量。如果right
为False(默认),则左边界是开放的。请注意,此行为与numpy.digitize的行为相反。 更正式地说,返回的索引满足以下规则:right
返回的索引满足
假
boundaries[i-1] < input[m][n]...[l][x] <= boundaries[i]
真
boundaries[i-1] <= input[m][n]...[l][x] < boundaries[i]
- Parameters
- Keyword Arguments
out_int32 (bool, 可选) – 指示输出数据类型。如果为True,则为torch.int32,否则为torch.int64。 默认值为False,即默认输出数据类型为torch.int64。
right (bool, 可选) – 如果为False,返回找到的第一个合适的位置。如果为True,返回最后一个这样的索引。如果没有找到合适的索引,返回0表示非数值(例如nan、inf)或
boundaries
的大小(超过最后一个索引)。 换句话说,如果为False,则为input
中的每个值从boundaries
获取下界索引。如果为True,则获取上界索引。 默认值为False。输出 (张量, 可选) – 输出张量,如果提供的话,必须与
输入
的大小相同。
示例:
>>> 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]])