Loss Functions

Complete reference for all 37+ loss functions in AiDotNet.



Classification Losses

Binary Classification

Loss FunctionFormulaUse Case
BinaryCrossEntropyLoss<T>-[y·log(p) + (1-y)·log(1-p)]Binary classification
BCEWithLogitsLoss<T>BCELoss with sigmoidNumerically stable
HingeLoss<T>max(0, 1 - y·p)SVM-style
SquaredHingeLoss<T>(max(0, 1 - y·p))²Smooth hinge
var loss = new BinaryCrossEntropyLoss<float>();
var value = loss.Compute(predictions, targets);

Multi-Class Classification

Loss FunctionDescriptionUse Case
CrossEntropyLoss<T>Standard cross entropyMulti-class
NLLLoss<T>Negative log likelihoodAfter log_softmax
SoftmaxCrossEntropyLoss<T>Softmax + cross entropyCombined operation
var loss = new CrossEntropyLoss<float>(
    labelSmoothing: 0.1f,
    ignoreIndex: -100);

Focal Loss (Imbalanced Data)

Loss FunctionDescriptionUse Case
FocalLoss<T>Down-weights easy examplesClass imbalance
BinaryFocalLoss<T>Focal loss for binaryHighly imbalanced
var loss = new FocalLoss<float>(
    gamma: 2.0f,  // Focusing parameter
    alpha: 0.25f); // Class weight

Regression Losses

Standard Losses

Loss FunctionFormulaUse Case
MSELoss<T>(y - p)²General regression
MAELoss<T>|y - p|Robust to outliers
HuberLoss<T>Quadratic near 0, linear elsewhereBalanced
SmoothL1Loss<T>Huber with delta=1Object detection
LogCoshLoss<T>log(cosh(y - p))Smooth approximation
var loss = new HuberLoss<float>(delta: 1.0f);

Quantile Losses

Loss FunctionDescriptionUse Case
QuantileLoss<T>Quantile regressionPrediction intervals
PinballLoss<T>Same as quantileProbabilistic forecasting
var loss = new QuantileLoss<float>(quantile: 0.9f);

Segmentation Losses

Loss FunctionDescriptionUse Case
DiceLoss<T>1 - Dice coefficientSegmentation
IoULoss<T>1 - Intersection over UnionObject detection
TverskyLoss<T>Generalized DiceImbalanced segmentation
FocalTverskyLoss<T>Focal + TverskyHard pixels
LovaszHingeLoss<T>Lovasz extensionIoU optimization
BoundaryLoss<T>Distance to boundaryEdge-aware
var loss = new DiceLoss<float>(smooth: 1e-6f);

Contrastive/Metric Learning

Loss FunctionDescriptionUse Case
ContrastiveLoss<T>Siamese networksSimilarity learning
TripletLoss<T>Anchor, positive, negativeFace recognition
TripletMarginLoss<T>Triplet with marginEmbedding learning
NTXentLoss<T>Normalized temperatureSelf-supervised
InfoNCELoss<T>Information NCEContrastive learning
SupConLoss<T>Supervised contrastiveWith labels
var loss = new TripletLoss<float>(margin: 1.0f);

Reconstruction Losses

Loss FunctionDescriptionUse Case
ReconstructionLoss<T>MSE for reconstructionAutoencoders
VAELoss<T>Reconstruction + KL divergenceVAE training
BetaVAELoss<T>VAE with betaDisentanglement
SSIMLoss<T>1 - SSIMImage quality
PerceptualLoss<T>Feature space distanceSuper-resolution
var loss = new VAELoss<float>(
    reconstructionWeight: 1.0f,
    klWeight: 0.001f);

GAN Losses

Loss FunctionDescriptionUse Case
AdversarialLoss<T>Standard GAN lossBasic GANs
WassersteinLoss<T>Wasserstein distanceWGAN
HingeLoss<T>GAN hinge lossSpectral normalization
LSGANLoss<T>Least squares GANStable training
NonSaturatingLoss<T>Non-saturatingBetter gradients
var generatorLoss = new NonSaturatingLoss<float>(mode: LossMode.Generator);
var discriminatorLoss = new NonSaturatingLoss<float>(mode: LossMode.Discriminator);

Ranking Losses

Loss FunctionDescriptionUse Case
MarginRankingLoss<T>Pairwise rankingLearning to rank
MultiMarginLoss<T>Multi-class marginMulti-class ranking
ListwiseLoss<T>Full list rankingSearch ranking
var loss = new MarginRankingLoss<float>(margin: 1.0f);

Regularization Losses

Loss FunctionDescriptionUse Case
L1Loss<T>L1 regularizationSparsity
L2Loss<T>L2 regularizationWeight decay
ElasticNetLoss<T>L1 + L2Combined
KLDivLoss<T>KL divergenceDistribution matching
JSDivLoss<T>Jensen-ShannonSymmetric KL

Sequence Losses

Loss FunctionDescriptionUse Case
CTCLoss<T>Connectionist TemporalSpeech recognition
SequenceLoss<T>Padded sequencesSeq2seq
LabelSmoothingLoss<T>Smoothed targetsNLP models
var loss = new CTCLoss<float>(blank: 0, zeroInfinity: true);

Combined Losses

// Combine multiple losses
var loss = new CombinedLoss<float>(
    new (ILoss<float>, float)[]
    {
        (new CrossEntropyLoss<float>(), 1.0f),
        (new DiceLoss<float>(), 0.5f),
        (new FocalLoss<float>(), 0.3f)
    });

Usage Examples

With AiModelBuilder

var result = await new AiModelBuilder<float, Tensor<float>, int>()
    .ConfigureModel(model)
    .ConfigureLossFunction(new FocalLoss<float>(gamma: 2.0f))
    .BuildAsync(trainData, trainLabels);

Custom Training Loop

var loss = new CrossEntropyLoss<float>(labelSmoothing: 0.1f);

foreach (var batch in dataLoader)
{
    var predictions = model.Forward(batch.Input);
    var lossValue = loss.Compute(predictions, batch.Target);
    lossValue.Backward();
    optimizer.Step();
}

Loss Selection Guide

TaskRecommended Loss
Binary classificationBCEWithLogitsLoss
Multi-class classificationCrossEntropyLoss
Imbalanced classificationFocalLoss
RegressionMSELoss or HuberLoss
SegmentationDiceLoss + CrossEntropyLoss
Object detectionSmoothL1Loss + FocalLoss
Face recognitionTripletLoss
AutoencodersReconstructionLoss
VAEVAELoss
GANsAdversarialLoss or WassersteinLoss
Speech recognitionCTCLoss