torch.bucketize#
- torch.bucketize(input, boundaries, *, out_int32=False, right=False, out=None) Tensor#
返回
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]- 引數
- 關鍵字引數
示例
>>> 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]])