概念
嵌入在许多人工智能和向量应用中都是核心。这份指南涵盖了这些概念。如果你想马上开始,可以看看我们关于生成嵌入的指南。
🌐 Embeddings are core to many AI and vector applications. This guide covers these concepts. If you prefer to get started right away, see our guide on Generating Embeddings.
嵌入是什么? #
🌐 What are embeddings?
嵌入捕捉文本、图片、视频或其他类型信息的“相关性”。这种相关性最常用于:
🌐 Embeddings capture the "relatedness" of text, images, video, or other types of information. This relatedness is most commonly used for:
- 搜索: 一个搜索词和一段文本有多相似?
- 推荐: 两个产品有多相似?
- **分类:**我们如何对一段文字进行分类?
- **聚类:**我们如何识别趋势?
下面的例子使用了文本嵌入。给定三个短语:
🌐 The following example uses text embeddings. Given three phrases:
- 猫追老鼠
- 小猫抓老鼠
- 我喜欢火腿三明治
你的工作是把意思相近的短语归类。如果你是人类,这应该很明显。短语1和短语2几乎一模一样,而短语3的意思完全不同。
🌐 Your job is to group phrases with similar meaning. If you are a human, this should be obvious. Phrases 1 and 2 are almost identical, while phrase 3 has a completely different meaning.
虽然短语1和短语2很相似,但它们没有共同的词汇(除了“the”)。然而,它们的意思几乎一模一样。我们该怎么教计算机知道它们是一样的呢?
🌐 Although phrases 1 and 2 are similar, they share no common vocabulary (besides "the"). Yet their meanings are nearly identical. How can we teach a computer that these are the same?
人类语言 #
🌐 Human language
人类用词语和符号来交流语言。但单独的词语大多是没有意义的——我们需要借助共同的知识和经验才能理解它们。‘你应该谷歌一下’这个短语只有在你知道谷歌是一个搜索引擎,并且人们已经把它当作动词使用的时候才有意义。
🌐 Humans use words and symbols to communicate language. But words in isolation are mostly meaningless - we need to draw from shared knowledge & experience in order to make sense of them. The phrase “You should Google it” only makes sense if you know that Google is a search engine and that people have been using it as a verb.
同样地,我们需要训练一个神经网络模型来理解人类语言。一个有效的模型应该在数百万不同的例子上进行训练,以理解每个单词、短语、句子或段落在不同上下文中可能的意思。
🌐 In the same way, we need to train a neural network model to understand human language. An effective model should be trained on millions of different examples to understand what each word, phrase, sentence, or paragraph could mean in different contexts.
那这和嵌入有什么关系呢?
🌐 So how does this relate to embeddings?
嵌入是怎么工作的? #
🌐 How do embeddings work?
嵌入将离散信息(单词和符号)压缩成分布式的连续数值数据(向量)。如果我们把之前的短语拿出来画在图表上,可能会像这样:
🌐 Embeddings compress discrete information (words & symbols) into distributed continuous-valued data (vectors). If we took our phrases from before and plot them on a chart, it might look something like this:
下面的图表把示例短语作为点绘制出来。意思相近的短语会靠得很近,没关系的短语就会距离很远。
🌐 The chart below plots example phrases as points. Phrases with similar meanings sit close together, and unrelated phrases sit far apart.

短语1和2会被画在彼此很近的位置,因为它们的意思相似。我们可以预期短语3会在很远的地方,因为它不相关。如果我们有第四个短语,“Sally吃了瑞士奶酪”,它可能会存在于短语3(奶酪可以放在三明治上)和短语1(老鼠喜欢瑞士奶酪)之间的某个地方。
🌐 Phrases 1 and 2 would be plotted close to each other, since their meanings are similar. We would expect phrase 3 to live somewhere far away since it isn't related. If we had a fourth phrase, “Sally ate Swiss cheese”, this might exist somewhere between phrase 3 (cheese can go on sandwiches) and phrase 1 (mice like Swiss cheese).
在这个例子里,我们只有两个维度:X轴和Y轴。实际上,我们需要更多的维度才能有效地捕捉人类语言的复杂性。
🌐 In this example we only have 2 dimensions: the X and Y axis. In reality, we would need many more dimensions to effectively capture the complexities of human language.
使用嵌入 #
🌐 Using embeddings
与上面我们的二维示例相比,大多数嵌入模型会输出更多维度。例如,开源 gte-small 模型会输出 384 维。
🌐 Compared to our 2-dimensional example above, most embedding models will output many more dimensions. For example the open source gte-small model outputs 384 dimensions.
这有什么用呢?一旦我们对多个文本生成了向量表示,通过向量运算(比如余弦距离)来计算它们的相似度就非常简单了。一个常见的用例是搜索。你的流程可能会是这样的:
🌐 Why is this useful? Once we have generated embeddings on multiple texts, it is trivial to calculate how similar they are using vector math operations like cosine distance. A common use case for this is search. Your process might look something like this:
- 先预处理你的知识库,然后为每一页生成嵌入
- 把你的嵌入保存起来,以便以后参考
- 创建一个搜索页面,让用户输入内容
- 获取用户输入,生成一次性嵌入,然后对预处理好的嵌入进行相似度搜索。
- 返回与用户最相似的页面
另请参阅 #
🌐 See also