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]
- 参数
- 关键字参数
示例
>>> 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]])