mindspore.nn.HingeEmbeddingLoss
- class mindspore.nn.HingeEmbeddingLoss(margin=1.0, reduction='mean')[source]
Calculate the Hinge Embedding Loss value based on the input ‘logits’ and’ labels’ (only including 1 or -1). Usually used to measure the similarity between two inputs.
The loss function for \(n\)-th sample in the mini-batch is
\[\begin{split}l_n = \begin{cases} x_n, & \text{if}\; y_n = 1,\\ \max \{0, \Delta - x_n\}, & \text{if}\; y_n = -1, \end{cases}\end{split}\]and the total loss functions is
\[\begin{split}\ell(x, y) = \begin{cases} \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.} \end{cases}\end{split}\]where \(L = \{l_1,\dots,l_N\}^\top\).
- Parameters
margin (float, int) – Threshold defined by Hinge Embedding Loss \(margin\). Represented as \(\Delta\) in the formula. Default:
1.0
.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.
- Inputs:
logits (Tensor) - The predicted value, expressed as \(x\) in the equation. Tensor of shape \((*)\) where \(*\) means any number of dimensions.
labels (Tensor) - Label value, represented as \(y\) in the equation. Same shape as the logits, contains -1 or 1.
- Returns
Tensor or Tensor scalar, the computed loss depending on \(reduction\).
- Raises
TypeError – If logits is not a Tensor.
TypeError – If labels is not a Tensor.
TypeError – If margin is not a float or int.
ValueError – If labels does not have the same shape as logits or they could not broadcast to each other.
ValueError – If reduction is not one of
'none'
,'mean'
,'sum'
.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore as ms >>> import mindspore.nn as nn >>> import numpy as np >>> arr1 = np.array([0.9, -1.2, 2, 0.8, 3.9, 2, 1, 0, -1]).reshape((3, 3)) >>> arr2 = np.array([1, 1, -1, 1, -1, 1, -1, 1, 1]).reshape((3, 3)) >>> logits = ms.Tensor(arr1, ms.float32) >>> labels = ms.Tensor(arr2, ms.float32) >>> loss = nn.HingeEmbeddingLoss(reduction='mean') >>> output = loss(logits, labels) >>> print(output) 0.16666667