Class FocalLoss<T>
- Namespace
- AiDotNet.LossFunctions
- Assembly
- AiDotNet.dll
Implements the Focal Loss function, which gives more weight to hard-to-classify examples.
public class FocalLoss<T> : LossFunctionBase<T>, ILossFunction<T>
Type Parameters
TThe numeric type used for calculations (e.g., float, double).
- Inheritance
-
FocalLoss<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
For Beginners: Focal Loss was designed to handle class imbalance in classification problems, especially for object detection tasks where background examples vastly outnumber foreground objects.
It modifies the standard cross-entropy loss by adding a factor that reduces the loss contribution from easy-to-classify examples and increases the importance of hard-to-classify examples.
The formula is: -a(1-p)^? * log(p) for positive class -(1-a)p^? * log(1-p) for negative class Where:
- p is the model's estimated probability for the correct class
- a is a weighting factor that balances positive vs negative examples
- ? (gamma) is the focusing parameter that adjusts how much to focus on hard examples
Key properties:
- When ?=0, Focal Loss equals Cross-Entropy Loss
- Higher ? values increase focus on hard-to-classify examples
- a helps handle class imbalance by giving more weight to the minority class
This loss function is ideal for:
- Highly imbalanced datasets
- One-stage object detectors
- Any classification task where easy negatives dominate training
Constructors
FocalLoss(double, double)
Initializes a new instance of the FocalLoss class.
public FocalLoss(double gamma = 2, double alpha = 0.25)
Parameters
gammadoubleThe focusing parameter that down-weights easy examples. Default is 2.0.
alphadoubleThe weighting factor for positive class. Default is 0.25.
Methods
CalculateDerivative(Vector<T>, Vector<T>)
Calculates the derivative of the Focal Loss with respect to the predicted values.
public override Vector<T> CalculateDerivative(Vector<T> predicted, Vector<T> actual)
Parameters
predictedVector<T>The predicted probabilities from the model.
actualVector<T>The actual (target) values (typically 0 or 1).
Returns
- Vector<T>
A vector containing the gradient of the loss with respect to each prediction.
CalculateLoss(Vector<T>, Vector<T>)
Calculates the Focal Loss between predicted and actual values.
public override T CalculateLoss(Vector<T> predicted, Vector<T> actual)
Parameters
predictedVector<T>The predicted probabilities from the model.
actualVector<T>The actual (target) values (typically 0 or 1).
Returns
- T
The focal loss value.
CalculateLossAndGradientGpu(IGpuTensor<T>, IGpuTensor<T>)
Calculates both Focal 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.