Implementation:Rapidsai Cuml Hinge Loss
| Knowledge Sources | |
|---|---|
| Domains | Machine_Learning, Classification, Model_Evaluation |
| Last Updated | 2026-02-08 12:00 GMT |
Overview
Computes the GPU-accelerated non-regularized hinge loss for binary and multiclass classification, commonly used to evaluate support vector machine (SVM) classifiers.
Description
The hinge_loss function calculates the average hinge loss from true labels and predicted decision function outputs. It is adapted from scikit-learn's hinge loss implementation and supports both binary and multiclass settings.
Binary case: Labels are binarized to +1 / -1 using LabelBinarizer. The margin is computed as y_true * pred_decision, and the hinge loss is max(0, 1 - margin).
Multiclass case: For each sample, the margin is computed as the difference between the decision value for the correct class and the maximum decision value among the incorrect classes. Labels are encoded with LabelEncoder. All class labels must either be present in y_true or passed explicitly via the labels parameter.
The function accepts cuDF Series/DataFrames and CuPy/Numba arrays as inputs.
Usage
Use this function to evaluate SVM-based classifiers or any classifier that produces raw decision function scores. Hinge loss penalizes predictions that are on the wrong side of the decision boundary or insufficiently confident.
Code Reference
Source Location
- Repository: Rapidsai_Cuml
- File:
python/cuml/cuml/metrics/hinge_loss.py
Signature
def hinge_loss(
y_true, pred_decision, labels=None, sample_weights=None
) -> float
Import
from cuml.metrics import hinge_loss
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| y_true | cuDF Series or CuPy array of shape (n_samples,) | Yes | True class labels. In binary classification the positive label must be greater than the negative label. |
| pred_decision | cuDF DataFrame or CuPy array of shape (n_samples,) or (n_samples, n_classes) | Yes | Predicted decision function values (floats), as returned by decision_function.
|
| labels | cuDF Series or CuPy array | No | In multiclass problems, all class labels must be provided. Default None.
|
| sample_weights | CuPy array of shape (n_samples,) | No | Per-sample weights for computing the average. Default None.
|
Outputs
| Name | Type | Description |
|---|---|---|
| loss | float | The average hinge loss across all samples. A non-negative value; lower is better, 0.0 is perfect. |
Usage Examples
import cupy as cp
from cuml.metrics import hinge_loss
# Binary classification example
y_true = cp.array([1, 1, -1, -1])
pred_decision = cp.array([0.8, 0.6, -0.7, -0.3])
labels = cp.array([-1, 1])
loss = hinge_loss(y_true, pred_decision, labels=labels)
print("Hinge Loss:", loss)
# Multiclass example
y_true_mc = cp.array([0, 1, 2, 0])
pred_decision_mc = cp.array([
[0.9, 0.1, 0.0],
[0.1, 0.8, 0.1],
[0.0, 0.2, 0.8],
[0.7, 0.2, 0.1],
])
labels_mc = cp.array([0, 1, 2])
loss_mc = hinge_loss(y_true_mc, pred_decision_mc, labels=labels_mc)
print("Multiclass Hinge Loss:", loss_mc)