Ignore batch of samples giving negative loss

I want to ignore all such samples which lead to negative loss , how to skip sample while calculating loss.

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?