# 三元组网络

三元组网络是一种用于度量学习的神经网络架构：一个嵌入网络的三个副本共享权重，分别处理锚点、正样本和负样本，然后由一个目标函数比较由此得到的锚点—正样本距离和锚点—负样本距离；常用的一种此类目标是基于边际的三元组合页损失，而最初的三元组网络论文则对两个距离施加 softmax 比较。其结果是得到一个嵌入空间，其中输入之间的距离衡量相似度，从而在推理时无需类别标签即可支持验证、检索和聚类。<sup>[1](https://ar5iv.labs.arxiv.org/html/1412.6622)</sup> 同样的损失经 FaceNet 推广用于人脸识别，将每张图像映射为一个 128 维向量，其平方 L2 距离直接对应人脸相似度。<sup>[2](https://doi.org/10.48550/arxiv.1503.03832)</sup>

| 关键事实 | 值 |
|---|---|
| 架构 | 一个前馈网络的三个实例，参数共享；输出为两个 L2 距离，\( [\lVert \mathrm{Net}(x)-\mathrm{Net}(x^{-})\rVert_{2},\ \lVert \mathrm{Net}(x)-\mathrm{Net}(x^{+})\rVert_{2} ] \)<sup>[1](https://ar5iv.labs.arxiv.org/html/1412.6622)</sup> |
| 损失 | \( \mathcal{L} = \max(d(a,p) - d(a,n) + \text{margin},\ 0) \)<sup>[3](https://omoindrot.github.io/triplet-loss)</sup> |
| 人脸验证（FaceNet） | 在 Labeled Faces in the Wild 上 99.63%，在 YouTube Faces DB 上 95.12%，128 字节嵌入<sup>[2](https://doi.org/10.48550/arxiv.1503.03832)</sup> |
| 三元组类型 | 简单（损失为 0）、半难（\( d(a,p) < d(a,n) < d(a,p) + \text{margin} \)）、难（\( d(a,n) < d(a,p) \)）<sup>[3](https://omoindrot.github.io/triplet-loss)</sup> |
| 批次内最难训练 | 每批 P 个身份 × K 张图像；在批次内选取最难正样本和最难负样本<sup>[4](https://doi.org/10.48550/arxiv.1703.07737)</sup> |
| 对比准确率 | 在一项六网络研究中，三元组网络 0.92，孪生网络 0.63，分类器基线 0.83<sup>[5](https://pmc.ncbi.nlm.nih.gov/articles/PMC10566582/)</sup> |
| 当前状态 | Sentence Transformers 中支持的损失；带难负样本挖掘的三元组损失在 2024 年被描述为跨模态检索的最先进技术<sup>[6](https://link.springer.com/article/10.1007/s10994-024-06710-z)</sup><sup> • </sup><sup>[7](https://sbert.net/docs/package_reference/sentence_transformer/losses.html)</sup> |

## 工作原理

网络用同一个函数 \( \mathrm{Net}(\cdot) \) 嵌入每个输入。给定一个锚点 \( x \)、一个正样本 \( x^{+} \)（同类或同身份）和一个负样本 \( x^{-} \)（不同类），它输出锚点嵌入与另外两个嵌入之间的两个 L2 距离。<sup>[1](https://ar5iv.labs.arxiv.org/html/1412.6622)</sup> 由于三个分支共享参数，反向传播会同时针对全部三个样本更新模型。<sup>[1](https://ar5iv.labs.arxiv.org/html/1412.6622)</sup>

训练信号是相对的，而非绝对的：损失为正在训练的三元组在锚点—正样本距离和锚点—负样本距离之间强制一个边际，因此同类点不需要坍缩到单个点，满足采样得到的三元组也不保证与另一类的每个点都分离。<sup>[1](https://ar5iv.labs.arxiv.org/html/1412.6622)</sup><sup> • </sup><sup>[4](https://doi.org/10.48550/arxiv.1703.07737)</sup> 这正是该方法学习输入之间度量而非类别概率的原因。

## 实现方法

该损失最常见的书面形式是

\[ \mathcal{L} = \max\bigl(d(a,p) - d(a,n) + \text{margin},\ 0\bigr), \]

它将 \( d(a,p) \) 推向 0，并将 \( d(a,n) \) 推到 \( d(a,p) + \text{margin} \) 以上；已经满足此条件的三元组贡献零损失。<sup>[3](https://omoindrot.github.io/triplet-loss)</sup> FaceNet 在一组 N 个三元组上将同一目标写作 \( \sum^{N} \bigl[ \lVert f(x_a^{i}) - f(x_p^{i})\rVert_{2}^{2} - \lVert f(x_a^{i}) - f(x_n^{i})\rVert_{2}^{2} + \alpha \bigr]_{+} \)，其中 \( \alpha \) 是在正样本对和负样本对之间强制的边际。<sup>[2](https://doi.org/10.48550/arxiv.1503.03832)</sup> 距离函数是一个可自行选择的组件：PyTorch 的 `TripletMarginWithDistanceLoss` 接受用户提供的任意非负实值距离函数，而 Keras 示例使用嵌入之间的平方[欧几里得距离](https://www.edgepedia.org/zh/euclidean-distance)。<sup>[8](https://docs.pytorch.org/docs/stable/generated/torch.nn.modules.loss.TripletMarginWithDistanceLoss.md)</sup><sup> • </sup><sup>[9](https://keras.io/examples/vision/siamese_network/)</sup>

从业者的流程如下。

1. **选择骨干网络并共享它。** 一个嵌入网络（例如 CNN）以共享权重实例化三次，如 Keras 实现中使用三个相同子网络那样。<sup>[9](https://keras.io/examples/vision/siamese_network/)</sup>
2. **构造三元组。** 三元组可以从数据集中离线生成，也可以在每个训练批次内在线生成。一个包含 B 个样本的批次最多产生 \( B^{3} \) 个候选三元组。<sup>[3](https://omoindrot.github.io/triplet-loss)</sup>
3. **挖掘信息量大的三元组。** 通过比较 \( d(a,p) \)、\( d(a,n) \) 和边际，将三元组分类为简单、半难或难。<sup>[3](https://omoindrot.github.io/triplet-loss)</sup> 对于 P 个身份、每个身份 K 个样本，一个批次总共产生 \( P \cdot K \cdot (K-1) \cdot (P \cdot K - K) \) 个有效候选三元组；在 batch-all 策略中，损失仅对难和半难三元组取平均，其数量取决于当前距离和边际，而批次内最难策略则为每个锚点选择最难正样本和最难的负样本，产生 \( P \cdot K \) 个三元组。<sup>[3](https://omoindrot.github.io/triplet-loss)</sup>
4. **训练和评估。** FaceNet 的在线挖掘使用约 1,800 个样本的小批量，每个身份约 40 张人脸，为每个锚点—正样本对随机选取一个半难负样本。<sup>[2](https://doi.org/10.48550/arxiv.1503.03832)</sup><sup> • </sup><sup>[3](https://omoindrot.github.io/triplet-loss)</sup> 对于排序任务，均匀三元组采样是次优的，因为排名最高的结果最重要；深度排序工作对 2400 万个三元组样本使用了在线重要性采样。<sup>[10](https://doi.org/10.48550/arxiv.1404.4661)</sup>

## 起源

该架构得名于论文《Deep metric learning using Triplet network》。<sup>[1](https://ar5iv.labs.arxiv.org/html/1412.6622)</sup> 该论文指出，一个类似的模型由 Jiang Wang 及其同事定义；相应的记录是《Learning Fine-grained Image Similarity with Deep Ranking》（arXiv 2014），它训练了一个基于三元组的排序模型，并对三元组进行在线重要性采样。<sup>[1](https://ar5iv.labs.arxiv.org/html/1412.6622)</sup><sup> • </sup><sup>[10](https://doi.org/10.48550/arxiv.1404.4661)</sup>

成对的前身是带对比损失的孪生网络，用于训练人脸验证模型，使同一人对的输出向量靠近、不同人对的输出向量远离；三元组论文将其列为其最明显的竞争者。<sup>[11](http://yann.lecun.com/exdb/publis/pdf/chopra-05.pdf)</sup><sup> • </sup><sup>[1](https://ar5iv.labs.arxiv.org/html/1412.6622)</sup> 与此同时，FaceNet（Florian Schroff、Dmitry Kalenichenko 和 James Philbin，2015 年，arXiv）将 LMNN 度量学习损失改编为用于人脸识别的“三元组损失”，并使其广为人知。<sup>[2](https://doi.org/10.48550/arxiv.1503.03832)</sup><sup> • </sup><sup>[4](https://doi.org/10.48550/arxiv.1703.07737)</sup>

## 变体

命名的变体和相关目标包括：

- **批次内最难（TriHard）训练**，即 P 个身份 × K 张图像的批次组织方式，配合批次内最难正样本和负样本选择，为人重识别提出。<sup>[4](https://doi.org/10.48550/arxiv.1703.07737)</sup>
- **四元组网络**，在三元组输入之外使用一个额外的负样本，并使用四元组损失，对来自不同探针图像的正样本对和负样本对之间的类间距离添加约束。<sup>[12](https://www.mdpi.com/2073-8994/11/9/1066)</sup>
- **三元组中心损失**，将三元组损失和中心损失结合，使特征向其类中心聚类，用于物体检索。<sup>[13](https://proceedings.neurips.cc/paper/2020/file/c96c08f8bb7960e11a1239352a479053-Paper.pdf)</sup>
- **改进的三元组损失**用于人重识别，其中三个参数共享的 CNN 块分别处理锚点、同人图像和不同人图像。<sup>[14](https://www.tnt.uni-hannover.de/papers/data/1332/08265263.pdf)</sup>

## 应用

人脸验证是记录最充分的基准：FaceNet 在 [Labeled Faces in the Wild](https://www.edgepedia.org/zh/labeled-faces-in-the-wild) 上达到 99.63% 的准确率，在 YouTube Faces DB 上达到 95.12%，以 128 字节嵌入将两个数据集上相对于此前最佳已发表结果的错误率降低了 30%。<sup>[2](https://doi.org/10.48550/arxiv.1503.03832)</sup> 三元组和四元组网络已被应用于说话人日志，而[深度度量学习](https://www.edgepedia.org/zh/deep-metric-learning)更广泛地涵盖人脸验证、人重识别、3D 建模、签名验证和音频信号处理。<sup>[12](https://www.mdpi.com/2073-8994/11/9/1066)</sup> 在跨模态图像—文本检索中，带难负样本挖掘的三元组训练结合了图像到文本和文本到图像目标，在 MS-COCO、Flickr30k 和 ROCO 医学图像数据集上评估。<sup>[6](https://link.springer.com/article/10.1007/s10994-024-06710-z)</sup>

与替代方案相比，一项六网络比较报告三元组网络测试准确率为 0.92，领先于 VAE-三元组（0.89）、分类器基线（0.83）、VAE（0.68）、VAE-孪生（0.64）和孪生网络（0.63）。<sup>[5](https://pmc.ncbi.nlm.nih.gov/articles/PMC10566582/)</sup> 在最初的三元组网络论文中，带对比损失的孪生基线在 MNIST 上得分较低，在其他三个数据集上没有产生有意义的结果。<sup>[1](https://ar5iv.labs.arxiv.org/html/1412.6622)</sup> 三元组训练仍在积极使用：一篇 2024 年同行评审论文将基于带难负样本挖掘的三元组损失的跨模态训练描述为跨模态检索的最先进技术，而当前的 Sentence Transformers 文档提供了带有简单、难和半难定义的 `TripletLoss` 实现，因此三元组损失如今是文本嵌入模型支持的微调损失。<sup>[6](https://link.springer.com/article/10.1007/s10994-024-06710-z)</sup><sup> • </sup><sup>[7](https://sbert.net/docs/package_reference/sentence_transformer/losses.html)</sup>

## 局限与替代方案

文献中量化了主要失效模式：

- **三元组爆炸和收敛缓慢。** 可能的三元组数量随数据集大小呈立方增长，且随着训练推进，大多数三元组的约束都能轻易得到满足，因此难样本挖掘至关重要；挖掘是一个单独的步骤，需要嵌入大部分数据并计算所有成对距离，增加了相当大的开销。<sup>[4](https://doi.org/10.48550/arxiv.1703.07737)</sup> 据报道，基于三元组的损失尽管有强监督信号，但收敛缓慢。<sup>[15](https://openreview.net/pdf?id=N8N2VMkWdVf)</sup>
- **坍缩和离群点选择。** 选择最难负样本可能在训练早期导致不良局部极小值，特别是 \( f(x) = 0 \) 的坍缩模型；<sup>[2](https://doi.org/10.48550/arxiv.1503.03832)</sup> 只展示最难三元组也会不成比例地频繁选择离群点。<sup>[4](https://doi.org/10.48550/arxiv.1703.07737)</sup>
- **边际敏感性。** 在一项受控研究中，准确率随边际增大到 0.5 而提高，超过 0.5 后趋于平稳，边际超过 2 后网络无法训练；嵌入大小低于 5 同样无法训练。<sup>[5](https://pmc.ncbi.nlm.nih.gov/articles/PMC10566582/)</sup>
- **模态内不一致。** 在跨模态训练中，三元组损失可能将相似度未知的模态内对排得比同一概念的跨模态对更高，这促使添加模态内约束。<sup>[6](https://link.springer.com/article/10.1007/s10994-024-06710-z)</sup>

最接近的替代方案是成对对比损失（孪生训练，在上述比较中较弱<sup>[1](https://ar5iv.labs.arxiv.org/html/1412.6622)</sup><sup> • </sup><sup>[5](https://pmc.ncbi.nlm.nih.gov/articles/PMC10566582/)</sup>）、基于分类的角度边际损失如 ArcFace——其作者认为半难样本挖掘是有效训练的难题，并通过在 softmax 损失上添加加性角度边际来避免它<sup>[16](https://ibug.doc.ic.ac.uk/media/uploads/documents/arcface.pdf)</sup>——以及原型网络损失，在所综述的说话人任务实验中，其训练更快且得分优于三元组损失。<sup>[12](https://www.mdpi.com/2073-8994/11/9/1066)</sup>

## References

1. [Deep metric learning using Triplet network](https://ar5iv.labs.arxiv.org/html/1412.6622)
2. [Schroff, Florian, Kalenichenko, Dmitry, Philbin, James (2015). FaceNet: A Unified Embedding for Face Recognition and Clustering. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1503.03832)
3. [Triplet Loss and Online Triplet Mining in TensorFlow (Olivier Moindrot blog)](https://omoindrot.github.io/triplet-loss)
4. [Hermans, Alexander, Beyer, Lucas, Leibe, Bastian (2017). In Defense of the Triplet Loss for Person Re-Identification. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1703.07737)
5. [Evaluation of metric and representation learning approaches: Effects of representations driven by relative distance on the performance](https://pmc.ncbi.nlm.nih.gov/articles/PMC10566582/)
6. [Intramodal consistency in triplet-based cross-modal learning for image retrieval (Machine Learning, Springer, 2024)](https://link.springer.com/article/10.1007/s10994-024-06710-z)
7. [Losses, Sentence Transformers documentation](https://sbert.net/docs/package_reference/sentence_transformer/losses.html)
8. [torch.nn.TripletMarginWithDistanceLoss (PyTorch docs)](https://docs.pytorch.org/docs/stable/generated/torch.nn.modules.loss.TripletMarginWithDistanceLoss.md)
9. [Image similarity estimation using a Siamese Network with a triplet loss (Keras example)](https://keras.io/examples/vision/siamese_network/)
10. [Wang, Jiang and colleagues (2014). Learning Fine-grained Image Similarity with Deep Ranking. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1404.4661)
11. [Learning a Similarity Metric Discriminatively, with Application to Face Verification](http://yann.lecun.com/exdb/publis/pdf/chopra-05.pdf)
12. [Deep Metric Learning: A Survey](https://www.mdpi.com/2073-8994/11/9/1066)
13. [The Dilemma of TriHard Loss and an Element-Weighted TriHard Loss for Person Re-Identification](https://proceedings.neurips.cc/paper/2020/file/c96c08f8bb7960e11a1239352a479053-Paper.pdf)
14. [Triplet-Based Deep Similarity Learning for Person Re-Identification](https://www.tnt.uni-hannover.de/papers/data/1332/08265263.pdf)
15. [OpenReview paper on triplet-based loss limitations](https://openreview.net/pdf?id=N8N2VMkWdVf)
16. [ArcFace: Additive Angular Margin Loss for Deep Face Recognition](https://ibug.doc.ic.ac.uk/media/uploads/documents/arcface.pdf)

---
*Initially written Sep 29, 2026 · Edited: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › 机器学习与神经计算 › 神经网络与深度学习 › 标题：I至Z

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/triplet-network

English original: https://www.edgepedia.org/triplet-network
