mindspore.nn.TripletMarginLoss
- class mindspore.nn.TripletMarginLoss(p=2, swap=False, eps=1e-06, reduction='mean', margin=1.)[source]
TripletMarginLoss operation.
Triple loss is used to measure the relative similarity between samples, which is measured by a triplet and a \(margin\) with a value greater than \(0\) . The triplet is composed by \(a\), \(p\), \(n\) in the following formula.
The shapes of all input tensors should be \((N, *)\) , where \(N\) is batch size and \(*\) means any number of additional dimensions.
The distance swap is described in detail in the paper Learning local feature descriptors with triplets and shallow convolutional neural networks by V. Balntas, E. Riba et al.
The loss function for each sample in the mini-batch is:
\[L(a, p, n) = \max \{d(a_i, p_i) - d(a_i, n_i) + {\rm margin}, 0\}\]where
\[d(x_i, y_i) = \left\lVert {\bf x}_i - {\bf y}_i \right\rVert_p\]- Parameters
p (int, optional) – The degree of norm for pairwise distance. Default:
2
.eps (float, optional) – Add small value to avoid division by zero. Default:
1e-06
.swap (bool, optional) – The distance swap change the negative distance to the distance between positive sample and negative sample. Default:
False
.reduction (str, optional) –
Apply specific reduction method to the output:
'none'
,'mean'
,'sum'
. Default:'mean'
.'none'
: no reduction will be applied.'mean'
: compute and return the mean of elements in the output.'sum'
: the output elements will be summed.
margin (Union[Tensor, float]) – Make a margin between the positive pair and the negative pair. The length of shape of margin must be 0. Default:
1.0
.
- Inputs:
x (Tensor) - A sample randomly selected from the training set. Data type must be BasicType. \(a\) in the above formula.
positive (Tensor) - A sample belonging to the same category as x, with the same type and shape as x. \(p\) in the above formula.
negative (Tensor) - A sample belonging to the different class from x, with the same type and shape as x. \(n\) in the above formula.
margin (Union[Tensor, float]) - Make a margin between the positive pair and the negative pair. The length of shape of margin must be 0. Default:
1.0
.
- Outputs:
Tensor. If reduction is
"none"
, its shape is \((N)\). Otherwise, a scalar value will be returned.
- Raises
TypeError – If x or positive or 'negative' is not a Tensor.
TypeError – If dtype of x, positive and negative is not the same.
TypeError – If p is not an int.
TypeError – If eps is not a float.
TypeError – If swap is not a bool.
ValueError – If dimensions of input x, positive and negative are less than or equal to 1 at the same time.
ValueError – If the dimension of input x or positive or negative is bigger than or equal to 8.
ValueError – If length of shape of margin is not 0.
ValueError – If shape of x, positive and negative cannot broadcast.
ValueError – If reduction is not one of
'none'
,'mean'
,'sum'
.
- Supported Platforms:
GPU
Examples
>>> import mindspore as ms >>> import mindspore.nn as nn >>> import numpy as np >>> loss = nn.TripletMarginLoss() >>> x = ms.Tensor(np.array([[0.3, 0.7], [0.5, 0.5]]), ms.float32) >>> positive = ms.Tensor(np.array([[0.4, 0.6], [0.4, 0.6]]), ms.float32) >>> negative = ms.Tensor(np.array([[0.2, 0.9], [0.3, 0.7]]), ms.float32) >>> output = loss(x, positive, negative) >>> print(output) 0.8881968