Class TripletLoss<T>
- Namespace
- AiDotNet.LossFunctions
- Assembly
- AiDotNet.dll
Implements the Triplet Loss function for learning similarity embeddings.
public class TripletLoss<T> : LossFunctionBase<T>, ILossFunction<T>
Type Parameters
TThe numeric type used for calculations (e.g., float, double).
- Inheritance
-
TripletLoss<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
For Beginners: Triplet Loss helps create embeddings (numerical representations) where similar items are close together and different items are far apart in a vector space.
It works with triplets of data:
- Anchor: A reference point (e.g., a person's face)
- Positive: An example similar to the anchor (e.g., another image of the same person)
- Negative: An example different from the anchor (e.g., an image of a different person)
The loss encourages the model to make the distance between the anchor and positive smaller than the distance between the anchor and negative by at least a specified margin.
This loss function is commonly used in:
- Face recognition systems
- Image retrieval applications
- Recommendation systems
- Any task where you need to learn meaningful similarity metrics
By minimizing triplet loss, the model learns to create an embedding space where semantically similar items cluster together and dissimilar items are pushed apart.
Constructors
TripletLoss(double)
Initializes a new instance of the TripletLoss class.
public TripletLoss(double margin = 1)
Parameters
margindoubleThe minimum desired difference between positive and negative distances. Default is 1.0.
Methods
CalculateDerivative(Matrix<T>, Matrix<T>, Matrix<T>)
Calculates the gradients of the Triplet Loss function for anchor, positive, and negative samples.
public (Matrix<T>, Matrix<T>, Matrix<T>) CalculateDerivative(Matrix<T> anchor, Matrix<T> positive, Matrix<T> negative)
Parameters
anchorMatrix<T>The anchor samples matrix.
positiveMatrix<T>The positive samples matrix (similar to anchor).
negativeMatrix<T>The negative samples matrix (dissimilar to anchor).
Returns
- (Matrix<T> U, Matrix<T> B, Matrix<T> V)
A tuple containing the gradients for anchor, positive, and negative samples.
Exceptions
- ArgumentException
Thrown when input matrices have inconsistent dimensions.
CalculateDerivative(Vector<T>, Vector<T>)
This method is not used for Triplet Loss as it requires multiple input vectors.
public override Vector<T> CalculateDerivative(Vector<T> predicted, Vector<T> actual)
Parameters
predictedVector<T>The predicted values vector.
actualVector<T>The actual (target) values vector.
Returns
- Vector<T>
Throws NotSupportedException.
Exceptions
- NotSupportedException
Always thrown as TripletLoss requires three input matrices.
CalculateLoss(Matrix<T>, Matrix<T>, Matrix<T>)
Calculates the Triplet Loss for embedding learning.
public T CalculateLoss(Matrix<T> anchor, Matrix<T> positive, Matrix<T> negative)
Parameters
anchorMatrix<T>The anchor samples (reference points).
positiveMatrix<T>The positive samples (similar to anchors).
negativeMatrix<T>The negative samples (dissimilar to anchors).
Returns
- T
A scalar value representing the triplet loss.
Exceptions
- ArgumentException
Thrown when input matrices have inconsistent dimensions.
CalculateLoss(Vector<T>, Vector<T>)
This method is not used for Triplet Loss as it requires multiple input vectors.
public override T CalculateLoss(Vector<T> predicted, Vector<T> actual)
Parameters
predictedVector<T>The predicted values vector.
actualVector<T>The actual (target) values vector.
Returns
- T
Throws NotSupportedException.
Exceptions
- NotSupportedException
Always thrown as TripletLoss requires three input matrices.
CalculateLossAndGradientGpu(IGpuTensor<T>, IGpuTensor<T>, IGpuTensor<T>)
Calculates Triplet Loss on GPU for batched input tensors.
public (T Loss, IGpuTensor<T> AnchorGradient, IGpuTensor<T> PositiveGradient, IGpuTensor<T> NegativeGradient) CalculateLossAndGradientGpu(IGpuTensor<T> anchor, IGpuTensor<T> positive, IGpuTensor<T> negative)
Parameters
anchorIGpuTensor<T>The anchor GPU tensor (batch of embeddings).
positiveIGpuTensor<T>The positive GPU tensor (similar to anchors).
negativeIGpuTensor<T>The negative GPU tensor (dissimilar to anchors).
Returns
- (T Loss, IGpuTensor<T> AnchorGradient, IGpuTensor<T> PositiveGradient, IGpuTensor<T> NegativeGradient)
A tuple containing the loss value and gradient tensors for anchor, positive, and negative.