mindspore.nn.HingeEmbeddingLoss
- class mindspore.nn.HingeEmbeddingLoss(margin=1.0, reduction='mean')[source]
Hinge Embedding Loss. Compute the output according to the input elements. Measures the loss given an input tensor x and a labels tensor y (containing 1 or -1). This is usually used for measuring the similarity between two inputs.
The loss function for
-th sample in the mini-batch isand the total loss functions is
where
.- Parameters
- Inputs:
logits (Tensor) - Tensor of shape
where means any number of dimensions.labels (Tensor) - 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.
ValueError – If labels does not have the same shape as logits.
ValueError – If reduction is not one of ‘none’, ‘mean’, ‘sum’.
- Supported Platforms:
Ascend
GPU
CPU
- Examplse:
>>> import numpy as np >>> from mindspore import Tensor >>> import mindspore.nn as nn >>> import mindspore.common.dtype as mstype >>> 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 = Tensor(arr1, mstype.float32) >>> labels = Tensor(arr2, mstype.float32) >>> loss = nn.HingeEmbeddingLoss(reduction='mean') >>> output = loss(logits, labels) >>> print(output) 0.16666667