Table of Contents

Class LossFunctionBase<T>

Namespace
AiDotNet.LossFunctions
Assembly
AiDotNet.dll

Base class for loss function implementations.

public abstract class LossFunctionBase<T> : ILossFunction<T>

Type Parameters

T

The 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

predicted Vector<T>

The predicted values from the model.

actual Vector<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

predicted Vector<T>

The predicted values from the model.

actual Vector<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

predicted IGpuTensor<T>

The predicted GPU tensor from the model.

actual IGpuTensor<T>

The actual (target) GPU tensor.

Returns

(T Loss, IGpuTensor<T> Gradient)

A tuple containing the loss value and gradient tensor.

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

predicted Vector<T>

The predicted values vector.

actual Vector<T>

The actual values vector.

Exceptions

ArgumentException

Thrown when vectors have different lengths.