Advanced indexing in 2d tensor

I have a 2d tensor X. and two lists of indexes that is first index and second call a and b. I want to do
X[a[i],b[i]] = 0 for i in range(len(a)). How can I do this. If i directly do X[a,b] the error is IndexError: The advanced indexing objects could not be broadcast

[X.__setitem__((a[i], b[i]), 0) for i in range(len(a))]

Sorry, what version of pytorch are you using? I’m on master and I’ve gotten the following to work:

In [19]: X = torch.randn(3, 3)

In [20]: a = torch.LongTensor([1, 2, 0])

In [21]: b = torch.LongTensor([0, 1, 2])

In [22]: X[a, b]


I can also assign X[a, b] = 0.

1 Like