ILossFunction<T>

Interfaces · AiDotNet.Interfaces

Interface for loss functions used in neural networks.

Known implementations: LossFunctionBase<T>

For Beginners

Loss functions measure how far the predictions of a neural network are from the expected outputs. They provide a signal that helps the network learn by adjusting its weights to minimize this “loss” value.

Think of a loss function as a score that tells you how well or poorly your neural network is performing. A higher loss value means worse performance, while a lower loss value indicates better performance.

Different types of problems require different loss functions. For example:

  • Mean Squared Error is often used for regression problems (predicting numeric values)
  • Cross Entropy is commonly used for classification problems (categorizing inputs)

Training also needs the loss’s derivative — the direction to adjust weights in. You do not write that derivative yourself. Implement ComputeTapeLoss with engine operations and the gradient tape differentiates it for you, exactly as PyTorch differentiates an nn.Module.forward. A loss defines its forward math once; the tape supplies the backward.

Methods

Method Summary
T CalculateLoss(Vector<T> predicted, Vector<T> actual) Calculates the loss between predicted and actual values.
Tensor<T> ComputeTapeLoss(Tensor<T> predicted, Tensor<T> target) Computes the loss as a scalar tensor using tape-differentiable engine operations, so that gradients flow through it automatically via GradientTape.
ValueTuple<T, Tensor<T>> CalculateLossAndGradientGpu(Tensor<T> predicted, Tensor<T> actual) Calculates both loss and gradient on GPU in a single pass.