# 度量学习

度量学习是一种机器学习方法，它学习一个面向特定任务的距离函数，使相似的样本被映射得彼此接近，而不相似的样本则相距较远。所学到的度量通常是一个[马哈拉诺比斯距离](https://www.edgepedia.org/zh/mahalanobis-distance)（Mahalanobis distance），在下游被用于k近邻分类、聚类、信息检索和降维。<sup>[1](http://contrib.scikit-learn.org/metric-learn/introduction.html)</sup> 在实践中，输出是一个矩阵或一个线性变换：软件实现将学到的度量暴露为一个矩阵 \( M \)，或作为一个变换 \( L \)，在施加欧几里得距离之前先对点进行嵌入。<sup>[2](http://contrib.scikit-learn.org/metric-learn/generated/metric_learn.ITML.html)</sup>

| 关键事实 | 详情 |
| --- | --- |
| 所学内容 | 一个马哈拉诺比斯矩阵 \( M \)，等价地是一个线性映射 \( L \)，满足 \( M = L^{\top} L \)，用于嵌入数据或计算成对距离<sup>[3](https://ar5iv.labs.arxiv.org/html/1812.05944)</sup> |
| 距离形式 | \( d_{M}(x, x') = \sqrt{(x - x')^{\top} M (x - x')} \)，通常以平方形式优化以避免开平方<sup>[4](https://arxiv.org/pdf/1306.6709)</sup> |
| 监督格式 | 成对等价与不等价约束，或三元组；也可以直接使用类别标签<sup>[5](https://www.cs.cmu.edu/~liuy/frame_survey_v2.pdf)</sup> |
| 约束条件 | \( M \) 必须是对称半正定的，该函数才能成为一个度量；\( M = I \) 时退化为欧几里得距离<sup>[3](https://ar5iv.labs.arxiv.org/html/1812.05944)</sup> |
| 经典算法 | 面向聚类约束的凸规划、NCA、LMNN 和 ITML<sup>[3](https://ar5iv.labs.arxiv.org/html/1812.05944)</sup> |
| 典型用途 | kNN分类、聚类、检索与排序、人脸验证、生物信息学、推荐系统<sup>[1](http://contrib.scikit-learn.org/metric-learn/introduction.html)</sup> |
| 实用求解器备注 | ITML 不需要特征值计算或半正定规划<sup>[6](https://www.cs.utexas.edu/~inderjit/public_papers/itml_icml07.pdf)</sup> |

## 工作原理

马哈拉诺比斯距离通过在差值向量与自身之间插入一个对称半正定矩阵 \( M \)，将[欧几里得距离](https://www.edgepedia.org/zh/euclidean-distance)推广开来：\[ d_{M}(x, x') = \sqrt{(x - x')^{\top} M (x - x')} \] 学习在平方距离上进行，从而避免了开平方。<sup>[4](https://arxiv.org/pdf/1306.6709)</sup> 当 \( M \) 满秩时，该函数是一个真正的距离；当它秩亏时，则是一个伪距离。欧几里得距离是 \( M = I \) 的特殊情形。<sup>[3](https://ar5iv.labs.arxiv.org/html/1812.05944)</sup>

由于 \( M \) 是半正定的，它可以分解为 \( M = L^{\top} L \)，简单的代数运算表明，马哈拉诺比斯距离等于经过全局线性变换后的欧几里得距离 \( \lVert Lx - Ly \rVert \)。<sup>[7](https://people.bu.edu/bkulis/pubs/ftml_metric_learning.pdf)</sup> 因此，学习 \( M \) 与学习线性映射 \( L \) 是同一问题的等价视角；学习 \( L \) 是无约束的，而学习 \( M \) 则需要维持半正定约束。<sup>[3](https://ar5iv.labs.arxiv.org/html/1812.05944)</sup>

与欧几里得距离相比，学到的度量所带来的好处是方差感知能力：欧几里得距离忽略数据云的散布，而马哈拉诺比斯距离则考虑方差，能够将一个点分配给散布更紧密的云团。<sup>[8](https://ar5iv.labs.arxiv.org/html/2201.09267)</sup> 严格来说，一个半正定的学习到的马哈拉诺比斯距离是一个伪度量：它满足非负性、对称性和三角不等式，但不一定满足不可分辨者同一性；当矩阵为正定时，它才是一个真正的度量。<sup>[1](http://contrib.scikit-learn.org/metric-learn/introduction.html)</sup>

## 实现方法

监督度量学习可以直接使用类别标签，也可以使用成对、三元组或其他约束，这些约束本身可能由标签导出：等价约束表示两个点应当接近，不等价约束表示它们应当远离，方法分为满足所有约束的全局方法和局部方法。<sup>[5](https://www.cs.cmu.edu/~liuy/frame_survey_v2.pdf)</sup> 大多数方法拟合如下一般目标 \[ M^{*} = \arg\min_{M} \; \ell(M, S, D, R) + \lambda R(M) \] 其中 \( \ell \) 惩罚被违反的必连、不可连或相对约束，\( R(M) \) 是正则项，\( \lambda \geq 0 \)。<sup>[9](https://researchers.lille.inria.fr/abellet/talks/metric_learning_tutorial_CIL.pdf)</sup>

主要的算法选择有：

- **面向聚类的凸规划。** 一种早期形式在 \( A \succeq 0 \) 和 \( \sum_{(x_i, x_j) \in D} \lVert x_i - x_j \rVert^{2}_{A} \geq 1 \) 的约束下，最小化 \( \sum_{(x_i, x_j) \in S} \lVert x_i - x_j \rVert^{2}_{A} \)，对于一般的对称 \( d \times d \) 矩阵，自由参数的数量为 \( d(d+1)/2 \)，若附加对角矩阵限制则为 \( d \)。<sup>[5](https://www.cs.cmu.edu/~liuy/frame_survey_v2.pdf)</sup>
- **NCA。** 优化随机最近邻分类器的期望留一误差，使用 \( M = L^{\top} L \) 分解；它是非凸的，专为1NN分类器量身定制，矩形 \( L \) 允许进行降维。<sup>[4](https://arxiv.org/pdf/1306.6709)</sup><sup> • </sup><sup>[9](https://researchers.lille.inria.fr/abellet/talks/metric_learning_tutorial_CIL.pdf)</sup>
- **LMNN。** 优化一个两项误差，惩罚到同类目标邻居的大距离和到异类冒充者的小距离，作为凸半正定规划求解；次梯度求解器可处理数百万到数十亿个约束。<sup>[3](https://ar5iv.labs.arxiv.org/html/1812.05944)</sup><sup> • </sup><sup>[4](https://arxiv.org/pdf/1306.6709)</sup>
- **ITML。** 最小化 LogDet 散度，受相似性约束 \( d_A(x_i, x_j) \leq u \) 和不相似性约束 \( d_A(x_i, x_j) \geq l \) 的约束；每次布雷格曼投影更新 \( M_{t+1} = M_t + \beta M_t (x_i - x_j)(x_i - x_j)^{\top} M_t \) 对每个约束的代价为 \( O(d^{2}) \)，并自动强制半正定性。<sup>[6](https://www.cs.utexas.edu/~inderjit/public_papers/itml_icml07.pdf)</sup><sup> • </sup><sup>[10](https://cvg.cit.tum.de/_media/teaching/ss2017/ml4cv/metric_learning.pdf)</sup>
- **深度损失。** 三元组损失以一定的间隔将相似的邻居拉近、将不相似的点推远；在深度度量学习中，性能共同取决于损失函数、采样策略和网络结构。<sup>[8](https://ar5iv.labs.arxiv.org/html/2201.09267)</sup><sup> • </sup><sup>[11](https://www.mdpi.com/2073-8994/11/9/1066)</sup>

随后，学到的度量被接入 kNN 分类、聚类或检索流水线；metric-learn 库在一个统一的、兼容 scikit-learn 的 API 后面实现了十种算法，涵盖监督、成对、三元组和四元组学习器。<sup>[12](https://www.jmlr.org/papers/volume21/19-678/19-678.pdf)</sup>

## 起源

度量学习作为一个领域，归功于埃里克·P·邢及其同事 2002 年的论文《距离度量学习及其在带辅助信息的聚类中的应用》，该文提出了一种凸优化算法，从相似点对以及（可选的）不相似点对中学习距离度量；综述将这项工作标记为该领域诞生的节点。<sup>[13](https://dl.acm.org/doi/10.1016/j.neunet.2018.06.003)</sup> 2004 年，雅各布·戈德伯格及其同事发表了邻域成分分析（Neighbourhood Components Analysis）。<sup>[12](https://www.jmlr.org/papers/volume21/19-678/19-678.pdf)</sup> 大间隔最近邻形式出现在 2005 年 NIPS 论文中，作者是当时在宾夕法尼亚大学的 [基利安·Q·温伯格](https://www.edgepedia.org/zh/kilian-q-weinberger)、约翰·布利策和劳伦斯·K·索尔。<sup>[14](https://dl.acm.org/doi/10.5555/2976248.2976433)</sup> 更近期，深度度量学习将注意力转向了用成对、三元组和批次级损失训练的嵌入网络。<sup>[8](https://ar5iv.labs.arxiv.org/html/2201.09267)</sup>

## 变体

全局方法学习一个满足所有约束的度量，而局部方法学习多个独立的度量，例如为每个训练样本簇学习一个度量，这可以改善结果。<sup>[5](https://www.cs.cmu.edu/~liuy/frame_survey_v2.pdf)</sup><sup> • </sup><sup>[14](https://dl.acm.org/doi/10.5555/2976248.2976433)</sup> 线性马哈拉诺比斯方法分为凸和非凸两个子族，核化通过学习 \( \phi \) 的特征空间中的线性变换，将它们扩展到非线性映射，仅需要数据的内积。<sup>[7](https://people.bu.edu/bkulis/pubs/ftml_metric_learning.pdf)</sup> 命名的变体包括 RCA，它从类内协方差矩阵的加权和中学习一个满秩度量，具有闭式解 \( B = (K/N) \hat{C}^{-1} \)，还有 MCML、LEGO、GB-LMNN，以及产生闭式解的信息几何方法 IGML 和 KIGML。<sup>[5](https://www.cs.cmu.edu/~liuy/frame_survey_v2.pdf)</sup><sup> • </sup><sup>[9](https://researchers.lille.inria.fr/abellet/talks/metric_learning_tutorial_CIL.pdf)</sup><sup> • </sup><sup>[15](http://proceedings.mlr.press/v5/wang09c/wang09c.pdf)</sup> 2024 年的一篇综述将迁移度量学习——即在一个域上学到的度量被适配到另一个域——组织为直接度量近似、子空间近似、距离近似和分布近似；直接近似由于参数数量多，在高维中容易过拟合，这促使采用低秩分解 \( A_m = U_m U_m^{\top} \)，其中 \( U_m \in \mathbb{R}^{d_m \times r} \)，从而为迁移创建一个共享子空间。<sup>[16](https://link.springer.com/article/10.1007/s44336-024-00003-8)</sup>

## 应用

已记录的应用领域包括计算机视觉任务，如图像分类、人脸识别、跟踪和标注；信息检索与排序；生物信息学；音乐推荐；身份验证；医学、安全、语音识别、推荐系统、行人重识别和亲属关系验证。<sup>[4](https://arxiv.org/pdf/1306.6709)</sup><sup> • </sup><sup>[9](https://researchers.lille.inria.fr/abellet/talks/metric_learning_tutorial_CIL.pdf)</sup><sup> • </sup><sup>[3](https://ar5iv.labs.arxiv.org/html/1812.05944)</sup>

NIPS 2005 的 LMNN 论文报告了在七个大小和难度各异的 MNIST 手写数字数据集上 1.3% 的测试错误率。<sup>[14](https://dl.acm.org/doi/10.5555/2976248.2976433)</sup> 在 ITML 论文针对 UCI 数据集与 MCML、LMNN 和 2002 年凸方法的基准比较中，ITML 是唯一在所有数据集上都在指定的 95% 置信区间内获得最优错误率的算法。<sup>[6](https://www.cs.utexas.edu/~inderjit/public_papers/itml_icml07.pdf)</sup> 增益并非普遍适用：在四个软件错误报告数据集中的三个上，学到的度量相比欧几里得基线仅带来小幅改善。<sup>[6](https://www.cs.utexas.edu/~inderjit/public_papers/itml_icml07.pdf)</sup>

## 局限与替代方案

线性度量往往无法捕捉多模态数据或非线性类别边界；标准的补救措施是核化、非线性度量形式和多重局部度量，尽管核方法可能加剧过拟合并存在扩展性问题。<sup>[9](https://researchers.lille.inria.fr/abellet/talks/metric_learning_tutorial_CIL.pdf)</sup><sup> • </sup><sup>[11](https://www.mdpi.com/2073-8994/11/9/1066)</sup> 通过投影梯度维持半正定约束需要复杂度为 \( O(d^{3}) \) 的特征值分解，在高维中代价高昂，而在秩约束下优化 \( M \) 是 NP 难的。<sup>[4](https://arxiv.org/pdf/1306.6709)</sup> LMNN 由于缺乏正则化，在高维中容易过拟合，并且对欧几里得距离选择目标邻居的好坏敏感；NCA 的距离矩阵有 \( O(d^{2}) \) 个参数，没有收敛到局部极大值的保证，并且在训练样本不足时容易过拟合。<sup>[4](https://arxiv.org/pdf/1306.6709)</sup><sup> • </sup><sup>[3](https://ar5iv.labs.arxiv.org/html/1812.05944)</sup> 在深度度量学习中，低效的成对或三元组采样浪费时间和内存，训练时的复杂度可能按成对的 \( n^{2} \)、三元组的 \( n^{3} \) 和四元组的 \( n^{4} \) 增长。<sup>[11](https://www.mdpi.com/2073-8994/11/9/1066)</sup> 度量学习假设至少有一些监督可用，这将其与 PCA 等无监督降维区分开来；RCA 从必连（等价）约束中学习度量，而 DCA 可以额外利用不可连约束；两者都可以看作线性判别分析的扩展。<sup>[7](https://people.bu.edu/bkulis/pubs/ftml_metric_learning.pdf)</sup><sup> • </sup><sup>[5](https://www.cs.cmu.edu/~liuy/frame_survey_v2.pdf)</sup>

## References

1. [What is Metric Learning?, metric-learn 0.7.0 documentation](http://contrib.scikit-learn.org/metric-learn/introduction.html)
2. [metric_learn.ITML, metric-learn 0.7.0 documentation](http://contrib.scikit-learn.org/metric-learn/generated/metric_learn.ITML.html)
3. [A Tutorial on Distance Metric Learning: Mathematical Foundations, Algorithms, Experimental Analysis, Prospects and Challenges](https://ar5iv.labs.arxiv.org/html/1812.05944)
4. [A Survey on Metric Learning for Feature Vectors and Structured Data (Bellet, Habrard, Sebban)](https://arxiv.org/pdf/1306.6709)
5. [Distance Metric Learning: A Comprehensive Survey (Liu et al., CMU)](https://www.cs.cmu.edu/~liuy/frame_survey_v2.pdf)
6. [Information-Theoretic Metric Learning (ICML 2007)](https://www.cs.utexas.edu/~inderjit/public_papers/itml_icml07.pdf)
7. [Metric Learning: A Survey (Kulis, Foundations and Trends in ML)](https://people.bu.edu/bkulis/pubs/ftml_metric_learning.pdf)
8. [Spectral, Probabilistic, and Deep Metric Learning: Tutorial and Survey (Ghojogh et al.)](https://ar5iv.labs.arxiv.org/html/2201.09267)
9. [Tutorial on Metric Learning (Bellet, CIL tutorial slides)](https://researchers.lille.inria.fr/abellet/talks/metric_learning_tutorial_CIL.pdf)
10. [Metric Learning (TUM lecture slides, Chiotellis)](https://cvg.cit.tum.de/_media/teaching/ss2017/ml4cv/metric_learning.pdf)
11. [Deep Metric Learning: A Survey](https://www.mdpi.com/2073-8994/11/9/1066)
12. [metric-learn: Metric Learning Algorithms in Python (JMLR 2020 software paper)](https://www.jmlr.org/papers/volume21/19-678/19-678.pdf)
13. [Survey and experimental study on metric learning methods (Neural Networks)](https://dl.acm.org/doi/10.1016/j.neunet.2018.06.003)
14. [Distance metric learning for large margin nearest neighbor classification (NIPS 2005 proceedings version)](https://dl.acm.org/doi/10.5555/2976248.2976433)
15. [An information geometry approach for distance metric learning (Wang, Jin, ICML 2009)](http://proceedings.mlr.press/v5/wang09c/wang09c.pdf)
16. [Transfer metric learning: algorithms, applications and outlooks (Vicinagearth, 2024)](https://link.springer.com/article/10.1007/s44336-024-00003-8)

---
*Initially written Sep 29, 2026 · Reviewed: Sep 30, 2026 · Edited: Sep 30, 2026; Oct 21, 2026 · Last review: Sep 30, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Machine learning and neural computation › Machine learning methods › Supervised, unsupervised, and semi-supervised learning › Supervised learning concepts

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/metric-learning

English original: https://www.edgepedia.org/metric-learning
