HingeLoss<T>
Models & Types · AiDotNet.LossFunctions
Implements the Hinge loss function commonly used in support vector machines.
Inheritance: LossFunctionBase<T> → HingeLoss<T>
For Beginners
Hinge loss is used for binary classification problems, particularly in support vector machines (SVMs). It measures how well your model separates different classes.
The formula is: max(0, 1 - y * f(x)), where:
- y is the true label (usually -1 or 1)
- f(x) is the model’s prediction
Key properties of hinge loss:
- It penalizes predictions that are incorrect or not confident enough
- It’s zero when the prediction is correct and confident (y*f(x) = 1)
- It increases linearly when the prediction is incorrect or not confident enough
- It encourages the model to find a decision boundary with a large margin between classes
This loss function is ideal for binary classification tasks where you want to maximize the margin between different classes, which often improves generalization to new data.
Example
using AiDotNet.LossFunctions;
using AiDotNet.Tensors.LinearAlgebra;
var loss = new HingeLoss<float>();
var predicted = new Vector<float>(new[] { 0.9f, 0.2f, 0.7f });
var actual = new Vector<float>(new[] { 1.0f, 0.0f, 1.0f });
float value = loss.CalculateLoss(predicted, actual);
Console.WriteLine($"HingeLoss = {value:F4}");
Constructors
| Constructor | Summary |
|---|---|
HingeLoss() |
Methods
| Method | Summary |
|---|---|
T CalculateLoss(Vector<T> predicted, Vector<T> actual) |
Calculates the Hinge loss between predicted and actual values. |
Tensor<T> ComputeTapeLoss(Tensor<T> predicted, Tensor<T> target) |
|
ValueTuple<T, Tensor<T>> CalculateLossAndGradientGpu(Tensor<T> predicted, Tensor<T> actual) |
Calculates both Hinge loss and gradient on GPU in a single efficient pass. |