# 嵌入（机器学习）

在机器学习中，嵌入是一种习得的浮点数向量，它将某个项目——例如单词、图像或用户——表示在一个连续空间中，在这个空间里，几何上的接近反映语义上的相似。嵌入向量由嵌入层或专门的编码器模型产生，其数值由训练决定而非人工指定：相似的项目最终会得到相似的数值组合，因此红杉和巨杉得到的向量比红杉和椰子树更接近。<sup>[1](https://developers.google.com/machine-learning/glossary?hl=en)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 一个浮点数数组，通常来自某个隐藏层，描述该层的输入；两个嵌入的点积衡量它们的相似度<sup>[1](https://developers.google.com/machine-learning/glossary?hl=en)</sup> |
| 目的 | 用稠密、低维的表示取代独热向量等大型稀疏输入，从而捕捉语义关系<sup>[2](https://developers.google.com/machine-learning/crash-course/embeddings?hl=en)</sup> |
| 里程碑式词模型 | 本吉奥等人（2003）在与语言模型联合训练中学习分布式词表示；word2vec（米科洛夫等，2013）在不到一天内用 16 亿词训练完成；GloVe（2014）在词类比任务上达到 75%<sup>[3](https://jmlr2020.csail.mit.edu/papers/v3/bengio03a.html)</sup><sup> • </sup><sup>[4](https://arxiv.org/abs/1301.3781)</sup><sup> • </sup><sup>[5](https://nlp.stanford.edu/pubs/glove.pdf)</sup> |
| 静态与上下文 | 静态模型为每个词分配一个向量；ELMo 和 BERT 等上下文模型产生的表示取决于周围的句子<sup>[6](https://developers.google.com/machine-learning/crash-course/embeddings/obtaining-embeddings)</sup> |
| 主要应用 | 语义搜索、推荐与检索、聚类、分类以及检索增强生成<sup>[7](https://developers.google.cn/machine-learning/recommendation/dnn/retrieval?hl=en)</sup><sup> • </sup><sup>[8](https://openai.com/index/introducing-text-and-code-embeddings/)</sup> |
| 标准基准 | MTEB 涵盖 8 种嵌入任务类型、58 个数据集和 112 种语言；没有任何单一嵌入方法在所有任务上占据主导<sup>[9](https://aclanthology.org/2023.eacl-main.148.pdf)</sup> |
| 已知失效模式 | 高维空间中的枢纽性会扭曲最近邻关系，嵌入还会编码训练语料中存在的社会偏见<sup>[10](https://pmc.ncbi.nlm.nih.gov/articles/PMC7327987/)</sup><sup> • </sup><sup>[11](https://arxiv.org/abs/1607.06520)</sup> |

## 为什么需要嵌入

许多重要的机器学习输入，例如单词，没有天然的向量表示。<sup>[12](https://github.com/tensorflow/docs/blob/master/site/en/r1/guide/embedding.md)</sup> 类别特征的默认编码——独热编码——会产生一个与词汇表等长的向量：一个以 73,000 种树种为特征的模型将需要一个包含 73,000 个元素的输入数组，其中几乎全是零。如此大型的稀疏输入意味着数量巨大的权重，需要更多数据、计算和内存，而且这些向量不携带任何关于项目之间关系的信息；在独热编码下，热狗和沙威玛之间的距离并不比热狗和沙拉更近。<sup>[2](https://developers.google.com/machine-learning/crash-course/embeddings?hl=en)</sup>

嵌入同时解决这两个问题。它们将输入压缩为维度适中的稠密向量，训练则安排空间，使语义相关的项目彼此靠近。<sup>[2](https://developers.google.com/machine-learning/crash-course/embeddings?hl=en)</sup> 这一想法在维度灾难中有着理论动机：[蒙特利尔大学](https://www.edgepedia.org/zh/universite-de-montreal)教授、2018 年[图灵奖](https://www.edgepedia.org/zh/turing-award)得主[约书亚·本吉奥](https://www.edgepedia.org/zh/yoshua-bengio)（Yoshua Bengio）及其同事在 2003 年发表于《机器学习研究杂志》的论文中论证，为单词学习分布式表示，可以让每个训练句子向模型提供关于指数级数量的语义相邻句子的信息，表示和序列概率函数同时学习。<sup>[3](https://jmlr2020.csail.mit.edu/papers/v3/bengio03a.html)</sup>

## 嵌入如何获得

**降维。** 主成分分析（PCA）等经典技术可以通过找到高维数据（如词袋向量）中高度相关的维度并将其合并来产生嵌入。<sup>[6](https://developers.google.com/machine-learning/crash-course/embeddings/obtaining-embeddings)</sup> [潜在语义分析](https://www.edgepedia.org/zh/latent-semantic-analysis)对词-文档矩阵应用奇异值分解，并使用前 300 个维度作为词向量。<sup>[13](https://web.stanford.edu/~jurafsky/slp3/old_aug24/10.pdf)</sup>

**训练嵌入层。** 神经网络可以包含一个大小为 d 的指定隐藏层，即嵌入层，其学习到的权重构成 d 维嵌入空间。训练期间，权重被优化，使相似样本的嵌入向量彼此靠近。这产生针对任务定制的嵌入，代价是训练时间比单独学习嵌入更长。<sup>[6](https://developers.google.com/machine-learning/crash-course/embeddings/obtaining-embeddings)</sup>

**专用词嵌入算法。** word2vec 由谷歌的托马斯·米科洛夫（Tomas Mikolov）、陈凯（Kai Chen）、格雷格·科拉多（Greg Corrado）和杰弗里·迪恩（Jeffrey Dean）于 2013 年提出，包含两种对数线性架构——连续词袋和跳元——用于从超大规模数据集计算连续词向量。在 16 亿词上训练耗时不到一天，这些向量在句法和语义词相似度测试上取得了当时的最佳性能，计算成本远低于早期的神经网络方法。<sup>[4](https://arxiv.org/abs/1301.3781)</sup> GloVe 由斯坦福大学的杰弗里·彭宁顿（Jeffrey Pennington）、理查德·索赫尔（Richard Socher）和克里斯托弗·D·曼宁（Christopher D. Manning）于 2014 年发表，是一种无监督算法，基于语料库中聚合的全局词-词共现统计进行训练，将全局矩阵分解与局部上下文窗口方法相结合；其向量展现出有意义的线性子结构，在词类比任务上得分 75%。<sup>[5](https://nlp.stanford.edu/pubs/glove.pdf)</sup> 像 word2vec 这样的词嵌入依赖分布假说，将语义相似的词映射到几何上接近的向量。<sup>[6](https://developers.google.com/machine-learning/crash-course/embeddings/obtaining-embeddings)</sup>

## 静态嵌入与上下文嵌入

静态嵌入为每个词分配向量空间中的单一位置，而不考虑上下文。这对多义词是一种局限：“post”可以指邮件、柱子或耳环背扣，“orange”可以指颜色或水果，但静态模型将该词放在一个位置，因此它总是更靠近某些义项而远离另一些。<sup>[6](https://developers.google.com/machine-learning/crash-course/embeddings/obtaining-embeddings)</sup>

上下文嵌入通过使用 ELMo 和 BERT 等模型，根据周围文本为每个词的出现计算表示来解决这个问题。<sup>[6](https://developers.google.com/machine-learning/crash-course/embeddings/obtaining-embeddings)</sup> 这一变化是实质性的：对于 ELMo、BERT 和 GPT-2，在所有层上平均而言，静态嵌入仅解释一个词的上下文化表示中不到 5% 的方差，这为这些上下文表示为何如此有效提供了一个原因。<sup>[14](https://aclanthology.org/D19-1006.pdf)</sup> 语言特征，包括结构化的依存关系，被编码在这些上下文表示的低维线性子空间中。<sup>[15](https://aclanthology.org/2021.conll-1.7/)</sup>

## 应用

**检索与推荐。** 推荐系统使用嵌入模型来寻找与用户偏好相似的项目。在服务时，系统计算或查找查询嵌入 q，然后在嵌入空间中搜索接近 q 的项目嵌入，这是一个最近邻问题，由点积等相似度函数评分。对于非常大的语料库，穷举评分代价高昂，因此系统离线预计算排名靠前的候选项，或使用近似最近邻工具，如谷歌开源的 ScaNN（可扩展最近邻）。<sup>[7](https://developers.google.cn/machine-learning/recommendation/dnn/retrieval?hl=en)</sup> 向量搜索服务将同样的原理应用于文档、图像和其他内容的语义搜索，以及检索增强生成，即为语言模型获取相关上下文。<sup>[16](https://docs.aws.amazon.com/en_gb/opensearch-service/latest/developerguide/vector-search.html)</sup>

**通用嵌入 API。** 自 2022 年以来，商业 API 提供用于语义搜索、聚类、主题建模和分类的嵌入。OpenAI 的端点使用源自 GPT-3 的神经网络模型将文本和代码映射到高维空间；两个嵌入之间的相似度用点积计算，给出 -1 到 1 之间的余弦相似度分数，嵌入可以预计算，因此比较速度很快。<sup>[8](https://openai.com/index/introducing-text-and-code-embeddings/)</sup>

## 评估

文本嵌入长期以来在来自单一任务的少量数据集上评估，使得进展难以追踪。大规模文本嵌入基准（MTEB）由尼克拉斯·穆宁霍夫（Niklas Muennighoff）、努阿曼·塔齐（Nouamane Tazi）、洛伊克·马涅（Loïc Magne）和尼尔斯·赖默斯（Nils Reimers）于 2022 年推出，涵盖 8 种嵌入任务类型，包括双文本挖掘、分类、聚类、配对分类、重排序、检索、语义文本相似度和摘要，覆盖 58 个数据集和 112 种语言。对 33 个模型的基准测试表明，没有特定的文本嵌入方法在所有任务上占据主导，表明该领域尚未收敛于一种通用的文本嵌入方法。<sup>[9](https://aclanthology.org/2023.eacl-main.148.pdf)</sup> MTEB 此后已发展为一个针对特定语言和模态的基准生态系统，社区驱动的扩展 MMTEB 覆盖超过 250 种语言的 500 多项评估任务。<sup>[17](https://openreview.net/pdf?id=zl3pfz4VCV)</sup>

## 局限

**枢纽性。** 在高维空间中，少数点——枢纽——作为不成比例地多的其他点的邻居出现，这一现象与距离集中有关。枢纽性已被认定为基于相似度的机器学习中的一个不利因素，损害分类、聚类、回归、图分析、可视化和异常检测，受影响的应用包括多媒体检索、推荐、协同过滤和说话人验证。枢纽过度广泛地传播其信息，而反枢纽携带的信息基本上丢失，因此最近邻关系在语义上变得不正确。互邻近和局部缩放等无监督枢纽性降低方法在广泛领域内改善了枢纽性度量和分类准确率。<sup>[10](https://pmc.ncbi.nlm.nih.gov/articles/PMC7327987/)</sup>

**偏见。** 嵌入继承其训练语料的统计规律，包括社会偏见。波士顿大学的托尔加·博卢克巴西（Tolga Bolukbasi）和微软研究院的凯-魏·张（Kai-Wei Chang）、詹姆斯·邹（James Zou）、文卡特什·萨利格拉马（Venkatesh Saligrama）和亚当·卡莱（Adam Kalai）在 2016 年表明，即使在[谷歌新闻](https://www.edgepedia.org/zh/google-news)文章上训练的词嵌入也表现出女性/男性性别刻板印象，由嵌入空间中的一个方向在几何上捕捉，他们提出了去偏算法，在保留聚类和类比性能等有用属性的同时消除刻板印象。<sup>[11](https://arxiv.org/abs/1607.06520)</sup> 后来的工作使标准诊断复杂化：“男人之于国王如同女人之于 X”这样的类比并不是偏见的准确诊断，尽管词嵌入中的潜在偏见是真实的并且仍然令人担忧。<sup>[18](https://aclanthology.org/2020.cl-2.7/)</sup> 将嵌入与调查数据进行比较的研究发现，基于嵌入的度量对于性别等最显著的维度能很好地追踪人类报告的信念，但对于种族等其他维度则不太可靠。<sup>[19](https://aclanthology.org/2020.acl-main.405/)</sup>

## References

1. [Embedding vector and embedding space, Google Machine Learning Glossary](https://developers.google.com/machine-learning/glossary?hl=en)
2. [Embeddings, Google Machine Learning Crash Course](https://developers.google.com/machine-learning/crash-course/embeddings?hl=en)
3. [Bengio, Ducharme, Vincent, Jauvin (2003). A Neural Probabilistic Language Model. JMLR 3.](https://jmlr2020.csail.mit.edu/papers/v3/bengio03a.html)
4. [Mikolov, Chen, Corrado, Dean (2013). Efficient Estimation of Word Representations in Vector Space. arXiv.](https://arxiv.org/abs/1301.3781)
5. [Pennington, Socher, Manning (2014). GloVe: Global Vectors for Word Representation.](https://nlp.stanford.edu/pubs/glove.pdf)
6. [embeddings:obtaining embeddings | machine learning | google for developers](https://developers.google.com/machine-learning/crash-course/embeddings/obtaining-embeddings)
7. [Retrieval, Google Machine Learning Crash Course (Recommendation Systems)](https://developers.google.cn/machine-learning/recommendation/dnn/retrieval?hl=en)
8. [Introducing text and code embeddings, OpenAI (2022)](https://openai.com/index/introducing-text-and-code-embeddings/)
9. [Muennighoff et al. (2023). MTEB: Massive Text Embedding Benchmark. EACL 2023.](https://aclanthology.org/2023.eacl-main.148.pdf)
10. [Schnabel et al. (2020). A comprehensive empirical comparison of hubness reduction in high-dimensional spaces. PMC.](https://pmc.ncbi.nlm.nih.gov/articles/PMC7327987/)
11. [Bolukbasi et al. (2016). Man is to Computer Programmer as Woman is to Homemaker? Debiasing Word Embeddings. arXiv.](https://arxiv.org/abs/1607.06520)
12. [Embeddings guide, TensorFlow documentation](https://github.com/tensorflow/docs/blob/master/site/en/r1/guide/embedding.md)
13. [Jurafsky & Martin, Speech and Language Processing (3rd ed. draft), Chapter 10 bibliography and history notes](https://web.stanford.edu/~jurafsky/slp3/old_aug24/10.pdf)
14. [Ethayarajh (2019). How Contextual are Contextualized Word Representations? EMNLP 2019.](https://aclanthology.org/D19-1006.pdf)
15. [Hernandez & Andreas (2021). The Low-Dimensional Linear Geometry of Contextualized Word Representations. CoNLL 2021.](https://aclanthology.org/2021.conll-1.7/)
16. [Vector search, Amazon OpenSearch Service documentation](https://docs.aws.amazon.com/en_gb/opensearch-service/latest/developerguide/vector-search.html)
17. [MMTEB: Massive Multilingual Text Embedding Benchmark, ICLR 2025](https://openreview.net/pdf?id=zl3pfz4VCV)
18. [Nissim, van Noord, van der Goot (2020). Fair Is Better than Sensational: Man Is to Doctor as Woman Is to Doctor. Computational Linguistics 46(2).](https://aclanthology.org/2020.cl-2.7/)
19. [Joseph & Morgan (2020). When do Word Embeddings Accurately Reflect Surveys on our Beliefs About People? ACL 2020.](https://aclanthology.org/2020.acl-main.405/)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 11, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

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

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/embedding-machine-learning

English original: https://www.edgepedia.org/embedding-machine-learning
