multi_label_margin_loss

paddle.nn.functional. multi_label_margin_loss ( input: Tensor, label: Tensor, reduction: _ReduceMode = 'mean', name: str | None = None ) Tensor [source]

Measures a multi-class multi-classification hinge loss (margin-based loss) between input \(input\) and label \(label\):

For i-th mini-batch sample, the loss in terms of the 2D input \(input_i\) and 2D label \(label_i\) is:

\[\text{loss}(input_i, label_i) = \frac{\sum_{j \in \text{valid_labels}} \sum_{k \neq \text{valid_labels}} \max(0, 1 - (input_i[\text{valid_labels}[j]] - input_i[k]))}{C}\]

where \(C\) is the number of classes, \(\text{valid_labels}\) contains all non-negative label indices for sample \(i\) (stopping at the first -1 encountered), and \(k\) ranges over all class indices except those in \(\text{valid_labels}\).

The criterion only considers the first non-negative label values, allowing different samples to have variable numbers of target classes.

Parameters
  • input (Tensor) – Input tensor, the data type is float32 or float64. Shape is (N, C), where C is number of classes.

  • label (Tensor) – Label tensor, the data type is int32 or int64. Shape is (N, C), same shape as input. Label values should be class indices (non-negative values) and -1 values. The -1 values are ignored and stop processing for each sample.

  • reduction (str, optional) – Indicate how to calculate the loss by batch_size, the candidates are 'none' | 'mean' | 'sum'. If reduction is 'none', the unreduced loss is returned; If reduction is 'mean', the reduced mean loss is returned; If reduction is 'sum', the summed loss is returned. Default: 'mean'

  • name (str|None, optional) – Name for the operation (optional, default is None). For more information, please refer to api_guide_Name.

Returns

Tensor, The tensor variable storing the multi_label_margin_loss of input and label.

Examples

>>> import paddle
>>> import paddle.nn.functional as F

>>> input = paddle.to_tensor([[0.1, 0.2, 0.4, 0.8], [0.2, 0.5, 0.3, 0.1]], dtype='float32')
>>> label = paddle.to_tensor([[3, 0, -1, -1], [0, 2, -1, -1]], dtype='int64')

>>> loss = F.multi_label_margin_loss(input, label, reduction='mean')
>>> print(loss)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
       0.94999999)