multiclasshingeloss
Metric
MulticlassHingeLossfromtorchmetrics(torchmetrics.classification.MulticlassHingeLoss)
When to invoke this skill
The user has predictions + ground truth and asks to evaluate with MulticlassHingeLoss, or
mentions torchmetrics.classification.MulticlassHingeLoss directly, or wants the standard torchmetrics implementation.
Reference signature
from torchmetrics.classification import MulticlassHingeLoss
# MulticlassHingeLoss(num_classes: int, squared: bool = False, multiclass_mode: Literal['crammer-singer', 'one-vs-all'] = 'crammer-singer', ignore_index: Optional[int] = None, validate_args: bool = True, **kwargs: Any) -> None
Library docstring
Compute the mean `Hinge loss`_ typically used for Support Vector Machines (SVMs) for multiclass tasks.
The metric can be computed in two ways. Either, the definition by Crammer and Singer is used:
.. math::
\text{Hinge loss} = \max\left(0, 1 - \hat{y}_y + \max_{i \ne y} (\hat{y}_i)\right)
Where :math:`y \in {0, ..., \mathrm{C}}` is the target class (where :math:`\mathrm{C}` is the number of classes),
and :math:`\hat{y} \in \mathbb{R}^\mathrm{C}` is the predicted output per class. Alternatively, the metric can
also be computed in one-vs-all approach, where each class is valued against all other classes in a binary fashion.
As input to ``forward`` and ``update`` the metric accepts the following input:
- ``preds`` (:class:`~torch.Tensor`): A float tensor of shape ``(N, C, ...)``. Preds should be a tensor
containing probabilities or logits for each observation. If preds has values outside [0,1] range we consider
the input to be logits and will auto apply softmax per sample.
- ``target`` (:class:`~torch.Tensor`): An int tensor of shape ``(N, ...)``. Target should be a tensor containing
ground truth labels, and therefore only contain values in the [0, n_classes-1] range (except if `ignore_index`
is specified).
.. tip::
Additional dimension ``...`` will be flattened into the batch dimension.
As output to ``forward`` and ``compute`` the metric returns the following output:
- ``mchl`` (:class:`~torch.Tensor`): A tensor containing the multi-class hinge loss.
Args:
num_classes: Integer specifying the number of classes
squared:
If True, this will compute the squared hinge loss. Otherwise, computes the regular hinge loss.
multiclass_mode:
Determines how to compute the metric
ignore_index:
Specifies a target value that is ignored and does not contribute to the metric calculation
validate_args: bool indicating if input arguments and tensors should be validated for correctness.
Set to ``False`` for faster computations.
kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.
Example:
>>> from torchmetrics.classification import MulticlassHingeLoss
>>> preds = torch.tenso
Quick recipe
import torchmetrics.classification as _m
score = _m.MulticlassHingeLoss(y_true, y_pred)
Don'ts
- Don't reimplement when the library version handles edge cases (NaN, ties, empty inputs) better than a hand-rolled formula.
- Always check the library version's argument order — sklearn is
(y_true, y_pred)while torchmetrics is(preds, target).