Table of Contents

Class BinaryCrossEntropyLoss<T>

Namespace
AiDotNet.LossFunctions
Assembly
AiDotNet.dll

Implements the Binary Cross Entropy loss function for binary classification problems.

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

Type Parameters

T

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

Inheritance
BinaryCrossEntropyLoss<T>
Implements
Inherited Members
Extension Methods

Remarks

For Beginners: Binary Cross Entropy is used when classifying data into two categories, such as spam/not-spam, positive/negative sentiment, or disease/no-disease.

The formula is: BCE = -(1/n) * ?[actual * log(predicted) + (1-actual) * log(1-predicted)]

It measures how well predicted probabilities match actual binary outcomes:

  • When the actual value is 1, it evaluates how close the prediction is to 1
  • When the actual value is 0, it evaluates how close the prediction is to 0

Key properties:

  • Predicted values must be probabilities (between 0 and 1)
  • Actual values are typically 0 or 1 (binary labels)
  • It heavily penalizes confident mistakes (predicting 0.01 when the true value is 1)
  • It's the preferred loss function for binary classification problems

Constructors

BinaryCrossEntropyLoss()

Initializes a new instance of the BinaryCrossEntropyLoss class.

public BinaryCrossEntropyLoss()

Methods

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

Calculates the derivative of the Binary Cross Entropy loss function.

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

Parameters

predicted Vector<T>

The predicted values (probabilities between 0 and 1).

actual Vector<T>

The actual (target) values (typically 0 or 1).

Returns

Vector<T>

A vector containing the derivatives of BCE for each prediction.

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

Calculates the Binary Cross Entropy loss between predicted and actual values.

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

Parameters

predicted Vector<T>

The predicted values (probabilities between 0 and 1).

actual Vector<T>

The actual (target) values (typically 0 or 1).

Returns

T

The binary cross entropy loss value.

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

Calculates both BCE 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 (probabilities between 0 and 1).

actual IGpuTensor<T>

The actual (target) GPU tensor.

Returns

(T Loss, IGpuTensor<T> Gradient)

A tuple containing the loss value and gradient tensor.