1

I have a 3-dimensional numpy array, for example:

 x = [[[0.3, 0.2, 0.5],
       [0.1, 0.2, 0.7],
       [0.2, 0.2, 0.6]]]

The indices array is also 3-dimensional, like:

indices = [[[0],
            [1],
            [2]]]

I expect the output is:

 output= [[[0.3],
           [0.2],
           [0.6]]]

I tried the torch.index_select and torch.gather function, but I couldn't find a right way to deal with the dimension. Thanks for any help!

2 Answers 2

1

How about using x.gather(dim=2, indices)? That works for me.

Sign up to request clarification or add additional context in comments.

2 Comments

yes, it works!!! Thank you! I must I lost my mind after trying too much!
@beepretty No worries! Happens with most of us :)
0

I found the answer. Please let me know if there is any better solution.

torch.cat([torch.index_select(a.view(1, -1), 1, i.view(1, -1)[0]) 
                                         for a, i in zip(x, indices)])

Comments

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.