It works with pytorch 0.4,
If your using 0.3, you migh want to do: a[a.nonzero().detach()]: you cannot backpropagate the indices so they should not require gradients. I don’t have pytorch 0.3 here, does that work?