LossFunctionExtensions

Helpers & Utilities · AiDotNet.Interfaces

Tensor-shaped conveniences over ILossFunction, including the library’s single implementation of “gradient of a loss with respect to a prediction”.

How It Works

Every loss gradient in the library is produced by differentiating ComputeTapeLoss on a GradientTape. Loss functions no longer carry a hand-written derivative, so a backward can no longer disagree with the forward it belongs to — the two are the same expression.

Prefer your own tape when you have one.ComputeGradient opens a tape, records one loss, and discards it. If you are already running a forward pass under a tape, record ComputeTapeLoss on that tape instead: one tape then spans forward and loss, you get parameter gradients directly, and the intermediate prediction-space gradient never has to be materialized. These helpers are for callers holding nothing but two finished tensors.

Methods

Method Summary
T ComputeLoss(ILossFunction<T> lossFunction, Tensor<T> predicted, Tensor<T> actual) Evaluates the loss for tensor-shaped predictions and targets.
Tensor<T> ComputeGradient(ILossFunction<T> lossFunction, Tensor<T> predicted, Tensor<T> actual) Computes the gradient of the loss with respect to predicted for callers working in vector shape.
ValueTuple<T, Tensor<T>> ComputeLossAndGradient(ILossFunction<T> lossFunction, Tensor<T> predicted, Tensor<T> actual) Computes the loss value and its gradient with respect to predicted from a single tape recording.
Vector<T> ComputeGradient(ILossFunction<T> lossFunction, Vector<T> predicted, Vector<T> actual) Computes the gradient of the loss with respect to predicted for callers working in vector shape.