hinge_loss

Contents

hinge_loss#

class braintools.metric.hinge_loss(predictor_outputs, targets)#

Compute the hinge loss for binary classification.

The hinge loss is commonly used for training classifiers, particularly Support Vector Machines. It provides a margin-based loss that is zero when the prediction is correct and confident, and increases linearly with the distance from the margin.

The hinge loss is defined as: max(0, 1 - y * f(x)) where y is the true class label and f(x) is the predicted output.

Parameters:
  • predictor_outputs (Array | ndarray | bool | number | bool | int | float | complex | Quantity) – Outputs of the decision function. Real-valued predictions from the model.

  • targets (Array | ndarray | bool | number | bool | int | float | complex | Quantity) – Target values. Must be in the set {-1, 1} for binary classification. Shape must be broadcastable with predictor_outputs.

Returns:

Hinge loss values with the same shape as the input arrays.

Return type:

Array | ndarray | bool | number | bool | int | float | complex | Quantity

Examples

>>> import jax.numpy as jnp
>>> import braintools
>>> predictions = jnp.array([1.0, -0.5, 2.0])
>>> targets = jnp.array([1, -1, 1])
>>> loss = braintools.metric.hinge_loss(predictions, targets)
>>> print(loss)
[0.  1.5 0. ]