# 深度哈希

深度哈希是一种机器学习方法，它训练一个深度神经网络，将每个输入（通常是图像）映射为紧凑的二值码，使得相似的物体获得汉明距离（两个二值码不同的比特位置数）较小的码，而不相似的物体获得汉明距离较大的码。这些码可实现快速的大规模最近邻搜索：比较 K 位码是一种按位运算，据报道，在标准数据集上，使用二进制哈希码的距离计算比使用向量量化表示的距离计算快 10 到 20 倍。<sup>[1](https://arxiv.org/html/2510.04127v2)</sup> 由于每个比特只占一位存储而非 32 位浮点数，在固定的字节预算下，对每个坐标做粗量化比以全精度存储少数坐标能保留更多的排序信息。<sup>[1](https://arxiv.org/html/2510.04127v2)</sup>

| 关键事实 | 数值 |
|---|---|
| 输出 | 每个输入一个 K 位二值码；汉明距离与内积呈线性关系，\( \mathrm{dist}_{H}(h_{i}, h_{j}) = \tfrac{1}{2}(K - \langle h_{i}, h_{j} \rangle) \)<sup>[2](https://ojs.aaai.org/index.php/AAAI/article/view/10235)</sup> |
| 方法分类 | 成对、基于排序、逐点和基于量化的有监督形式<sup>[3](https://arxiv.org/pdf/2003.03369)</sup> |
| 典型基准 | DPSH 在 CIFAR-10 上 12/24/32/48 位时达到 MAP 0.713/0.727/0.744/0.757，在 NUS-WIDE 上（第一种实验设置）达到 0.794/0.822/0.838/0.851<sup>[4](https://dl.acm.org/doi/10.5555/3060832.3060860)</sup> |
| 最佳经典结果 | DSDH 在第二种实验设置下于 CIFAR-10 上平均 MAP 为 0.938<sup>[5](https://papers.neurips.cc/paper_files/paper/2017/file/e94f63f579e05cb49c05c2d050ead9c0-Paper.pdf)</sup> |
| 松弛策略 | 惩罚松弛、离散交替优化，以及采用 \( y = \tanh(\beta x) \) 的延拓法<sup>[3](https://arxiv.org/pdf/2003.03369)</sup> |
| 规模警示 | 在 Glint360K（1700 万张图像）上，经典乘积量化在 32 位时 Top-1 比 GreedyHash 高出近 57%<sup>[6](https://proceedings.neurips.cc/paper_files/paper/2023/file/c2469e35d469e3c0eca09dbe484eb474-Paper-Conference.pdf)</sup> |

## 工作原理

深度哈希模型是一个编码器网络，其最后一层产生 K 个实值输出，通过符号函数二值化为 \(\{+1, -1\}^{K}\) 中的码。训练会塑造连续输出，使二值化损失的信息很少。深度哈希网络（Deep Hashing Network，DHN）是其一个经典的四组件分解：用于图像表示的卷积-池化子网络、全连接哈希层、用于保持相似性的成对交叉熵损失，以及用于提升哈希质量的成对量化损失。<sup>[2](https://ojs.aaai.org/index.php/AAAI/article/view/10235)</sup>

各损失项承担不同的职责。相似性项使相似对的[汉明距离](https://www.edgepedia.org/zh/hamming-distance)变小、不相似对的汉明距离变大；例如 DSDH 最小化成对负对数似然 \( J = -\sum_{(i,j)\in S}[s_{ij}\Phi_{ij}-\log(1+e^{\Phi_{ij}})] \)，并在“好的码也应当是理想分类码”的假设下对码添加分类损失。<sup>[5](https://papers.neurips.cc/paper_files/paper/2017/file/e94f63f579e05cb49c05c2d050ead9c0-Paper.pdf)</sup> 量化项惩罚连续输出与二值码之间的距离，常见形式为 \( \lvert\lvert \lvert h_{i} \rvert - 1 \rvert\rvert_{1} \) 或配合 tanh 激活的 \( -\lvert\lvert h_{i} \rvert\rvert \)，使得 \( \mathrm{sgn}(h_{i}) \approx h_{i} \)。<sup>[3](https://arxiv.org/pdf/2003.03369)</sup> 不同形式的区别在于训练信号：成对方法使用带标签的相似/不相似对，基于排序的方法使用三元组或排序，而逐点方法（如 GreedyHash）仅对码使用 softmax 交叉熵损失，不使用对比损失或三元组检索损失。<sup>[7](https://proceedings.neurips.cc/paper/2018/file/13f3cf8c531952d72e5847c4183e6910-Paper.pdf)</sup> 成对和三元组方案需要昂贵地构建图像组，这在大规模数据集上变得不可行，而在这些场景中逐点方案更为简单。<sup>[7](https://proceedings.neurips.cc/paper/2018/file/13f3cf8c531952d72e5847c4183e6910-Paper.pdf)</sup>

## 实现方法

实践者选择骨干网络（在经典工作中是 AlexNet 或 CNN-F 等 CNN；DSDH 使用 CNN-F，含五个卷积层和两个全连接层，最后一层大小设为码的数量，超参数 \( \mu, \nu, \eta \) 通过交叉验证设为 1、0.1 和 55）<sup>[5](https://papers.neurips.cc/paper_files/paper/2017/file/e94f63f579e05cb49c05c2d050ead9c0-Paper.pdf)</sup>，接上一个 K 单元哈希层，选择损失函数和松弛策略，然后训练。训练吞吐量是实用的：DPSH 在单块 NVIDIA K80 GPU 上以约每秒 290 张图像的速度训练。<sup>[4](https://dl.acm.org/doi/10.5555/3060832.3060860)</sup> 部署时，新查询通过前向传播和二值化进行编码，\( b_{q} = h(x_{q}) = \mathrm{sgn}(W^{T}\varphi(x_{q};\theta) + v) \)，<sup>[4](https://dl.acm.org/doi/10.5555/3060832.3060860)</sup> 检索按汉明距离对数据库码排序；近期的流程使用非二值化查询概率向量与二值数据库码之间的非对称距离。<sup>[8](https://openaccess.thecvf.com/content/CVPR2026W/ECV/papers/Moummad_Image_Hashing_via_Cross-View_Code_Alignment_in_the_Age_of_CVPRW_2026_paper.pdf)</sup>

## 起源

学习哈希始于通过随机投影实现的数据无关 LSH，它需要长码和大量内存。<sup>[9](https://openaccess.thecvf.com/content_cvpr_2016/papers/Liu_Deep_Supervised_Hashing_CVPR_2016_paper.pdf)</sup> LSH 由阿里斯蒂德斯·吉奥尼斯（Aristides Gionis）、彼得·因迪克（Piotr Indyk）和[拉吉夫·莫特瓦尼](https://www.edgepedia.org/zh/rajeev-motwani)（Rajeev Motwani）于 1999 年提出。<sup>[10](https://dl.acm.org/doi/10.1109/TPAMI.2018.2789887)</sup> 此外还有谱哈希（Spectral Hashing）。<sup>[10](https://dl.acm.org/doi/10.1109/TPAMI.2018.2789887)</sup> 鲁斯兰·萨拉赫丁诺夫（Ruslan Salakhutdinov）和[杰弗里·辛顿](https://www.edgepedia.org/zh/geoffrey-hinton)（Geoffrey Hinton）于 2009 年提出的语义哈希（Semantic hashing）建立在堆叠受限玻尔兹曼机之上，文献中被视为首次将深度学习技术用于哈希。<sup>[3](https://arxiv.org/pdf/2003.03369)</sup> 综述文献将 CNNH（一种带坐标下降的两步策略）描述为深度有监督哈希框架。<sup>[3](https://arxiv.org/pdf/2003.03369)</sup> 自 2015 年起，该领域转向联合特征学习与哈希编码：赖汉江（Hanjiang Lai）、潘岩（Yan Pan）、刘烨（Ye Liu）和[颜水成](https://www.edgepedia.org/zh/shuicheng-yan)（Shuicheng Yan）提出的 DNNH（2015），<sup>[11](https://doi.org/10.48550/arxiv.1504.03410)</sup> 沈富民（Fumin Shen）、沈春华（Chunhua Shen）、刘威（Wei Liu）和沈恒涛（Heng Tao Shen）提出的有监督离散哈希（Supervised Discrete Hashing，2015），<sup>[12](https://doi.org/10.48550/arxiv.1503.01557)</sup> 以及 2016 年一系列端到端 CNN 方法，包括 DSH（CVPR 2016）、<sup>[9](https://openaccess.thecvf.com/content_cvpr_2016/papers/Liu_Deep_Supervised_Hashing_CVPR_2016_paper.pdf)</sup> DPSH（IJCAI 2016）、<sup>[4](https://dl.acm.org/doi/10.5555/3060832.3060860)</sup> 和 DHN（AAAI 2016）。<sup>[2](https://ojs.aaai.org/index.php/AAAI/article/view/10235)</sup> 李琦（Qi Li）、孙哲南（Zhenan Sun）、何冉（Ran He）和谭铁牛（Tieniu Tan）提出的 DSDH 出现在 NeurIPS 2017。<sup>[5](https://papers.neurips.cc/paper_files/paper/2017/file/e94f63f579e05cb49c05c2d050ead9c0-Paper.pdf)</sup>

## 变体

**有监督成对。** DPSH 针对成对标签应用同时进行特征学习和哈希码学习；<sup>[4](https://dl.acm.org/doi/10.5555/3060832.3060860)</sup> DSDH 在成对目标中加入分类信息。<sup>[5](https://papers.neurips.cc/paper_files/paper/2017/file/e94f63f579e05cb49c05c2d050ead9c0-Paper.pdf)</sup>

**侧重优化。** 曹章杰（Zhangjie Cao）、龙明胜（Mingsheng Long）、王建民（Jianmin Wang）和于士元（Philip S. Yu）于 2017 年提出的 HashNet 使用延拓法，以 \( y = \tanh(\beta x) \) 训练并逐渐增大 \( \beta \) 向符号函数靠拢，并针对正对远多于负对的不平衡数据使用加权最大似然损失。<sup>[13](https://doi.org/10.48550/arxiv.1702.00758)</sup> 苏守鹏（Shupeng Su）、张超（Chao Zhang）、韩凯（Kai Han）和田永鸿（Yonghong Tian）于 2018 年提出的 GreedyHash（NeurIPS 2018）在前向传播中严格应用符号函数，同时在反向传播中完整传递梯度，避免了梯度消失。<sup>[7](https://proceedings.neurips.cc/paper/2018/file/13f3cf8c531952d72e5847c4183e6910-Paper.pdf)</sup> 姜庆元（Qing-Yuan Jiang）和李武军（Wu-Jun Li）于 2017 年提出的非对称深度有监督哈希（Asymmetric Deep Supervised Hashing）对查询点和数据库点区别对待，后续工作如 JLDSH 继承这种非对称训练。<sup>[14](https://www.sciencedirect.com/science/article/abs/pii/S0925231219318119)</sup>

**无监督。** SADH 交替执行三个模块：深度哈希模型训练、相似图更新，以及直接处理二值约束的二值码优化。<sup>[10](https://dl.acm.org/doi/10.1109/TPAMI.2018.2789887)</sup> 无监督方法分为基于重建的方法和基于对比学习的方法。<sup>[15](https://www.sciencedirect.com/science/article/abs/pii/S092523122503262X)</sup>

## 应用

深度哈希用于快速的大规模图像检索。在标准的 CIFAR-10 和 NUS-WIDE 协议上，基于 CNN 的方法大幅优于传统哈希学习方法。<sup>[9](https://openaccess.thecvf.com/content_cvpr_2016/papers/Liu_Deep_Supervised_Hashing_CVPR_2016_paper.pdf)</sup> DSDH 在第二种设置下于 CIFAR-10 上达到平均 MAP 0.938，高于 DPSH（0.787）、DTSH（0.922）和 VDSH（0.846）。<sup>[5](https://papers.neurips.cc/paper_files/paper/2017/file/e94f63f579e05cb49c05c2d050ead9c0-Paper.pdf)</sup>

## 局限与替代方案

**量化误差。** 去掉量化损失（DHN-Q）后，在对连续嵌入做二值化时，MAP 分别下降 4.18%、0.01% 和 2.29%，而早期深度哈希方法的一个关键缺点是量化误差未在统计意义上最小化。<sup>[2](https://ojs.aaai.org/index.php/AAAI/article/view/10235)</sup> 饱和 sigmoid 或 tanh 松弛收敛慢且带来较大的量化误差，而符号函数不可微，这促使了离散交替优化，如 DSDH 的离散循环坐标下降。<sup>[5](https://papers.neurips.cc/paper_files/paper/2017/file/e94f63f579e05cb49c05c2d050ead9c0-Paper.pdf)</sup>

**评估与基线。** 2018 年的一项重新评估发现，深度哈希评估所用数据集对真实基于内容的图像检索而言过小且过于简单，忽略了搜索时间，也忽视了 LSH 的多哈希表技巧；在修正设置下，最先进的深度哈希方法劣于多表 IsoH 这一简单的无监督方法。<sup>[16](https://ar5iv.labs.arxiv.org/html/1711.06016)</sup> 这与标准协议中“基于 CNN 的方法大幅优于传统哈希”<sup>[9](https://openaccess.thecvf.com/content_cvpr_2016/papers/Liu_Deep_Supervised_Hashing_CVPR_2016_paper.pdf)</sup> 的说法相矛盾，分歧至今未解决：两项结果使用了不同的协议。

**替代方案。** 在大规模数据上，经典乘积量化优于许多深度哈希方法；FPPQ 在 Glint360K 上 32 位时 Top-1 比 GreedyHash 高出近 57%，在 64 位和 128 位时分别高出约 28% 和近 4%。<sup>[6](https://proceedings.neurips.cc/paper_files/paper/2023/file/c2469e35d469e3c0eca09dbe484eb474-Paper-Conference.pdf)</sup> 基于图的索引（如 [HNSW](https://www.edgepedia.org/zh/hierarchical-navigable-small-world)、NSG 和 DiskANN）基本不限定投影或量化方式，并且在召回率-延迟权衡上常常优于哈希。<sup>[1](https://arxiv.org/html/2510.04127v2)</sup>

**2023 年以来。** 该领域已转向冻结的基础模型嵌入。Hashing-Baseline 是一种无需训练的方法，结合 PCA、随机正交投影和预训练编码器嵌入的阈值二值化，即使在 16 位时也能在 CIFAR-10、Flickr25K、COCO 和 NUS-WIDE 上达到很高的 mAP，无需额外学习。<sup>[17](https://arxiv.org/pdf/2509.14427v1.pdf)</sup> 陈永彪（Yongbiao Chen）及其同事于 2021 年提出的 TransHash（arXiv）被作者描述为首个没有 CNN 骨干的深度哈希方法，使用带贝叶斯学习和柯西量化损失的孪生视觉 Transformer。<sup>[18](https://doi.org/10.48550/arxiv.2105.01823)</sup>

## References

1. [Projection and Quantisation: A Unifying View of Learning to Hash, from Random Projections to the RAG Era](https://arxiv.org/html/2510.04127v2)
2. [Deep Hashing Network for Efficient Similarity Retrieval (DHN, AAAI 2016)](https://ojs.aaai.org/index.php/AAAI/article/view/10235)
3. [A Survey on Deep Hashing Methods (Wang et al.)](https://arxiv.org/pdf/2003.03369)
4. [Feature learning based deep supervised hashing with pairwise labels (DPSH, IJCAI 2016)](https://dl.acm.org/doi/10.5555/3060832.3060860)
5. [Deep Supervised Discrete Hashing (DSDH, NIPS 2017)](https://papers.neurips.cc/paper_files/paper/2017/file/e94f63f579e05cb49c05c2d050ead9c0-Paper.pdf)
6. [Unleashing the Full Potential of Product Quantization for Large-Scale Image Retrieval (FPPQ, NeurIPS 2023)](https://proceedings.neurips.cc/paper_files/paper/2023/file/c2469e35d469e3c0eca09dbe484eb474-Paper-Conference.pdf)
7. [Greedy Hash: Towards Fast Optimization for Accurate Hash Coding in CNN (NeurIPS 2018)](https://proceedings.neurips.cc/paper/2018/file/13f3cf8c531952d72e5847c4183e6910-Paper.pdf)
8. [Image Hashing via Cross-View Code Alignment in the Age of Foundation Models (CroVCA, CVPRW 2026)](https://openaccess.thecvf.com/content/CVPR2026W/ECV/papers/Moummad_Image_Hashing_via_Cross-View_Code_Alignment_in_the_Age_of_CVPRW_2026_paper.pdf)
9. [Deep Supervised Hashing for Fast Image Retrieval (DSH, CVPR 2016)](https://openaccess.thecvf.com/content_cvpr_2016/papers/Liu_Deep_Supervised_Hashing_CVPR_2016_paper.pdf)
10. [Unsupervised Deep Hashing with Similarity-Adaptive and Discrete Optimization (SADH, TPAMI)](https://dl.acm.org/doi/10.1109/TPAMI.2018.2789887)
11. [Lai, Hanjiang and colleagues (2015). Simultaneous Feature Learning and Hash Coding with Deep Neural Networks. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1504.03410)
12. [Shen, Fumin and colleagues (2015). Supervised Discrete Hashing. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1503.01557)
13. [Cao, Zhangjie and colleagues (2017). HashNet: Deep Learning to Hash by Continuation. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1702.00758)
14. [Joint learning based deep supervised hashing for large-scale image retrieval (JLDSH, Neurocomputing)](https://www.sciencedirect.com/science/article/abs/pii/S0925231219318119)
15. [Unsupervised deep hashing based on multi-scale aggregation and optimal transport matching for image retrieval (Neurocomputing, 2025)](https://www.sciencedirect.com/science/article/abs/pii/S092523122503262X)
16. [A Revisit on Deep Hashings for Large-scale Content Based Image Retrieval (AAAI 2018)](https://ar5iv.labs.arxiv.org/html/1711.06016)
17. [Hashing-Baseline: Rethinking Hashing in the Age of Pretrained Models](https://arxiv.org/pdf/2509.14427v1.pdf)
18. [Chen, Yongbiao and colleagues (2021). TransHash: Transformer-based Hamming Hashing for Efficient Image Retrieval. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2105.01823)

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

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Machine learning and neural computation › Machine learning methods

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/deep-hashing

English original: https://www.edgepedia.org/deep-hashing
