Solved by eqy in post #2 
              
              
                It looks like there is an implicit reduction here; are you treating rows with multiple ones the same as those that have a single one? 
In that case, you could try: 
import torch
a = torch.tensor([[0., 0., 0., 0.],
    [0., 1., 1., 0.],
    [0., 0., 0., 0.],
    [0., 0., 1., 0.],
    [0., 0., 0., 0.]…