Yes. Thanks for this solution. I also have another problem in which I need to index a whole batch of tensors like this.
a = torch.zeros((5, 30000))
indices = [[1,2], [1,4,5], [2,6], [6,8], [3,12, 110]]
a[indices] = 1

As you can see, I have a sequence of such sparse tensors. And a sequence of indices. We can definitely use a for loop to assign value 1 to each of the five 30000 dimensional vectors one by one, but its huge overhead (approx. 4 seconds in my program). Is there more efficient way to do this? Without using a for loop? Thanks