PyTorch is pretty good at automatically differentiating stuff. If loss.backward() isn’t throwing any errors then chances are it is working properly.
I notice that triplet_loss calculates reg_dist_neg but never uses it. Is that intentional?