Have a look at this repository 244. They have implemented an online loss function that uses a pair selector to find the indices of the examples which are hard and then uses those losses to do a loss.backward()