You could calculate the unreduced loss and index the positive losses before reducing it and calling backward(). However, did you check why negative losses are created in the first place, as it’s rather uncommon?