1

I've got a tensor A with shape (M, N), and have another tensor B with shape (M, P) and with values of given indices in corresponding rows of A. Now I would like to set the values of A with corresponding indices in B to 0.

For example:

In[1]: import torch
       A = torch.tensor([range(1,11), range(1,11), range(1,11)])
       A
Out[1]: 
tensor([[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10],
        [ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10],
        [ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10]])
In[2]: B = torch.tensor([[1,2], [2,3], [3,5]])
       B
Out[2]: 
tensor([[1, 2],
        [2, 3],
        [3, 5]])

The objective is to set the value of the element with index 1,2 in the first row, 2,3 in the second row, and 3,5 in the third row of A to 0, i.e., setting A to

tensor([[ 1,  0,  0,  4,  5,  6,  7,  8,  9, 10],
        [ 1,  2,  0,  0,  5,  6,  7,  8,  9, 10],
        [ 1,  2,  3,  0,  5,  0,  7,  8,  9, 10]])

I have applied row by row for loop, and also tried scatter:

zeros = torch.zeros(A.shape, dtype=torch.float).to("cuda")
A = A.scatter_(1, B, zeros)

The two methods work fine, but all give quite poor performance. Actually, I infer that some efficient approach should exist based on an error before. I initially used A[:, B] = 0. This would set all the indices of appeared in B to 0, regardless of the row. However, the training speed improved drastically when doing A[:, B] = 0.

Is there any way to implement this more efficiently?

1
  • Are you sure scatter_ is slow, can you prove it? That should be the go-to method... Commented Aug 22, 2021 at 9:17

1 Answer 1

1

Here's what i would do:

import torch
A = torch.tensor([range(1,11), range(1,11), range(1,11)])
B = torch.tensor([[1,2], [2,3], [3,5]])
r, c = B.shape
idx0 = torch.arange(r).reshape(-1, 1).repeat(1, c).flatten()
idx1 = B.flatten()
A[idx0, idx1] = 0

output:

A = 
tensor([[ 1,  0,  0,  4,  5,  6,  7,  8,  9, 10],
        [ 1,  2,  0,  0,  5,  6,  7,  8,  9, 10],
        [ 1,  2,  3,  0,  5,  0,  7,  8,  9, 10]])
Sign up to request clarification or add additional context in comments.

1 Comment

You could use idx0 = torch.arange(r).repeat_interleave(c) instead.

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.