0

I am working with a batched interpolation class where a typical task is to find the knot index for each evaluation point in the batch. This sounds like a job for torch.bucketize, but the knot vector is not strictly increasing but has repeated vals. See the example below.

The documentation of torch.bucketize states for the boundary argument: "...must contain a strictly increasing sequence, or the return value is undefined."

So is there a similar function that can be used instead? Or is the documentation not clear enough at this point and instead, only the returned indices on the left and right extreme can be incorrect?

import torch

if __name__ == "__main__":
    # Repeated knots at start and end (typical for splines)
    boundaries = torch.tensor([0.0, 0.0, 0.0, 0.0, 0.5, 1.0, 1.0, 1.0, 1.0])

    # Evaluation points around boundaries and middle
    x_test = torch.tensor([-0.1, 0.0, 0.25, 0.5, 0.75, 1.0, 1.1], dtype=torch.float64) 
        
    indices = torch.bucketize(x_test, boundaries, right=False)

    print("Knots:    ", knots.tolist())
    print("x values: ", x_test.tolist())
    print("Indices:  ", indices.tolist())
1
  • How do the end repeats impact the downstream use case? And is the boundaries tensor always strictly increasing aside from the end repeats? For example could you do a pass through boundaries to remove the end repeats to yield [0.0, 0.5, 1.0] for the purpose of the bucketize call and still get the correct result? Commented Jun 20 at 18:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.