Class CharbonnierLoss<T>
- Namespace
- AiDotNet.LossFunctions
- Assembly
- AiDotNet.dll
Implements the Charbonnier loss function, a smooth approximation of L1 loss.
public class CharbonnierLoss<T> : LossFunctionBase<T>, ILossFunction<T>
Type Parameters
TThe numeric type used for calculations (e.g., float, double).
- Inheritance
-
CharbonnierLoss<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
For Beginners: Charbonnier loss is a differentiable approximation of the absolute error (L1 loss).
The formula is: L(x, y) = sqrt((x - y)² + ε²)
Where:
- x is the predicted value
- y is the actual value
- ε (epsilon) is a small constant (typically 1e-6 or 1e-9)
Key properties:
- Like L1 loss, it's robust to outliers
- Unlike L1 loss, it's differentiable everywhere (smooth at zero)
- Provides more stable gradients for training deep neural networks
- Widely used in video super-resolution and image restoration
Charbonnier loss is preferred over L1 loss in deep learning because:
- L1 loss has an undefined derivative at zero
- Charbonnier loss provides smooth gradients that help with optimization
- The epsilon parameter controls the "sharpness" of the approximation
Reference: Charbonnier et al., "Two deterministic half-quadratic regularization algorithms for computed imaging", ICIP 1994.
Constructors
CharbonnierLoss(double)
Initializes a new instance of the CharbonnierLoss class.
public CharbonnierLoss(double epsilon = 1E-06)
Parameters
epsilondoubleThe smoothing parameter. Default is 1e-6.
Remarks
For Beginners: The epsilon parameter controls how closely Charbonnier loss approximates L1 loss: - Smaller epsilon: Closer to L1 loss but less smooth - Larger epsilon: Smoother gradients but less like L1 loss
The default value of 1e-6 works well for most applications.
Methods
CalculateDerivative(Vector<T>, Vector<T>)
Calculates the derivative of the Charbonnier loss function.
public override Vector<T> CalculateDerivative(Vector<T> predicted, Vector<T> actual)
Parameters
predictedVector<T>The predicted values from the model.
actualVector<T>The actual (target) values.
Returns
- Vector<T>
A vector containing the derivatives of Charbonnier loss for each prediction.
Remarks
The derivative is: (predicted - actual) / sqrt((predicted - actual)² + ε²)
This derivative is always well-defined (unlike L1 loss) because the denominator is always at least ε, avoiding division by zero.
CalculateLoss(Vector<T>, Vector<T>)
Calculates the Charbonnier loss between predicted and actual values.
public override T CalculateLoss(Vector<T> predicted, Vector<T> actual)
Parameters
predictedVector<T>The predicted values from the model.
actualVector<T>The actual (target) values.
Returns
- T
The Charbonnier loss value.
Remarks
The loss is computed as the mean of sqrt((predicted - actual)² + ε²) across all elements.
CalculateLossAndGradientGpu(IGpuTensor<T>, IGpuTensor<T>)
Calculates both Charbonnier 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.