Hinge Loss¶
Module Interface¶
- class torchmetrics.HingeLoss(squared=False, multiclass_mode=None, compute_on_step=None, **kwargs)[source]
Computes the mean Hinge loss, typically used for Support Vector Machines (SVMs).
In the binary case it is defined as:

Where
is the target, and
is the prediction.In the multi-class case, when
multiclass_mode=None(default),multiclass_mode=MulticlassMode.CRAMMER_SINGERormulticlass_mode="crammer-singer", this metric will compute the multi-class hinge loss defined by Crammer and Singer as:
Where
is the target class (where
is the number of classes),
and
is the predicted output per class.In the multi-class case when
multiclass_mode=MulticlassMode.ONE_VS_ALLormulticlass_mode='one-vs-all', this metric will use a one-vs-all approach to compute the hinge loss, giving a vector of C outputs where each entry pits that class against all remaining classes.This metric can optionally output the mean of the squared hinge loss by setting
squared=TrueOnly accepts inputs with preds shape of (N) (binary) or (N, C) (multi-class) and target shape of (N).
- Parameters
squared¶ (
bool) – If True, this will compute the squared hinge loss. Otherwise, computes the regular hinge loss (default).multiclass_mode¶ (
Union[str,MulticlassMode,None]) – Which approach to use for multi-class inputs (has no effect in the binary case).None(default),MulticlassMode.CRAMMER_SINGERor"crammer-singer", uses the Crammer Singer multi-class hinge loss.MulticlassMode.ONE_VS_ALLor"one-vs-all"computes the hinge loss in a one-vs-all fashion.compute_on_step¶ (
Optional[bool]) –Forward only calls
update()and returns None if this is set to False.Deprecated since version v0.8: Argument has no use anymore and will be removed v0.9.
kwargs¶ (
Dict[str,Any]) – Additional keyword arguments, see Advanced metric settings for more info.
- Raises
ValueError – If
multiclass_modeis not: None,MulticlassMode.CRAMMER_SINGER,"crammer-singer",MulticlassMode.ONE_VS_ALLor"one-vs-all".
- Example (binary case):
>>> import torch >>> from torchmetrics import HingeLoss >>> target = torch.tensor([0, 1, 1]) >>> preds = torch.tensor([-2.2, 2.4, 0.1]) >>> hinge = HingeLoss() >>> hinge(preds, target) tensor(0.3000)
- Example (default / multiclass case):
>>> target = torch.tensor([0, 1, 2]) >>> preds = torch.tensor([[-1.0, 0.9, 0.2], [0.5, -1.1, 0.8], [2.2, -0.5, 0.3]]) >>> hinge = HingeLoss() >>> hinge(preds, target) tensor(2.9000)
- Example (multiclass example, one vs all mode):
>>> target = torch.tensor([0, 1, 2]) >>> preds = torch.tensor([[-1.0, 0.9, 0.2], [0.5, -1.1, 0.8], [2.2, -0.5, 0.3]]) >>> hinge = HingeLoss(multiclass_mode="one-vs-all") >>> hinge(preds, target) tensor([2.2333, 1.5000, 1.2333])
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- compute()[source]
Override this method to compute the final metric value from state variables synchronized across the distributed backend.
- Return type
Functional Interface¶
- torchmetrics.functional.hinge_loss(preds, target, squared=False, multiclass_mode=None)[source]
Computes the mean Hinge loss typically used for Support Vector Machines (SVMs).
In the binary case it is defined as:

Where
is the target, and
is the prediction.In the multi-class case, when
multiclass_mode=None(default),multiclass_mode=MulticlassMode.CRAMMER_SINGERormulticlass_mode="crammer-singer", this metric will compute the multi-class hinge loss defined by Crammer and Singer as:
Where
is the target class (where
is the number of classes),
and
is the predicted output per class.In the multi-class case when
multiclass_mode=MulticlassMode.ONE_VS_ALLormulticlass_mode='one-vs-all', this metric will use a one-vs-all approach to compute the hinge loss, giving a vector of C outputs where each entry pits that class against all remaining classes.This metric can optionally output the mean of the squared hinge loss by setting
squared=TrueOnly accepts inputs with preds shape of (N) (binary) or (N, C) (multi-class) and target shape of (N).
- Parameters
preds¶ (
Tensor) – Predictions from model (as float outputs from decision function).squared¶ (
bool) – If True, this will compute the squared hinge loss. Otherwise, computes the regular hinge loss (default).multiclass_mode¶ (
Union[str,MulticlassMode,None]) – Which approach to use for multi-class inputs (has no effect in the binary case).None(default),MulticlassMode.CRAMMER_SINGERor"crammer-singer", uses the Crammer Singer multi-class hinge loss.MulticlassMode.ONE_VS_ALLor"one-vs-all"computes the hinge loss in a one-vs-all fashion.
- Raises
ValueError – If preds shape is not of size (N) or (N, C).
ValueError – If target shape is not of size (N).
ValueError – If
multiclass_modeis not: None,MulticlassMode.CRAMMER_SINGER,"crammer-singer",MulticlassMode.ONE_VS_ALLor"one-vs-all".
- Example (binary case):
>>> import torch >>> from torchmetrics.functional import hinge_loss >>> target = torch.tensor([0, 1, 1]) >>> preds = torch.tensor([-2.2, 2.4, 0.1]) >>> hinge_loss(preds, target) tensor(0.3000)
- Example (default / multiclass case):
>>> target = torch.tensor([0, 1, 2]) >>> preds = torch.tensor([[-1.0, 0.9, 0.2], [0.5, -1.1, 0.8], [2.2, -0.5, 0.3]]) >>> hinge_loss(preds, target) tensor(2.9000)
- Example (multiclass example, one vs all mode):
>>> target = torch.tensor([0, 1, 2]) >>> preds = torch.tensor([[-1.0, 0.9, 0.2], [0.5, -1.1, 0.8], [2.2, -0.5, 0.3]]) >>> hinge_loss(preds, target, multiclass_mode="one-vs-all") tensor([2.2333, 1.5000, 1.2333])
- Return type