Active Hackathon

A Beginners Guide to Deep Metric Learning

Learning the similarity between objects has a dominant role in human cognitive processes and artificial systems for recognition and classification.

Learning the similarity between objects has a dominant role in human cognitive processes and artificial systems for recognition and classification. Using an appropriate distance metric, the metric learning attempts to quantify sample similarity while conducting learning tasks. Metric learning techniques, which typically use a linear projection, are limited in their capacity to tackle non-linear real-world scenarios. Kernel approaches are employed in metric learning to overcome this problem. In this post, we will understand what metric learning and deep metric learning are and how deep metric learning can address the challenges faced by metric learning. We will also explore in this post how metric learning can be used in practice. The major points to be discussed in this article are outlined below.

Table of Contents 

  1. Working of Various Learning Schemes
  2. What is Metric Learning?
  3. Deep Metric Learning
  4. Important Points of Deep Metric Learning 

Let’s start the discussion by discussing, in brief, the working of various learning schemes and based on this understanding we will have a look into metric learning. 


Sign up for your weekly dose of what's up in emerging technology.

Working of Various Learning Scheme

After computers achieved the ability to recognize objects, the idea of machine learning evolved, which allows computers to learn clearly without being explicitly programmed. Today, we can use machine learning to make our lives easier in a variety of ways. Face recognition, medical diagnosis, intrusion detection systems, audio recognition, voice recognition, text mining, object identification, and more applications use it. As a result, machine learning can be said to provide successful answers for complicated issues and enormous amounts of data.

In machine learning applications, k-nearest neighbours, support vector machines, and Naive Bayes classifiers can be employed. Despite the fact that these algorithms have a certain level of classification performance, it is feasible to better describe the data. These algorithms do not move an existing dataset to a new location. In terms of categorization, each attribute has a different impact.

As a result, feature weighting could be applied prior to categorization. The dataset can also be shifted from its original location to a new location. Data transformation algorithms, such as Principal Component Analysis, are used to do this. Linear Discriminant Analysis, as well as others, have benefited. 

Metric learning is a method of determining similarity or dissimilarity between items based on a distance metric. Metric learning seeks to increase the distance between dissimilar things while reducing the distance between similar objects. As a result, there are ways that calculate distance information, such as k-nearest neighbours, as well as approaches that transform data into a new representation. 

The method is built on a W projection matrix, while the metric learning procedures are relocated to the transformation space using distance information. Current research is focused on the Mahalanobis distance in general. The metric learning strategy is offered when the Mahalanobis distance is turned into the Euclidean distance, and it is based on the decomposition of the covariance matrix and the usage of symmetric positive definite matrices when executing these operations.

Deep learning and metric learning have been combined in recent years to create the notion of deep metric learning. The principle of similarity between samples underpins deep metric learning. Let’s look at metric learning first before moving on to deep metric learning.

What is Metric Learning?

In terms of classification and clustering, each dataset has its own set of issues. Distance metrics with poor learning ability, regardless of the problem, can be said to fail to produce successful data classification outcomes. To produce satisfactory results on the input data, an appropriate distance metric is required. Several studies have been undertaken to employ metric learning methodologies to address this issue.

By evaluating data, metric learning creates a new distance metric. A metric learning strategy that performs the learning process on data will be able to distinguish the sample data better. Metric learning’s main goal is to learn a new metric that will minimize distances between samples of the same class while increasing distances between samples of different classes. While metric learning seeks to bring similar things closer together, it increases the gap between dissimilar objects, as shown in Figure 1c below.

Figure-1 (Source)

When it comes to metric learning studies in the literature, it’s clear that they’re all linked to Mahalanobis’ distance metric. The training samples are  X=[x1,x2,…,xN] ∈ Rd×N where xi∈Rd

 is the ith training example and N is the total number of training samples. The following formula is used to compute the distance between xi and xj:

The qualities of nonnegativity, the identity of indiscernibles, symmetry, and the triangle inequality must all be present in dM(xi,xj). M must be symmetric and semidefinite positive. To be positive semidefinite, all of M’s eigenvalues or determinants must be positive or zero.

Having a better ability to represent data would undoubtedly allow us to make more accurate predictions in classification and clustering challenges. The goal of metric learning is to derive an appropriate distance metric from data. The distance metric is a novel data format that uses the similarity relationship between samples to deliver a more meaningful and strong discriminating model.

Let’s take a look at a short practical example of metric learning in which we will be testing the iris dataset against Neighborhood Components Analysis (NCA) which is a distance metric learning approach aimed at improving the accuracy of nearest neighbour classification when compared to the traditional Euclidean distance. On the training set, the algorithm maximizes a stochastic variant of the leave-one-out k-nearest neighbours (KNN) score. It can also learn a low-dimensional linear data transformation for data presentation and categorization.

For this, we will be using the sci-kit learn to package and metric-learn package, installation of both can be done simply by the pip command. 

