MultiLabelMarginLoss
- class paddle.nn. MultiLabelMarginLoss ( reduction: _ReduceMode = 'mean', name: str | None = None ) [source]
-
Creates a criterion that optimizes 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
-
reduction (str, optional) – Indicate how to calculate the loss by batch_size, the candidates are
'none'
|'mean'
|'sum'
. Ifreduction
is'none'
, the unreduced loss is returned; Ifreduction
is'mean'
, the reduced mean loss is returned; Ifreduction
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.
- Call parameters:
-
input (Tensor): Input tensor, the data type is float32 or float64.
- label (Tensor): Label tensor, the data type is int32 or int64.
-
Label values should be class indices (non-negative values) and -1 values. The -1 values are ignored and stop processing for each sample.
- Shape:
-
input: 2-D Tensor, the shape is \([N, C]\), where \(N\) is batch size and \(C\) is number of classes.
label: 2-D Tensor, the shape is \([N, C]\), same shape as input.
output: scalar. If
reduction
is'none'
, then same shape as \([N]\).
- Returns
-
A callable object of MultiLabelMarginLoss.
Examples
>>> import paddle >>> import paddle.nn as nn >>> 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') >>> multi_label_margin_loss = nn.MultiLabelMarginLoss(reduction='mean') >>> loss = multi_label_margin_loss(input, label) >>> print(loss) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, 0.94999999)
-
forward
(
input: Tensor,
label: Tensor
)
Tensor
forward¶
-
Defines the computation performed at every call. Should be overridden by all subclasses.
- Parameters
-
*inputs (tuple) – unpacked tuple arguments
**kwargs (dict) – unpacked dict arguments