Class LossFunctionBase<T>
- Namespace
- AiDotNet.LossFunctions
- Assembly
- AiDotNet.dll
Base class for loss function implementations.
public abstract class LossFunctionBase<T> : ILossFunction<T>
Type Parameters
TThe numeric type used for calculations (e.g., float, double).
- Inheritance
-
LossFunctionBase<T>
- Implements
- Derived
- Inherited Members
- Extension Methods
Constructors
LossFunctionBase()
Initializes a new instance of the LossFunctionBase class.
protected LossFunctionBase()
Fields
NumOps
Provides operations for the numeric type T.
protected readonly INumericOperations<T> NumOps
Field Value
- INumericOperations<T>
Properties
Engine
Gets the global execution engine for vector operations.
protected IEngine Engine { get; }
Property Value
- IEngine
Methods
CalculateDerivative(Vector<T>, Vector<T>)
Calculates the derivative (gradient) of the loss function.
public abstract Vector<T> CalculateDerivative(Vector<T> predicted, Vector<T> actual)
Parameters
predictedVector<T>The predicted values from the model.
actualVector<T>The actual (target) values.
Returns
- Vector<T>
A vector containing the derivatives of the loss with respect to each prediction.
CalculateLoss(Vector<T>, Vector<T>)
Calculates the loss between predicted and actual values.
public abstract T CalculateLoss(Vector<T> predicted, Vector<T> actual)
Parameters
predictedVector<T>The predicted values from the model.
actualVector<T>The actual (target) values.
Returns
- T
The loss value.
CalculateLossAndGradientGpu(IGpuTensor<T>, IGpuTensor<T>)
Calculates both loss and gradient on GPU in a single pass. Default implementation falls back to separate calls.
public virtual (T Loss, IGpuTensor<T> Gradient) CalculateLossAndGradientGpu(IGpuTensor<T> predicted, IGpuTensor<T> actual)
Parameters
predictedIGpuTensor<T>The predicted GPU tensor from the model.
actualIGpuTensor<T>The actual (target) GPU tensor.
Returns
ValidateVectorLengths(Vector<T>, Vector<T>)
Validates that the predicted and actual vectors have the same length.
protected void ValidateVectorLengths(Vector<T> predicted, Vector<T> actual)
Parameters
predictedVector<T>The predicted values vector.
actualVector<T>The actual values vector.
Exceptions
- ArgumentException
Thrown when vectors have different lengths.