Photo by SIMON LEE on Unsplash

Dagshub Glossary

Triplet Loss

Triplet loss is a loss function commonly used in machine learning for tasks like face recognition, image retrieval, and similarity learning. Its primary purpose is to train a model to learn embeddings (vector representations) of data points such that similar data points are closer together in the embedding space, while dissimilar ones are farther apart. Triplet loss is often used in Siamese networks or triplet network architectures.

Basic Idea Behind Triplet Loss

Triplet loss is motivated by the idea that when training a model to learn similarity or dissimilarity between data points, you need to provide the model with triplets of examples for each training instance. Each triplet consists of three data points:

  • Anchor: The data point for which we want to learn an embedding.
  • Positive: A data point that is similar to the anchor (e.g., a different image of the same person’s face).
  • Negative: A data point that is dissimilar to the anchor (e.g., an image of a different person’s face).

Embedding Space

The embedding space in the context of triplet loss is a multi-dimensional space where data points are represented as vectors. These vectors, called embeddings, capture essential features or characteristics of the data. The primary purpose of this embedding space is to facilitate the comparison of data points based on their similarity or dissimilarity.

During training, a neural network learns to map data points, such as images or text, into this embedding space. Importantly, in this space, similar data points are positioned close together, and dissimilar data points are positioned far apart. The distances or similarities between embeddings in this space are used to quantify how alike or different data points are from each other.

Triplet Loss Function

The loss function for a triplet (A, P, N) can be defined as follows: Triplet Loss = max(0, d(A, P) - d(A, N) + margin)

Where:

  • d(A, P): The distance (e.g., Euclidean distance or cosine distance) between the anchor and positive embeddings.
  • d(A, N): The distance between the anchor and negative embeddings.
  • margin: A hyperparameter that defines the minimum desired separation between the positive and negative pairs in the embedding space.

Transform your ML development with DagsHub –
Try it now!

Objective of Triplet Loss

The objective of triplet loss is to make the distance between the anchor and positive pair smaller than the distance between the anchor and negative pair by at least the margin value. If d(A, P) + margin < d(A, N), the triplet loss is zero, which is the ideal scenario. If d(A, P) + margin ≥ d(A, N), the model is penalized, and the loss is a positive value that increases as the violation of this condition becomes larger.

Training with Triplet Loss as the Loss Function

During training, you provide the model with a large number of triplets, and the model adjusts its parameters (e.g., neural network weights) to minimize the overall triplet loss across the dataset.

By minimizing the triplet loss, the model learns to project similar data points (e.g., similar faces) closer together in the embedding space and push dissimilar data points (e.g., different faces) farther apart. This learned embedding space can then be used for tasks like face recognition, image retrieval, or any application where measuring similarity between data points is essential.

Related terms

Back to top
Back to top