CompositeLoss<T>
Models & Types · AiDotNet.LossFunctions
A weighted sum of several loss functions: L = sum_i w_i * L_i.
Inheritance: LossFunctionBase<T> → CompositeLoss<T>
For Beginners
a loss function scores how wrong a prediction is. Some models are trained against several such scores at once, each contributing a different amount — for instance “mostly care about focal loss, and a little about dice loss”. This class expresses exactly that: give it the individual losses and how much each one counts.
How It Works
Many published training objectives are explicitly a linear combination of simpler terms, and implementing only one of those terms is a silent departure from the paper. Examples in this library:
Weights are used as given and are NOT normalised, because published ratios (such as SAM’s 20:1) are stated as absolute coefficients. Normalising would silently rescale the objective and change the effective learning rate.
Example
using AiDotNet.LossFunctions;
using AiDotNet.Tensors.LinearAlgebra;
var loss = new CompositeLoss<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($"CompositeLoss = {value:F4}");
Constructors
| Constructor | Summary |
|---|---|
CompositeLoss(ValueTuple<LossFunctionBase<T>, double>[] terms) |
Creates a composite loss from (loss, weight) pairs. |
Properties
| Property | Summary |
|---|---|
int TermCount { get; } |
Gets the number of terms in this composite objective. |
Methods
| Method | Summary |
|---|---|
T CalculateLoss(Vector<T> predicted, Vector<T> actual) |
|
Tensor<T> ComputeTapeLoss(Tensor<T> predicted, Tensor<T> target) |