Shortcuts

torch.bucketize

torch.bucketize(input, boundaries, *, out_int32=False, right=False, out=None)Tensor

Returns the indices of the buckets to which each value in the input belongs, where the boundaries of the buckets are set by boundaries. Return a new tensor with the same size as input. If right is False (default), then the left boundary is closed. More formally, the returned index satisfies the following rules:

right

returned index satisfies

False

boundaries[i-1] < input[m][n]...[l][x] <= boundaries[i]

True

boundaries[i-1] <= input[m][n]...[l][x] < boundaries[i]

Parameters
  • input (Tensor or Scalar) – N-D tensor or a Scalar containing the search value(s).

  • boundaries (Tensor) – 1-D tensor, must contain a monotonically increasing sequence.

Keyword Arguments
  • out_int32 (bool, optional) – indicate the output data type. torch.int32 if True, torch.int64 otherwise. Default value is False, i.e. default output data type is torch.int64.

  • right (bool, optional) – if False, return the first suitable location that is found. If True, return the last such index. If no suitable index found, return 0 for non-numerical value (eg. nan, inf) or the size of boundaries (one pass the last index). In other words, if False, gets the lower bound index for each value in input from boundaries. If True, gets the upper bound index instead. Default value is False.

  • out (Tensor, optional) – the output tensor, must be the same size as input if provided.

Example:

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

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources