you can use the code from here for pairwise L2 distance.

  
      github.com
  
  
    jiyanggao/Video-Person-ReID/blob/master/losses.py#L47-L89 182

class TripletLoss(nn.Module):
"""Triplet loss with hard positive/negative mining.


Reference:
Hermans et al. In Defense of the Triplet Loss for Person Re-Identification. arXiv:1703.07737.


Code imported from https://github.com/Cysu/open-reid/blob/master/reid/loss/triplet.py.


Args:
    margin (float): margin for triplet.
"""
def __init__(self, margin=0.3):
    super(TripletLoss, self).__init__()
    self.margin = margin
    self.ranking_loss = nn.MarginRankingLoss(margin=margin)


def forward(self, inputs, targets):
    """
    Args:
        inputs: feature matrix with shape (batch_size, feat_dim)


  This file has been truncated. show original