Class LogCoshLoss<T>
- Namespace
- AiDotNet.LossFunctions
- Assembly
- AiDotNet.dll
Implements the Log-Cosh loss function, a smooth approximation of Mean Absolute Error.
public class LogCoshLoss<T> : LossFunctionBase<T>, ILossFunction<T>
Type Parameters
TThe numeric type used for calculations (e.g., float, double).
- Inheritance
-
LogCoshLoss<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
For Beginners: Log-Cosh loss is a smooth approximation of the Mean Absolute Error. It calculates the logarithm of the hyperbolic cosine of the difference between predictions and actual values.
This loss function has several desirable properties:
- It's smooth everywhere (unlike Huber loss which has a point where its derivative is not continuous)
- It's less affected by outliers than Mean Squared Error
- It behaves like Mean Squared Error for small errors and Mean Absolute Error for large errors
- Its derivatives are well-defined and bounded, which helps prevent gradient explosions during training
Log-Cosh loss is particularly useful for regression problems where:
- You want the smoothness of MSE but with better robustness to outliers
- You need stable gradients for model training
- You want a compromise between MSE and MAE
Methods
CalculateDerivative(Vector<T>, Vector<T>)
Calculates the derivative of the Log-Cosh 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 Log-Cosh loss for each prediction.
CalculateLoss(Vector<T>, Vector<T>)
Calculates the Log-Cosh 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 Log-Cosh loss value.
CalculateLossAndGradientGpu(IGpuTensor<T>, IGpuTensor<T>)
Calculates both Log-Cosh 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.