from metric_learn import NCA
from sklearn.datasets import load_iris
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.neighbors import KNeighborsClassifier
def evaluate(clf):
  X, y = load_iris(return_X_y=True)
  return cross_val_score(clf, X, y)
clf = make_pipeline(NCA(), KNeighborsClassifier())
clf1 = KNeighborsClassifier()
print("Validation Score Before Metric-Learn",evaluate(clf1))
print("Validation Score After Metric-Learn",evaluate(clf))

Now let’s have a look at Deep Metric Learning. 

Deep Metric Learning

The capacity of traditional machine learning approaches to process data on raw data limits them. As a result, feature engineering is required before classification or clustering operations, such as preprocessing and feature extraction. All of these processes necessitate expertise and are not covered by the classification system. Deep learning, on the other hand, learns the higher level of data from the categorization structure itself. The basic distinction between classic machine learning approaches and deep learning is demonstrated in this perspective.

The Euclidean, Mahalanobis, Matusita, Bhattacharyya, and Kullback-Leibler distances are basic similarity metrics used for data classification. These pre-defined measures, on the other hand, offer restricted data classification capabilities. To address this challenge, a strategy based on the Mahalanobis metric was developed to categorize the data into traditional metric learning.

Deep metric learning provides problem-based solutions that are caused by learning from raw data by utilizing deep architectures and finding embedded feature similarity through nonlinear subspace learning. When it comes to the extent of deep metric learning, it includes everything from video comprehension to human re-identification, medical difficulties, three-dimensional (3D) modelling, face verification and recognition, and signature verification.

Figure 2 (Source)

The majority of existing deep learning methods rely on a deep architectural backdrop rather than a distance measure in a new data representation space. Distance-based techniques, on the other hand, have recently become one of the most fascinating subjects in deep learning. 

Deep metric learning, which tries to increase the distance between similar data while decreasing the distance between dissimilar samples, is directly related to the distance between samples. The metric loss function has been used in deep learning to carry out this process. It pushes samples from different classes apart from each other while striving to bring examples from the same classes closer together shown in below figure2 a.

Some experiments on the MNIST picture collection were conducted while utilizing contrastive loss to show this approach with a figure2. In Figure 2b, distance values reflect the average of distances between similar and different images. The distance value for similar photos dropped step by step after each epoch, as shown in the Figure. 

The distance value for different photos, on the other hand, increased at the same time. For comparable or different images, the Siamese network distance relationship has been successfully used in each epoch (Figure 2b). This experiment demonstrates that the approach’s goal can be achieved successfully.

Important Points of Deep Metric Learning

Informed input samples, the topology of the network model, and a metric loss function are the three basic components of deep metric learning. Although deep metric learning focuses on metric loss functions, informative sample selection is also critical in classification and clustering. 

One of the most important components that contribute to the success of deep metric learning is informative samples. The sampling technique has the potential to improve the network’s success as well as its training pace. In the contrastive loss, the simplest technique to determine train samples is to use randomly generated positive or negative pairs of objects.

To recap, even if we construct excellent mathematical models and architectures, the network’s learning capacity will be limited by the discriminating power of the samples it is presented with. To help the network learn and achieve better representation, distinguishing training instances should be supplied to it. As a result, the impact of the sample relationship on deep metric learning should be thoroughly investigated.

As a result, sample selection will be highly important as a preprocessing step before applying the deep metric learning model to boost the success of the network model. According to the findings in the literature, research of negative mining in deep metric learning has a high effect value.


Deep metric learning aims to train a similarity metric that uses samples to compute the similarity or dissimilarity of two or more objects. Face recognition, face verification, person re-identification, 3D form retrieval, semantic textual similarity, speaker verification, patient similarity, and other images, video, text, and audio tasks, all have substantial similarity challenges. Here we tried to understand the concept of metric learning and deep metric learning and highlighted its important points. Most of the discussions in this article are inspired by the recent research paper published by Mahmut K and Hasan S B whose link is added below for more details.


More Great AIM Stories

Vijaysinh Lendave
Vijaysinh is an enthusiast in machine learning and deep learning. He is skilled in ML algorithms, data manipulation, handling and visualization, model building.

Our Upcoming Events

Conference, in-person (Bangalore)
Cypher 2022
21-23rd Sep

Conference, in-person (Bangalore)
Machine Learning Developers Summit (MLDS) 2023
19-20th Jan

Conference, in-person (Bangalore)
Data Engineering Summit (DES) 2023
21st Apr, 2023

3 Ways to Join our Community

Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

Telegram Channel

Discover special offers, top stories, upcoming events, and more.

Subscribe to our newsletter

Get the latest updates from AIM

Council Post: Enabling a Data-Driven culture within BFSI GCCs in India

Data is the key element across all the three tenets of engineering brilliance, customer-centricity and talent strategy and engagement and will continue to help us deliver on our transformation agenda. Our data-driven culture fosters continuous performance improvement to create differentiated experiences and enable growth.

Ouch, Cognizant

The company has reduced its full-year 2022 revenue growth guidance to 8.5% – 9.5% in constant currency from the 9-11% in the previous quarter