Table of Contents

Class ContrastiveLoss<T>

Namespace
AiDotNet.LossFunctions
Assembly
AiDotNet.dll

Implements the Contrastive Loss function for learning similarity metrics.

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

Type Parameters

T

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

Inheritance
ContrastiveLoss<T>
Implements
Inherited Members
Extension Methods

Remarks

For Beginners: Contrastive Loss helps a model learn to identify whether two examples are similar or dissimilar. It works with pairs of examples and their similarity label (1 for similar, 0 for dissimilar).

For similar pairs, the loss penalizes distance between them, encouraging them to be close together. For dissimilar pairs, the loss penalizes proximity below a certain margin, encouraging them to be at least that far apart.

The formula has two components:

  • For similar pairs (y=1): distance²
  • For dissimilar pairs (y=0): max(0, margin - distance)²

Contrastive Loss is commonly used in:

  • Siamese neural networks
  • Face verification systems (determining if two faces are the same person)
  • Signature verification
  • Any situation where you need to learn a similarity metric between pairs

This approach is simpler than Triplet Loss as it only requires pairs of examples rather than triplets.

Constructors

ContrastiveLoss(double)

Initializes a new instance of the ContrastiveLoss class.

public ContrastiveLoss(double margin = 1)

Parameters

margin double

The minimum desired distance between dissimilar examples. Default is 1.0.

Methods

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

This method is not used for Contrastive Loss as it requires two input vectors and a similarity label.

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

Parameters

predicted Vector<T>

The predicted values vector.

actual Vector<T>

The actual (target) values vector.

Returns

Vector<T>

Throws NotSupportedException.

Exceptions

NotSupportedException

Always thrown as ContrastiveLoss requires two input vectors and a similarity label.

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

Calculates the gradients of the Contrastive Loss function for both output vectors.

public (Vector<T>, Vector<T>) CalculateDerivative(Vector<T> output1, Vector<T> output2, T similarityLabel)

Parameters

output1 Vector<T>

The first output vector.

output2 Vector<T>

The second output vector.

similarityLabel T

A value of 1 indicates similar pairs, 0 indicates dissimilar pairs.

Returns

(Vector<T> mean, Vector<T> logVar)

A tuple containing the gradients for both output vectors.

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

This method is not used for Contrastive Loss as it requires two input vectors and a similarity label.

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

Parameters

predicted Vector<T>

The predicted values vector.

actual Vector<T>

The actual (target) values vector.

Returns

T

Throws NotSupportedException.

Exceptions

NotSupportedException

Always thrown as ContrastiveLoss requires two input vectors and a similarity label.

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

Calculates the Contrastive Loss between two output vectors based on their similarity.

public T CalculateLoss(Vector<T> output1, Vector<T> output2, T similarityLabel)

Parameters

output1 Vector<T>

The first output vector.

output2 Vector<T>

The second output vector.

similarityLabel T

A value of 1 indicates similar pairs, 0 indicates dissimilar pairs.

Returns

T

The contrastive loss value.

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

Calculates Contrastive Loss on GPU for batched input tensors.

public (T Loss, IGpuTensor<T> Gradient1, IGpuTensor<T> Gradient2) CalculateLossAndGradientGpu(IGpuTensor<T> output1, IGpuTensor<T> output2, IGpuTensor<T> labels)

Parameters

output1 IGpuTensor<T>

The first output GPU tensor.

output2 IGpuTensor<T>

The second output GPU tensor.

labels IGpuTensor<T>

The similarity labels GPU tensor (1 for similar, 0 for dissimilar).

Returns

(T Loss, IGpuTensor<T> Gradient1, IGpuTensor<T> Gradient2)

A tuple containing the loss value and gradient tensors for both outputs.