Table of Contents

Class HuberLoss<T>

Namespace
AiDotNet.LossFunctions
Assembly
AiDotNet.dll

Implements the Huber loss function, which combines properties of both MSE and MAE.

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

Type Parameters

T

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

Inheritance
HuberLoss<T>
Implements
Inherited Members
Extension Methods

Remarks

For Beginners: Huber loss combines the best properties of Mean Squared Error and Mean Absolute Error.

The formula is:

  • For errors smaller than delta: 0.5 * error²
  • For errors larger than delta: delta * (|error| - 0.5 * delta)

Where "error" is the difference between predicted and actual values.

Key properties:

  • For small errors, it behaves like MSE (quadratic/squared behavior)
  • For large errors, it behaves like MAE (linear behavior)
  • Less sensitive to outliers than MSE, but still provides smooth gradients
  • The delta parameter controls the transition point between quadratic and linear regions

Huber loss is ideal for regression problems where:

  • You want to balance between MSE and MAE
  • Your data might contain outliers
  • You need stable gradients for learning

The delta parameter lets you control the definition of an "outlier" - errors larger than delta are treated as outliers and handled using the more robust linear function.

Constructors

HuberLoss(double)

Initializes a new instance of the HuberLoss class with the specified delta.

public HuberLoss(double delta = 1)

Parameters

delta double

The threshold parameter that controls the transition point. Default is 1.0.

Methods

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

Calculates the derivative of the Huber loss function.

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

Parameters

predicted Vector<T>

The predicted values from the model.

actual Vector<T>

The actual (target) values.

Returns

Vector<T>

A vector containing the derivatives of Huber loss for each prediction.

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

Calculates the Huber loss between predicted and actual values.

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

Parameters

predicted Vector<T>

The predicted values from the model.

actual Vector<T>

The actual (target) values.

Returns

T

The Huber loss value.

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

Calculates both 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.

Remarks

Uses the SmoothL1 kernel which is equivalent to Huber loss with beta = delta.