TripletLoss

class TripletLoss(margin=1, weight=None, batch_axis=0, **kwargs)

Calculates triplet loss given three input tensors and a positive margin. Triplet loss measures the relative similarity between a positive example, a negative example, and prediction:

(47)\[L = \sum_i \max(\Vert {pos_i}_i - {pred} \Vert_2^2 - \Vert {neg_i}_i - {pred} \Vert_2^2 + {margin}, 0)\]

positive, negative, and ‘pred’ can have arbitrary shape as long as they have the same number of elements.

Parameters:
margin : float

Margin of separation between correct and incorrect pair.

weight : float or None

Global scalar weight for loss.

batch_axis : int, default 0

The axis that represents mini-batch.

Inputs:
  • pred: prediction tensor with arbitrary shape
  • positive: positive example tensor with arbitrary shape. Must have the same size as pred.
  • negative: negative example tensor with arbitrary shape Must have the same size as pred.
Outputs:
  • loss: loss tensor with shape (batch_size,).