Table of Contents

Class PoissonLoss<T>

Namespace
AiDotNet.LossFunctions
Assembly
AiDotNet.dll

Implements the Poisson loss function for count data modeling.

public class PoissonLoss<T> : LossFunctionBase<T>, ILossFunction<T>

Type Parameters

T

The numeric type used for calculations (e.g., float, double).

Inheritance
PoissonLoss<T>
Implements
Inherited Members
Extension Methods

Remarks

For Beginners: Poisson loss is designed for modeling count data where the target values represent the number of occurrences of an event in a fixed interval.

Examples include:

  • Number of customer arrivals per hour
  • Number of network failures per day
  • Number of disease cases per region

The loss is derived from the Poisson probability distribution, which is ideal for modeling rare events where we know the average rate of occurrence.

The formula is: predicted - actual * log(predicted) + log(actual!)

Since log(actual!) is constant with respect to predictions, it can be omitted during optimization, so the loss is often implemented as just: predicted - actual * log(predicted)

Poisson loss is appropriate when:

  • Your target values are non-negative counts
  • The variance of the data is approximately equal to the mean
  • You're modeling the rate or frequency of events

Constructors

PoissonLoss()

Initializes a new instance of the PoissonLoss class.

public PoissonLoss()

Methods

CalculateDerivative(Vector<T>, Vector<T>)

Calculates the derivative of the Poisson loss function.

public override Vector<T> CalculateDerivative(Vector<T> predicted, Vector<T> actual)

Parameters

predicted Vector<T>

The predicted values from the model (should be positive).

actual Vector<T>

The actual (target) values (typically counts or rates).

Returns

Vector<T>

A vector containing the gradient of the loss with respect to each prediction.

CalculateLoss(Vector<T>, Vector<T>)

Calculates the Poisson loss between predicted and actual values.

public override T CalculateLoss(Vector<T> predicted, Vector<T> actual)

Parameters

predicted Vector<T>

The predicted values from the model (should be positive).

actual Vector<T>

The actual (target) values (typically counts or rates).

Returns

T

The average Poisson loss across all samples.

CalculateLossAndGradientGpu(IGpuTensor<T>, IGpuTensor<T>)

Calculates both Poisson loss and gradient on GPU in a single efficient pass.

public override (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.