Issue
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)
Solution
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:
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.
Answered By - Noob ML Dude
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.