Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.bucketize function

PyTorch torch.bucketize function


PyTorch torch reference manual PyTorch torch reference manual

torch.bucketize is a function in PyTorch used for bucket indexing. It maps input values to the corresponding bucket indices using a binary search over the boundary array. It is commonly used to discretize continuous values or for binning.

Function definition

torch.bucketize(input, boundaries, right=False, out_int32=False, **kwargs)


Usage examples

Example

import

torch

Basic usage: Bucket indexing

boundaries

=

torch.

tensor

(

[

0

,

1

,

2

,

3

,

4

]

)

values

=

torch.

tensor

(

[

0.5

,

1.5

,

2.5

,

3.5

,

0.2

,

2.0

]

)

result

=

torch.

bucketize

(

values

,

boundaries

)

print

(

"Boundaries:"

,

boundaries

)

print

(

"Values:"

,

values

)

print

(

"Bucket indices:"

,

result

)

Output: tensor([1, 2, 3, 4, 0, 2])

right=True means the boundaries are right-closed intervals

result_right

=

torch.

bucketize

(

values

,

boundaries

,

right

=

True

)

print

(

"right=True bucket indices:"

,

result_right

)

Output: tensor([0, 1, 2, 3, 0, 1])

Multidimensional input

values

=

torch.

tensor

(

[

[

0.5

,

1.5

]

,

[

2.5

,

3.5

]

]

)

result

=

torch.

bucketize

(

values

,

boundaries

)

print

(

"Multidimensional input result:"

,

result

)


PyTorch torch reference manual PyTorch torch reference manual