Table of Contents

Class ModifiedHuberLoss<T>

Namespace
AiDotNet.LossFunctions
Assembly
AiDotNet.dll

Implements the Modified Huber Loss function, a smoother version of the hinge loss.

public class ModifiedHuberLoss<T> : LossFunctionBase<T>, ILossFunction<T>

Type Parameters

T

The numeric type used for calculations (e.g., float, double).

Inheritance
ModifiedHuberLoss<T>
Implements
Inherited Members
Extension Methods

Remarks

For Beginners: Modified Huber Loss is a smoother version of the hinge loss that's less sensitive to outliers. It combines quadratic behavior near zero with linear behavior for large negative values.

The formula is:

  • For z = -1: max(0, 1 - z)²
  • For z < -1: -4 * z

Where z = y * f(x), with y being the true label and f(x) the prediction.

Key properties:

  • It's smoother than hinge loss, making optimization easier
  • It's more robust to outliers than squared hinge loss
  • It combines the benefits of both quadratic and linear losses
  • It has a continuous first derivative

Modified Huber Loss is particularly useful for:

  • Binary classification problems
  • Datasets with noisy labels
  • Problems where you want to balance between being sensitive to errors but not overly influenced by extreme mistakes

Methods

CalculateDerivative(Vector<T>, Vector<T>)

Calculates the derivative of the Modified Huber Loss function.

public override Vector<T> CalculateDerivative(Vector<T> predicted, Vector<T> actual)

Parameters

predicted Vector<T>

The predicted values vector.

actual Vector<T>

The actual (ground truth) values vector, typically -1 or 1.

Returns

Vector<T>

A vector containing the derivatives of the modified huber loss with respect to each prediction.

CalculateLoss(Vector<T>, Vector<T>)

Calculates the Modified Huber Loss between predicted and actual values.

public override T CalculateLoss(Vector<T> predicted, Vector<T> actual)

Parameters

predicted Vector<T>

The predicted values vector.

actual Vector<T>

The actual (ground truth) values vector, typically -1 or 1.

Returns

T

The modified huber loss value.

CalculateLossAndGradientGpu(IGpuTensor<T>, IGpuTensor<T>)

Calculates both Modified Huber 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

predicted IGpuTensor<T>

The predicted GPU tensor from the model.

actual IGpuTensor<T>

The actual (target) GPU tensor.

Returns

(T Loss, IGpuTensor<T> Gradient)

A tuple containing the loss value and gradient tensor.