評價此頁

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]

引數
  • input (TensorScalar) – N 維張量或包含搜尋值的 Scalar。

  • boundaries (Tensor) – 1 維張量,必須包含一個嚴格遞增的序列,否則返回值未定義。

關鍵字引數
  • out_int32 (bool, optional) – 指示輸出資料型別。如果為 True,則為 torch.int32,否則為 torch.int64。預設值為 False,即預設輸出資料型別為 torch.int64。

  • right (bool, optional) – 確定 boundaries 中值的行為。請參見上表。

  • 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]])