Similarity Learning lacks a framework. So we built one

Vasnetsov Andrey
Qdrant Vector Database Blog
4 min readJun 28, 2022

Similarity Learning is not new but is an actively developing area of machine learning. It is also known as Metric Learning in literature, but I prefer the other term, as it better represents the main idea.

In a nutshell, Similarity Learning deals with the models which try to predict if a pair of objects are similar or not. The fact is that “similar” objects do not have to be identical. Moreover, they can exist in different forms, e.g., a picture and its text description — anything that we could match with each other.

This seemingly simple principle allows you to solve many problems, from search and recommendations to classification and anomaly detection.

Why Similarity Learning?

In addition to its wide range of applications, Similarity Learning also has certain advantages over classical approaches.

There is a number of issues you are likely to face, if you are attempting to solve machine learning problems using traditional approaches like classification:

  • Cold start problem — you may not have marked-up data before the product is launched. And to mark them up, you have to launch the product.
  • Dataset compatibility — you have found several datasets suitable for your task, but they have slightly different labeling, which makes it impossible to use them together.
  • Requirement changes — you have trained the model, but after launching it you found out that there is a new class that is not included in the markup.
  • Impossible tweaking — once the model is deployed, it is impossible to alter it’s behavior in some specific corner cases without manual condition checking.
  • Lack of Explanability — explaining predictions of the classification model is a form of art. Only the simplest models have some form of explainability available.

Similarity Learning offers an alternative that eliminates these disadvantages.

Unlike traditional classification models it does not rely on a predefined set of labels, but instead learns the similarities between objects. And turns out that this approach gives a number of benefits on data collection, modelling, and deployment stages.

It is much easier to collect datasets for similarity learning, because any classification dataset is also a similarity dataset. The information about classes can be directly used to determine similarity between objects.

And therefore, multiple classification datasets could be combined into a single similarity dataset even if initial labels were not compatible. And in some cases you won’t even need manual markup at all. If the data consists of several components, such as multimodal pairs, you can use a self-supervised approach.

On the modelling stage similarity learning is also much flexible — it does not depend on a fixed number of classes, so the new categories could be introduced by just a simple extension of the reference set. Same is also true for the deployment stage, where you can introduce new examples without any downtime and redeployment.

And finally, examining the similarities between objects resulting from the model can provide insights into what the model is guided by when making predictions.

Why do we need a new framework?

Many general-purpose frameworks allow you to train Computer Vision or NLP tasks quickly. However, Similarity Learning has peculiarities, which usually require an additional layer of complexity on top of the usual pipelines.

So, for example, the batch size in the training of similarity models has a much greater role than in other models. Labels either do not exist or are handled in a completely different way. In many cases, the model is already pre-trained, which also adjusts the process.

By focusing on Similarity Learning, we can better support such specialized requirements while simplifying development and accelerating the training process.

Introducing Quaterion

Developing Similarity Learning models one after another, we began to notice patterns that helped us generalize and bring all our experience with training and fine-tuning such models into one package.

Please meet Quaterion — an open-source, blazing fast, customizable and scalable framework for training and fine-tuning similarity learning models.

It not only lets you develop Similarity Learning models as easily as a regular classifier, but you can do it an order of magnitude faster, even on the weakest GPU.

Comparison of training speed with Quaterion Cache feature (recording speed is unchanged)

Thanks to the caching mechanism, you can conduct dozens of experiments while you finish a cup of coffee. At the same time, it allows you to use batch sizes that are unattainable in other ways.

Quaterion is built on top of PyTorch Lightning — a framework for high-performance AI research. It takes care of all the tasks involved in constructing a training loops for ML models: epochs management, logging, early stopping, check-pointing, Distributed training, and many more.

In addition to PyTorch Lightning functionality, Quaterion provides a scaffold for defining:

  • Fine-tunable similarity learning models — Encoders and Head Layers
  • Datasets and Data Loaders for representing similarity information
  • Loss functions for similarity learning
  • Metrics for evaluating model performance

What is next?

You can start using Quaterion right now!

We have prepared examples of models that you can fine-tune with Quaterion:

  • NLP Tutorial — Answer on cloud provider’s frequently asked questions with similarity learning
  • CV Tutorial — Search for similar cars in novel categories

If you need more inspiration, check out our live demos, solutions and industries where similarity learning is already successfully applied.

We also look forward to seeing you in our discord community, chatting and answering questions from people working on similar tasks.

--

--

Vasnetsov Andrey
Qdrant Vector Database Blog

CTO, creator of Qdrant. Interested in Vector Search, Metric Learning, Self-Supervised and One-Shot learning.