Fastest way of converting a real number to a one-hot vector representing a bin?

I’ve got a batch of real numbers and list of values representing bins. I want to find the closest matching bin value for each real number, and convert that real number into a one-hot vector for that bin (resulting in a batch of one-hot vectors). I want to be able to do this on the fly (i.e. not preprocess the real values into one-hot vectors before training). If this were in NumPy, I would use something like searchsorted (since the bin value vector is already sorted). Unfortunately, PyTorch doesn’t seem to have an equivalent function yet. I can use a broadcasted version of something like this answer on SO, but this assumes no sorting, and probably will take much longer to run than is necessary. Any thoughts on a better alternative? Thanks!

Here’s what I’ve come up with to use until a faster method is suggested:

def indexes_to_one_hot(indexes, n_dims=None):
    """Converts a vector of indexes to a batch of one-hot vectors. """
    indexes = indexes.type(torch.int64).view(-1, 1)
    n_dims = n_dims if n_dims is not None else int(torch.max(indexes)) + 1
    one_hots = torch.zeros(indexes.size()[0], n_dims).scatter_(1, indexes, 1)
    one_hots = one_hots.view(*indexes.shape, -1)
    return one_hots


def real_number_batch_to_one_hot_vector_bins(real_numbers, bins):
    """Converts a batch of real numbers to a batch of one hot vectors for the bins the real numbers fall in."""
    _, indexes = (real_numbers.view(-1, 1) - bins.view(1, -1)).abs().min(dim=1)
    return indexes_to_one_hot(indexes, n_dims=bins.shape[0])

Where the bin is represented by it’s middle value.