# Binaryhingeloss

> Compute the BinaryHingeLoss metric — provided by torchmetrics. Use when the user has predictions and ground-truth and needs to compute BinaryHingeLoss, or asks how to score with BinaryHingeLoss.

- Skill: `qhjqhj00/binaryhingeloss` (Agent Skill)
- Install (CLI): `npx skillmds add qhjqhj00/binaryhingeloss`
- Raw SKILL.md: https://api.skillmd.com/api/skills/qhjqhj00/binaryhingeloss/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: qhjqhj00 (https://skillmd.com/u/qhjqhj00)
- Updated: 2026-09-08
- Page: https://skillmd.com/skills/qhjqhj00/binaryhingeloss

---


# binaryhingeloss

> Metric `BinaryHingeLoss` from `torchmetrics` (torchmetrics.classification.BinaryHingeLoss)

## When to invoke this skill

The user has predictions + ground truth and asks to evaluate with BinaryHingeLoss, or
mentions `torchmetrics.classification.BinaryHingeLoss` directly, or wants the standard torchmetrics implementation.

## Reference signature

```python
from torchmetrics.classification import BinaryHingeLoss

# BinaryHingeLoss(squared: bool = False, 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 binary tasks.

.. math::
    \text{Hinge loss} = \max(0, 1 - y \times \hat{y})

Where :math:`y \in {-1, 1}` is the target, and :math:`\hat{y} \in \mathbb{R}` is the prediction.

As input to ``forward`` and ``update`` the metric accepts the following input:

- ``preds`` (:class:`~torch.Tensor`): A float tensor of shape ``(N, ...)``. 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 sigmoid per element.
- ``target`` (:class:`~torch.Tensor`): An int tensor of shape ``(N, ...)``. Target should be a tensor containing
  ground truth labels, and therefore only contain {0,1} values (except if `ignore_index` is specified). The value
  1 always encodes the positive class.

.. tip::
   Additional dimension ``...`` will be flattened into the batch dimension.

As output to ``forward`` and ``compute`` the metric returns the following output:

- ``bhl`` (:class:`~torch.Tensor`): A tensor containing the hinge loss.

Args:
    squared:
        If True, this will compute the squared hinge loss. Otherwise, computes the regular hinge loss.
    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 BinaryHingeLoss
    >>> preds = torch.tensor([0.25, 0.25, 0.55, 0.75, 0.75])
    >>> target = torch.tensor([0, 0, 1, 1, 1])
    >>> bhl = BinaryHingeLoss()
    >>> bhl(preds, target)
    tensor(0.6900)
    >>> bhl = BinaryHingeLoss(squared=True)
    >>> bhl(preds, target)
    tensor(0.6905)
```

## Quick recipe

```python
import torchmetrics.classification as _m
score = _m.BinaryHingeLoss(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)`.

