# triplet_margin_loss¶

paddle.nn.functional. triplet_margin_loss ( input, positive, negative, margin=1.0, p=2, epsilon=1e-06, swap=False, reduction='mean', name=None ) [source]

Measures the triplet loss given an input tensors $$x1$$, $$x2$$, $$x3$$ and a margin with a value greater than $$0$$. This is used for measuring a relative similarity between samples. A triplet is composed by input, positive and negative (i.e., input, positive examples and negative examples respectively). The shapes of all input tensors should be $$(N, *)$$.

The loss function for each sample in the mini-batch is:

$L(input, pos, neg) = \max \{d(input_i, pos_i) - d(input_i, neg_i) + {\rm margin}, 0\}$

where

$d(x_i, y_i) = \left\lVert {\bf x}_i - {\bf y}_i \right\rVert_p$
Parameters
• input (Tensor) – Input tensor, the data type is float32 or float64. the shape is [N, *], N is batch size and * means any number of additional dimensions, available dtype is float32, float64.

• positive (Tensor) – Positive tensor, the data type is float32 or float64. The shape of label is the same as the shape of input.

• negative (Tensor) – Negative tensor, the data type is float32 or float64. The shape of label is the same as the shape of input.

• margin (float, Optional) – Default: $$1$$.

• p (int, Optional) – The norm degree for pairwise distance. Default: $$2$$.

• epsilon (float, Optional) – Add small value to avoid division by zero, default value is 1e-6.

• swap (bool,Optional) – The distance swap change the negative distance to the distance between positive sample and negative sample. For more details, see Learning shallow convolutional feature descriptors with triplet losses. Default: False.

• reduction (str, Optional) – Indicate how to average the loss by batch_size. the candicates are 'none' | 'mean' | 'sum'. If reduction is 'none', the unreduced loss is returned; If reduction is 'mean', the reduced mean loss is returned; If reduction is 'sum', the summed loss is returned. Default: 'mean'

• name (str, Optional) – Name for the operation (optional, default is None). For more information, please refer to Name.

Returns

Tensor. The tensor variable storing the triplet_margin_loss of input and positive and negative.

Return type

Output

Examples

import paddle