FocalLoss<T>

Models & Types · AiDotNet.LossFunctions

Implements the Focal Loss function, which gives more weight to hard-to-classify examples.

Inheritance: LossFunctionBase<T> → FocalLoss<T>

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

Example

using AiDotNet.LossFunctions;
using AiDotNet.Tensors.LinearAlgebra;

var loss = new FocalLoss<float>();
var predicted = new Vector<float>(new[] { 0.9f, 0.2f, 0.7f });
var actual = new Vector<float>(new[] { 1.0f, 0.0f, 1.0f });

float value = loss.CalculateLoss(predicted, actual);
Console.WriteLine($"FocalLoss = {value:F4}");

Constructors

Constructor Summary
FocalLoss(double gamma, double alpha) Initializes a new instance of the FocalLoss class.

Methods

Method Summary
T CalculateLoss(Vector<T> predicted, Vector<T> actual) Calculates the Focal Loss between predicted and actual values.
Tensor<T> ComputeTapeLoss(Tensor<T> predicted, Tensor<T> target)
ValueTuple<T, Tensor<T>> CalculateLossAndGradientGpu(Tensor<T> predicted, Tensor<T> actual) Calculates both Focal Loss and gradient on GPU in a single efficient pass.