Similarity learning

Last updated 2026.03.25

{{Short description|Supervised learning of a similarity function}} '''Similarity learning''' is an area of [[Supervised learning|supervised machine learning]] in [[artificial intelligence]]. It is closely related to [[regression (machine learning)|regression]] and [[classification in machine learning|classification]], but the goal is to learn a [[similarity function]] that measures how [[Similarity (philosophy)|similar]] or related two objects are. It has applications in [[ranking]], in [[recommendation systems]], visual identity tracking, face verification, and speaker verification.

== Learning setup ==

There are four common setups for similarity and metric distance learning.

; ''[[Regression (machine learning)|Regression]] similarity learning'' : In this setup, pairs of objects are given (x_i^1, x_i^2) together with a measure of their similarity y_i \in R . The goal is to learn a function that approximates f(x_i^1, x_i^2) \sim y_i for every new labeled triplet example (x_i^1, x_i^2, y_i). This is typically achieved by minimizing a regularized loss \min_W \sum_i loss(w;x_i^1, x_i^2,y_i) + reg(w). ; ''[[Classification in machine learning|Classification]] similarity learning'' : Given are pairs of similar objects (x_i, x_i^+) and non similar objects (x_i, x_i^-). An equivalent formulation is that every pair (x_i^1, x_i^2) is given together with a binary label y_i \in {0,1} that determines if the two objects are similar or not. The goal is again to learn a classifier that can decide if a new pair of objects is similar or not. ; ''Ranking similarity learning'' : Given are triplets of objects (x_i, x_i^+, x_i^-) whose relative similarity obey a predefined order: x_i is known to be more similar to x_i^+ than to x_i^-. The goal is to learn a function f such that for any new triplet of objects (x, x^+, x^-), it obeys f(x, x^+) > f(x, x^-) ([[contrastive learning]]). This setup assumes a weaker form of supervision than in regression, because instead of providing an exact [[Similarity measure|measure of similarity]], one only has to provide the relative order of similarity. For this reason, ranking-based similarity learning is easier to apply in real large-scale applications.{{cite journal| last1 = Chechik | first1 = G. | last2 = Sharma | first2 = V. | last3 = Shalit | first3 = U. | last4 = Bengio | first4 = S. | title=Large Scale Online Learning of Image Similarity Through Ranking|journal=Journal of Machine Learning Research|year=2010|volume=11|pages=1109–1135|url=https://www.jmlr.org/papers/volume11/chechik10a/chechik10a.pdf}} ; [[Locality sensitive hashing]] (LSH)Gionis, Aristides, Piotr Indyk, and Rajeev Motwani. "Similarity search in high dimensions via hashing." VLDB. Vol. 99. No. 6. 1999. : [[Hash function|Hashes]] input items so that similar items map to the same "buckets" in memory with high probability (the number of buckets being much smaller than the universe of possible input items). It is often applied in [[nearest neighbor search]] on large-scale high-dimensional data, e.g., image databases, document collections, time-series databases, and genome databases.{{cite web | first1 = A.|last1=Rajaraman |first2= J.|last2=Ullman|author2-link=Jeffrey Ullman | url = http://infolab.stanford.edu/~ullman/mmds.html | title=Mining of Massive Datasets, Ch. 3. | year = 2010 }}

A common approach for learning similarity is to model the similarity function as a [[bilinear form]]. For example, in the case of ranking similarity learning, one aims to learn a matrix W that parametrizes the similarity function f_W(x, z) = x^T W z . When data is abundant, a common approach is to learn a [[siamese network]] – a deep network model with parameter sharing.

== Metric learning ==

Similarity learning is closely related to ''distance metric learning''. Metric learning is the task of learning a distance function over objects. A [[Metric (mathematics)|metric]] or [[distance function]] has to obey four axioms: [[non-negative|non-negativity]], [[identity of indiscernibles]], [[symmetry]] and [[subadditivity]] (or the triangle inequality). In practice, metric learning algorithms ignore the condition of identity of indiscernibles and learn a pseudo-metric.

When the objects x_i are vectors in R^d, then any matrix W in the symmetric positive semi-definite cone S_+^d defines a distance pseudo-metric of the space of x through the form D_W(x_1, x_2)^2 = (x_1-x_2)^{\top} W (x_1-x_2). When W is a symmetric positive definite matrix, D_W is a metric. Moreover, as any symmetric positive semi-definite matrix W \in S_+^d can be decomposed as W = L^{\top}L where L \in R^{e \times d} and e \geq rank(W), the distance function D_W can be rewritten equivalently D_W(x_1, x_2)^2 = (x_1-x_2)^{\top} L^{\top}L (x_1-x_2) = | L (x_1-x_2) |_2^2. The distance D_W(x_1, x_2)^2=| x_1' - x_2' |_2^2 corresponds to the Euclidean distance between the transformed [[feature vector]]s x_1'= Lx_1 and x_2'= Lx_2.

Many formulations for metric learning have been proposed.{{cite arXiv | last1 = Bellet | first1 = A. | last2 = Habrard | first2 = A. | last3 = Sebban | first3 = M. |eprint=1306.6709 |class=cs.LG |title=A Survey on Metric Learning for Feature Vectors and Structured Data |year=2013}}{{cite journal| last = Kulis | first = B.| title=Metric Learning: A Survey | journal=Foundations and Trends in Machine Learning | volume = 5| issue = 4| pages = 287–364| year=2012 | url=https://www.nowpublishers.com/article/Details/MAL-019| doi = 10.1561/2200000019| url-access = subscription}} Some well-known approaches for metric learning include learning from relative comparisons,{{cite journal| last1 = Schultz | first1 = M. | last2 = Joachims | first2 = T. | title=Learning a distance metric from relative comparisons|journal=Advances in Neural Information Processing Systems |volume=16|year=2004|pages=41–48|url=https://papers.nips.cc/paper/2366-learning-a-distance-metric-from-relative-comparisons.pdf}} which is based on the [[triplet loss]], [[large margin nearest neighbor]],{{cite journal| last1 = Weinberger | first1 = K. Q. | last2 = Blitzer | first2 = J. C. | last3 = Saul | first3 = L. K. | title=Distance Metric Learning for Large Margin Nearest Neighbor Classification|journal=Advances in Neural Information Processing Systems |volume=18|year=2006|pages=1473–1480|url=http://books.nips.cc/papers/files/nips18/NIPS2005_0265.pdf}} and information theoretic metric learning (ITML).{{cite journal | last1 = Davis | first1 = J. V. | last2 = Kulis | first2 = B. | last3 = Jain | first3 = P. | last4 = Sra | first4 = S. | last5 = Dhillon | first5 = I. S. | title=Information-theoretic metric learning | journal=International Conference in Machine Learning| year=2007 | pages=209–216 | url=http://www.cs.utexas.edu/users/pjain/itml/}}

In [[statistics]], the [[covariance]] matrix of the data is sometimes used to define a distance metric called [[Mahalanobis distance]].

== Applications == Similarity learning is used in information retrieval for [[learning to rank]], in face verification or face identification,{{cite book| last1 = Guillaumin | first1 = M. | last2 = Verbeek | first2 = J. | last3 = Schmid | first3 = C. | chapter = Is that you? Metric learning approaches for face identification | pages = 498–505 | title=2009 IEEE 12th International Conference on Computer Vision|chapter-url=https://hal.inria.fr/docs/00/58/50/36/PDF/verbeek09iccv2.pdf|year=2009|doi=10.1109/ICCV.2009.5459197 | isbn = 978-1-4244-4420-5 | url = https://inria.hal.science/inria-00439290 }}{{cite book| last1 = Mignon | first1 = A. | last2 = Jurie | first2 = F. | chapter = PCCA: A new approach for distance learning from sparse pairwise constraints | pages = 2666–2672 | title=2012 IEEE Conference on Computer Vision and Pattern Recognition|year=2012|chapter-url=https://hal.archives-ouvertes.fr/docs/00/80/60/07/PDF/12_cvpr_ldca.pdf|doi=10.1109/CVPR.2012.6247987 | isbn = 978-1-4673-1228-8 | url = https://hal.science/hal-00806007 }} and in [[recommendation systems]]. Also, many machine learning approaches rely on some metric. This includes [[unsupervised learning]] such as [[cluster analysis|clustering]], which groups together close or similar objects. It also includes supervised approaches like [[K-nearest neighbor algorithm]] which rely on labels of nearby objects to decide on the label of a new object. Metric learning has been proposed as a preprocessing step for many of these approaches.{{cite journal| last1 = Xing | first1 = E. P. | last2 = Ng | first2 = A. Y. | last3 = Jordan | first3 = M. I. | last4 = Russell | first4 = S. | title=Distance Metric Learning, with Application to Clustering with Side-information | journal=Advances in Neural Information Processing Systems |volume=15 | year=2002| pages = 505–512 | url=https://ai.stanford.edu/~ang/papers/nips02-metric.pdf}}

== Scalability ==

Metric and similarity learning scale quadratically with the dimension of the input space, as can easily see when the learned metric has a bilinear form f_W(x, z) = x^T W z . Scaling to higher dimensions can be achieved by enforcing a sparseness structure over the matrix model, as done with HDSL,{{Cite journal| last1=Liu | last2=Bellet | last3=Sha| title=Similarity Learning for High-Dimensional Sparse Data|year=2015|journal=International Conference on Artificial Intelligence and Statistics (AISTATS)| arxiv=1411.2374 | bibcode=2014arXiv1411.2374L |url=http://jmlr.org/proceedings/papers/v38/liu15.pdf}} and with COMET.{{Cite journal | last1=Atzmon | last2=Shalit | last3=Chechik | title=Learning Sparse Metrics, One Feature at a Time | journal=J. Mach. Learn. Research |year=2015|url=http://jmlr.org/proceedings/papers/v44/atzmon2015.pdf}}

== Software ==

*metric-learn{{cite web | url=https://github.com/scikit-learn-contrib/metric-learn | title=Scikit-learn-contrib/Metric-learn | website=[[GitHub]] }} is a [[free software]] Python library which offers efficient implementations of several supervised and weakly-supervised similarity and metric learning algorithms. The API of metric-learn is compatible with [[scikit-learn]].{{Cite journal | last1=Vazelhes | last2=Carey | last3=Tang | last4=Vauquier | last5=Bellet | title=metric-learn: Metric Learning Algorithms in Python | journal=J. Mach. Learn. Research |year=2020| arxiv=1908.04710 |url=https://www.jmlr.org/papers/volume21/19-678/19-678.pdf}}

  • OpenMetricLearning{{cite web | url=https://github.com/OML-Team/open-metric-learning | title=OML-Team/Open-metric-learning | website=[[GitHub]] }} is a [[Python (programming language)|Python]] framework to train and validate the models producing high-quality embeddings.

== Further information == For further information on this topic, see the surveys on metric and similarity learning by Bellet et al. and Kulis.

==See also== *[[Kernel method]] *[[Latent semantic analysis]] *[[Learning to rank]]

== References == {{reflist}}

[[Category:Machine learning]] [[Category:Semantic relations]]