YouTip LogoYouTip

Pytorch Torch Bucketize

--

PyTorch Tutorial

PyTorch TutorialPyTorch IntroductionPyTorch InstallationPyTorch BasicsPyTorch TensorsPyTorch Neural Networks BasicsPyTorch First Neural NetworkPyTorch Data Processing and LoadingPyTorch Linear RegressionPyTorch Convolutional Neural NetworksPyTorch Recurrent Neural NetworksPyTorch DatasetsPyTorch Data TransformationsPyTorch torchPyTorch torch.nnTransformer ModelPyTorch TransformerPyTorch torch.optimPyTorch torchvisionPyTorch Model DeploymentPyTorch Model Saving and LoadingPyTorch Image ClassificationPyTorch Text Sentiment AnalysisPyTorch AutogradPyTorch GPU / CUDA AccelerationPyTorch Loss FunctionsPyTorch Learning Rate SchedulerPyTorch Transfer LearningPyTorch Batch NormalizationPyTorch LSTM / GRUPyTorch EmbeddingPyTorch GANPyTorch AutoencoderPyTorch Model Evaluation and DebuggingPyTorch torchtextPyTorch Mixed Precision TrainingPyTorch TorchScript/ONNX ExportPyTorch Distributed TrainingPyTorch Attention Mechanism

PyTorch Data TransformationsPyTorch Data Transformations

PyTorch torch.nn Reference ManualPyTorch torch.nn Reference Manual

PyTorch torch.bucketize Function


PyTorch torch Reference ManualPyTorch torch Reference Manual

torch.bucketize is a PyTorch function for bucket sort indexing. It maps input values to corresponding bucket indices based on boundary arrays using binary search. Commonly used for discretizing continuous values or binning operations.

Function Definition

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

Usage Examples

Example

import torch

# Basic usage: Bucket Sort Index

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

# Multi-dimensional input
values = torch.tensor([[0.5,1.5],[2.5,3.5]])

result = torch.bucketize(values, boundaries)

print("Multi-dimensional Input Result:", result)

PyTorch torch.nn Reference ManualPyTorch torch.nn Reference Manual

PyTorch Data TransformationsPyTorch Data Transformations

← Pytorch Torch Cartesian_ProdPytorch Torch Broadcast_Tensor β†’