ISupportsLossFunction<T>

Interfaces · AiDotNet.Interfaces

Implemented by models whose loss function can be replaced by the caller after construction.

Known implementations: AutoformerModel<T>, InformerModel<T>, NBEATSModel<T>, NHiTSModel<T>, NeuralNetworkBase<T>

For Beginners

The loss function is how a model measures how wrong it is. Most models let you pick one. A few are built around a specific loss and cannot be given a different one — those don’t implement this interface, so asking for a different loss tells you so instead of quietly doing nothing.

How It Works

This is the seam AiModelBuilder.ConfigureLossFunction uses. Setting the loss on the model — rather than somewhere in the facade — is deliberate: the loss must reach the component that computes gradients, and the optimizer already adopts a model’s DefaultLossFunction when the model is attached (GradientBasedOptimizerBase.OnModelChanged). Injecting here therefore reaches both the model’s own training path and the optimizer’s, and keeps the loss reported in metrics identical to the loss actually optimized.

Not every model can implement this. Where the loss is intrinsic to the architecture — DeepAR’s Gaussian likelihood over a (mean, scale) head, or the Temporal Fusion Transformer’s quantile pinball loss, which defines the shape of its output — an arbitrary ILossFunction is not merely suboptimal but incorrect, and the model should not implement this interface. The facade reports that as an error rather than silently corrupting the model.

Methods

Method Summary
void SetLossFunction(ILossFunction<T> lossFunction) Replaces the loss function this model trains against.