Table of Contents

Class JaccardLoss<T>

Namespace
AiDotNet.LossFunctions
Assembly
AiDotNet.dll

Implements the Jaccard loss function, commonly used for measuring dissimilarity between sets.

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

Type Parameters

T

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

Inheritance
JaccardLoss<T>
Implements
Inherited Members
Extension Methods

Remarks

For Beginners: Jaccard loss measures how dissimilar two sets are. It's calculated as 1 minus the size of the intersection divided by the size of the union.

The formula is: 1 - |A n B| / |A ? B| Where:

  • A n B is the intersection of sets A and B (elements in both)
  • A ? B is the union of sets A and B (elements in either)

For continuous values (like probabilities), the intersection is the sum of the minimum values, and the union is the sum of the maximum values at each position.

Key properties:

  • A value of 0 means perfect overlap (identical sets)
  • A value of 1 means no overlap at all
  • It's symmetric: Jaccard(A,B) = Jaccard(B,A)
  • It's a proper distance metric, suitable for measuring dissimilarity

Jaccard loss is particularly useful for:

  • Image segmentation tasks
  • Set similarity problems
  • Binary classification problems
  • Tasks where the positive class is rare (imbalanced data)

It's often a better choice than pixel-wise losses (like MSE) for segmentation tasks because it directly optimizes for the overlap of segments.

Constructors

JaccardLoss()

Initializes a new instance of the JaccardLoss class.

public JaccardLoss()

Methods

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

Calculates the derivative of the Jaccard loss function.

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

Parameters

predicted Vector<T>

The predicted values vector.

actual Vector<T>

The actual (ground truth) values vector.

Returns

Vector<T>

A vector containing the derivatives of the Jaccard loss with respect to each predicted value.

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

Calculates the Jaccard loss between predicted and actual values.

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

Parameters

predicted Vector<T>

The predicted values vector.

actual Vector<T>

The actual (ground truth) values vector.

Returns

T

The Jaccard loss value.