0

I have a tensor X with shape (N,M) and a list of indices idx_list. I would like to modify X but only at the rows given by idx_list.
So I'd like to do something like this:

X[idx_list, :] = Y

Y is a tensor with shape (len(idx_list), M)

2 Answers 2

1

The solution is mentioned in your question. The usual slicing notation that's used in numpy arrays works well with PyTorch tensors as well.

X[idx_list, :] = Y

Here's a screenshot from Jupyter notebook illustrating it: enter image description here

Your approach posted as answer would work too

X[(torch.tensor(idx_list),)] = Y

However, you do not need to complicate it like this by converting the idx_list into a tensor. Slicing is preferably done using standard Python lists.

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

1 Comment

Very clear, thanks a lot!
0

This works pretty well:

X[(torch.tensor(idx_list),)] = Y

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.