megengine.functional.nn.hinge_loss¶
- hinge_loss(pred, label, norm='L1', reduction='mean')[source]¶
Caculates the hinge loss which is often used in SVM.
The hinge loss can be described as:
\[loss(x, y) = \frac{1}{N}\sum_i\sum_j(max(0, 1 - x_{ij}*y_{ij}))\]- Parameters
pred (
Tensor
) – input tensor representing the predicted probability, shape is (N, C).label (
Tensor
) – input tensor representing the binary classification label, shape is (N, C).norm (
str
) – specify the norm to caculate the loss, should be “L1” or “L2”.reduction (
str
) – the reduction to apply to the output: ‘none’ | ‘mean’ | ‘sum’. Default: ‘mean’
- Return type
- Returns
loss value.
Examples
>>> pred = Tensor([[0.5, -0.5, 0.1], [-0.6, 0.7, 0.8]]) >>> label = Tensor([[1, -1, -1], [-1, 1, 1]]) >>> F.nn.hinge_loss(pred, label) Tensor(1.5, device=xpux:0) >>> F.nn.hinge_loss(pred, label, reduction="none") Tensor([2.1 0.9], device=xpux:0) >>> F.nn.hinge_loss(pred, label, reduction="sum") Tensor(3.0, device=xpux:0)