mindspore.ops.multi_margin_loss

View Source On Gitee
mindspore.ops.multi_margin_loss(input, target, p=1, margin=1, weight=None, reduction='mean')[source]

Hinge loss for optimizing a multi-class classification.

Optimizes a multi-class classification hinge loss (margin-based loss) between input and output.

For each mini-batch sample, the loss in terms of the 1D input \(x\) and scalar output \(y\) is:

\[\text{loss}(x, y) = \frac{\sum_i \max(0, \text{margin} - x[y] + x[i])^p}{\text{x.size}(0)}\]

where \(i\in \{0,⋯,x.size(0)-1\}\) and \(i \ne y\).

Parameters
  • input (Tensor) – Input , with shape \((N, C)\). Data type only support float32, float16 or float64. It is \(x\) in the above formula.

  • target (Tensor) – Ground truth labels, with shape \((N,)\). Data type only support int64. The value of target should be non-negative, less than C. It is \(y\) in the above formula.

  • p (int, optional) – The norm degree for pairwise distance. Should be 1 or 2. Default: 1 .

  • margin (int, optional) – A parameter to change pairwise distance. Default: 1 .

  • weight (Tensor, optional) – The rescaling weight to each class with shape \((C,)\). Data type only support float16, float32 or float64. Default: None .

  • 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 weighted mean of elements in the output.

    • 'sum': the output elements will be summed.

Returns

  • outputs - Tensor. If reduction is 'none', returns a Tensor with the same shape as target. Otherwise, it is a scalar.

Raises
  • TypeError – If dtype of p or target is not int.

  • TypeError – If dtype of margin is not int.

  • TypeError – If dtype of reduction is not str.

  • TypeError – If dtype of input is not float16, float or float64.

  • TypeError – If dtype of weight and input is not the same.

  • ValueError – If p is not 1 or 2.

  • ValueError – If reduction is not one of {'none','sum','mean'}.

  • ValueError – If shape[0] of input is not equal to shape[0] of target.

  • ValueError – If shape[1] of input is not equal to shape[0] of weight.

  • ValueError – If rank of weight is not 1 or rank of target is not 1 or input is not 2.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> inputs = Tensor(np.ones(shape=[3, 3]), mindspore.float32)
>>> target = Tensor(np.array([1, 2, 1]), mindspore.int64)
>>> weight = Tensor(np.array([1, 1, 1]), mindspore.float32)
>>> output = ops.multi_margin_loss(inputs, target, weight=weight)
>>> print(output)
0.6666667