Class SquaredHingeLoss<T>
- Namespace
- AiDotNet.LossFunctions
- Assembly
- AiDotNet.dll
Implements the Squared Hinge Loss function for binary classification problems.
public class SquaredHingeLoss<T> : LossFunctionBase<T>, ILossFunction<T>
Type Parameters
TThe numeric type used for calculations (e.g., float, double).
- Inheritance
-
SquaredHingeLoss<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
For Beginners: Squared Hinge Loss is a variation of the Hinge Loss used in Support Vector Machines (SVMs) that applies a squared penalty to incorrectly classified examples.
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:
- It heavily penalizes predictions that are incorrect or not confident enough
- The quadratic nature creates a smoother loss surface compared to regular Hinge Loss
- It has a continuous derivative everywhere, which can make optimization easier
- It's zero when predictions are correct and confident (y*f(x) = 1)
Squared Hinge Loss is particularly useful for:
- Binary classification problems
- Support Vector Machines
- Any situation where smoother gradients are beneficial for optimization
Compared to regular Hinge Loss, it penalizes violations more severely due to the squaring operation.
Methods
CalculateDerivative(Vector<T>, Vector<T>)
Calculates the derivative of the Squared Hinge Loss function.
public override Vector<T> CalculateDerivative(Vector<T> predicted, Vector<T> actual)
Parameters
predictedVector<T>The predicted values vector.
actualVector<T>The actual (ground truth) values vector, typically -1 or 1.
Returns
- Vector<T>
A vector containing the derivatives of the squared hinge loss with respect to each predicted value.
CalculateLoss(Vector<T>, Vector<T>)
Calculates the Squared Hinge Loss between predicted and actual values.
public override T CalculateLoss(Vector<T> predicted, Vector<T> actual)
Parameters
predictedVector<T>The predicted values vector.
actualVector<T>The actual (ground truth) values vector, typically -1 or 1.
Returns
- T
The squared hinge loss value.
CalculateLossAndGradientGpu(IGpuTensor<T>, IGpuTensor<T>)
Calculates both Squared Hinge loss and gradient on GPU in a single efficient pass.
public override (T Loss, IGpuTensor<T> Gradient) CalculateLossAndGradientGpu(IGpuTensor<T> predicted, IGpuTensor<T> actual)
Parameters
predictedIGpuTensor<T>The predicted GPU tensor from the model.
actualIGpuTensor<T>The actual (target) GPU tensor.