mindspore.nn.MultiMarginLoss
- class mindspore.nn.MultiMarginLoss(p=1, margin=1.0, reduction='mean', weight=None)[source]
Creates a criterion that optimizes a multi-class classification hinge loss (margin-based loss) between input \(x\) (a 2D mini-batch Tensor) and output \(y\) (which is a 1D tensor of target class indices, \(0 \leq y \leq \text{x.size}(1)-1\)):
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, w[y] * (\text{margin} - x[y] + x[i]))^p}{\text{x.size}(0)}\]where \(x \in \left\{0, \; \cdots , \; \text{x.size}(0) - 1\right\}\) and \(i \neq y\).
- Parameters
p (int, optional) – The norm degree for pairwise distance. Should be 1 or 2. Default:
1
.margin (float, optional) – A parameter to change pairwise distance. 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'
: the sum of the output will be divided by the number of elements in the output.'sum'
: the output will be summed.
weight (Tensor, optional) – The rescaling weight to each class with shape \((C,)\). Data type only support float32, float16 or float64. Default:
None
, all classes are weighted equally.
- Inputs:
x (Tensor) - Input x, with shape \((N, C)\). Data type only support float32, float16 or float64. x 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. target is \(y\) in the above formula.
- Outputs:
Tensor, When reduction is ‘none’, the shape is \((N,)\). Otherwise, it is a scalar. Has the same data type with x.
- Raises
TypeError – If dtype of p or target is not int.
TypeError – If dtype of margin is not float.
TypeError – If dtype of reduction is not str.
TypeError – If dtype of x is not float16, float or float64.
TypeError – If dtype of weight and x 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 x is not equal to shape[0] of target.
ValueError – If shape[1] of x is not equal to shape[0] of weight.
ValueError – IF rank of weight is not 1.
ValueError – If rank of x is not 2 or rank of ‘target’ is not 1.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore as ms >>> import mindspore.nn as nn >>> import numpy as np >>> x = ms.Tensor(np.ones(shape=[3, 3]), ms.float32) >>> target = ms.Tensor(np.array([1, 2, 1]), ms.int64) >>> loss = nn.MultiMarginLoss() >>> output = loss(x, target) >>> print(output) 0.6666667