torch.bucketize¶
- torch.bucketize(input, boundaries, *, out_int32=False, right=False, out=None) Tensor ¶
傳回
input
中每個值所屬的 bucket 的索引,其中 bucket 的邊界由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]
- 參數
- 關鍵字參數
out_int32 (bool, optional) – 指示輸出資料類型。 如果為 True,則為 torch.int32,否則為 torch.int64。 預設值為 False,即預設輸出資料類型為 torch.int64。
right (bool, optional) – 如果為 False,則傳回找到的第一個合適的位置。 如果為 True,則傳回最後一個此類索引。 如果找不到合適的索引,則對於非數值 (例如 nan, inf) 傳回 0,或傳回
boundaries
的大小(超過最後一個索引一個位置)。 換句話說,如果為 False,則從boundaries
取得input
中每個值的下界索引。 如果為 True,則改為取得上界索引。 預設值為 False。out (Tensor, optional) – 輸出張量,如果提供,則必須與
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]])