第一章:什么是语言模型——让机器学会说话 | Chapter 1: What is a Language Model - Teaching Machines to Speak
第一章:什么是语言模型——让机器学会说话 | Chapter 1: What is a Language Model - Teaching Machines to Speak
1.1 当机器开始“听懂”我们说话 | When Machines Begin to "Understand" Us
想象这样一个场景:清晨,你睡眼惺忪地对手机说:“Hey Siri,明天早上七点叫我起床。”Siri立刻回应道:“好的,我已经为你设置了明天早上七点的闹钟。”你甚至还没说完“闹钟”两个字,Siri就已经猜到了你的意图。这看似简单的交互背后,隐藏着一个精妙的技术——语言模型。
Imagine this scenario: It's morning, and with sleepy eyes you say to your phone, "Hey Siri, wake me up at seven tomorrow." Siri immediately responds, "Okay, I've set your alarm for seven tomorrow morning." Before you even finish saying the word "alarm," Siri has already guessed your intention. Behind this seemingly simple interaction lies an elegant technology—the language model.
或者再想想,当你打开ChatGPT,输入“Write a story about a dragon who”,还没等你按下发送键,屏幕上已经出现了几个灰色的预测词:“loves to bake cookies”(喜欢烤饼干)、“befriends a lonely knight”(与孤独的骑士交朋友)、“starts a rock band”(组建摇滚乐队)……这些预测是如何产生的?答案同样指向语言模型。
Or consider this: when you open ChatGPT and type "Write a story about a dragon who," before you even hit send, gray predicted words already appear on the screen: "loves to bake cookies," "befriends a lonely knight," "starts a rock band"... How are these predictions generated? The answer again points to language models.
语言模型,这个听起来有些学术化的术语,实际上已经深深嵌入我们的日常生活。从手机输入法的智能联想,到搜索引擎的自动补全;从智能音箱的语音助手,到翻译软件的准确翻译——语言模型无处不在,悄然改变着人与机器交互的方式。
Language model, this somewhat academic-sounding term, has actually been deeply embedded in our daily lives. From the smart suggestions in your phone's keyboard, to auto-complete in search engines; from voice assistants in smart speakers, to accurate translations in translation software—language models are everywhere, quietly transforming how humans interact with machines.
但语言模型究竟是什么?它为什么能够“理解”我们的语言?它又是如何学会“说话”的?这些问题,正是本书第一章要带领大家探索的核心问题。在接下来的内容中,我们将从日常生活出发,逐步深入技术的本质,揭开语言模型的神秘面纱。
But what exactly is a language model? Why can it "understand" our language? How does it learn to "speak"? These questions are precisely the core issues this first chapter invites us to explore. In the following content, we will start from everyday life and gradually dive into the essence of the technology, unveiling the mystery of language models.
1.2 语言模型的数学本质 | The Mathematical Essence of Language Models
1.2.1 预测的艺术:下一个词是什么?| The Art of Prediction: What is the Next Word?
要理解语言模型,我们首先需要掌握它的核心思想:语言模型的任务是预测下一个词。
To understand language models, we first need to grasp their core idea: The task of a language model is to predict the next word.
这个概念看似简单,却蕴含着深刻的洞察。当我们说“太阳从东方升起”时,下一个词很可能是什么?可能是“来”(太阳从东方升起来)、“照射”(太阳从东方升起照射大地)、或者“了”(太阳从东方升起了)。语言模型需要学会计算每种可能性出现的概率,然后选择最有可能的词作为输出。
This concept seems simple, yet it contains profound insight. When we say "the sun rises from the east," what is the next most likely word? It could be "rises" (the sun rises from the east), "shines" (the sun rises from the east and shines on the earth), or a period. A language model needs to learn to calculate the probability of each possibility, then select the most likely word as output.
让我们用数学语言来精确描述这个过程。语言模型的核心是计算一个词序列的概率分布。用 $w_1, w_2, ..., w_n$ 表示一个包含 $n$ 个词的序列,语言模型的目标是计算这个序列出现的概率:
Let's precisely describe this process using mathematical language. The core of a language model is to compute the probability distribution of a sequence of words. Representing a sequence of $n$ words as $w_1, w_2, ..., w_n$, the goal of a language model is to calculate the probability of this sequence:
$$P(w_1, w_2, ..., w_n)$$
这个概率表示“这句话在语言中出现的可能性有多大”。概率越高,说明这句话越“像”一句人话;概率越低,说明这句话越奇怪。
This probability represents "how likely this sentence is to appear in language." The higher the probability, the more "human-like" the sentence sounds; the lower the probability, the stranger the sentence appears.
1.2.2 条件概率与链式法则 | Conditional Probability and the Chain Rule
但是,直接计算整个序列的概率是非常困难的。想象一下,要计算“我今天吃了一个苹果”这句话的概率,我们需要考虑所有可能的词序列——这几乎是一个无穷大的搜索空间。
However, calculating the probability of an entire sequence directly is very difficult. Imagine calculating the probability of the sentence "I ate an apple today"—we would need to consider all possible word sequences—an almost infinite search space.
幸运的是,数学给了我们一个优雅的工具:链式法则(Chain Rule)。这个法则允许我们将一个复杂的联合概率分解为一系列简单的条件概率的乘积:
Fortunately, mathematics gives us an elegant tool: the Chain Rule. This rule allows us to decompose a complex joint probability into a product of a series of simple conditional probabilities:
$$P(w_1, w_2, ..., w_n) = P(w_1) \cdot P(w_2|w_1) \cdot P(w_3|w_1, w_2) \cdot ... \cdot P(w_n|w_1, w_2, ..., w_{n-1})$$
或者更简洁地写作:
Or more concisely:
$$P(w_1, w_2, ..., w_n) = \prod_{k=1}^{n} P(w_k|w_1, w_2, ..., w_{k-1})$$
这个公式的美妙之处在于:它将一个看似不可能完成的任务(计算整个句子的概率)转化为一系列相对简单的任务(计算每个词在特定上下文下出现的概率)。
The beauty of this formula lies in: it transforms an seemingly impossible task (calculating the probability of an entire sentence) into a series of relatively simple tasks (calculating the probability of each word given specific context).
这就是语言模型的核心任务:给定前面的所有词,预测下一个词的概率分布。用公式表示就是:
This is the core task of language models: Given all the preceding words, predict the probability distribution of the next word. Expressed as a formula:
$$P(w_n|w_1, w_2, ..., w_{n-1})$$
读作“在已知词序列 $w_1, w_2, ..., w_{n-1}$ 的条件下,$w_n$ 出现的概率”。这个公式看起来有些抽象,但它正是语言模型最核心的数学表达。
Read as "the probability of $w_n$ appearing given the known word sequence $w_1, w_2, ..., w_{n-1}$." This formula may seem abstract, but it is precisely the most core mathematical expression of language models.
1.2.3 骰子与语言:形象的比喻 | Dice and Language: An Illustrative Metaphor
为了帮助大家更直观地理解语言模型的工作原理,让我们用一个形象的比喻——抛骰子。
To help everyone more intuitively understand how language models work, let's use an illustrative metaphor—throwing dice.
想象你手里有一副特殊的骰子。这些骰子不是普通的六面立方体,而是带有词汇面的特殊骰子。每个面上的不是点数,而是一个词语。当你抛出这些骰子时,落在桌面上的词语会组成一句话。
Imagine you have a special set of dice in your hand. These dice are not ordinary six-sided cubes, but special dice with vocabulary faces. On each face, instead of dots, is a word. When you throw these dice, the words landing on the table form a sentence.
普通的骰子每个面出现的概率是均等的($1/6$)。但语言这副“骰子”不同——它是有偏好的(biased)。比如,当你已经抛出“我”“爱”“吃”这三个词后,下一个词出现“苹果”的概率可能很高(比如 $0.3$),而出现“汽车”的概率则很低(比如 $0.001$)。
Ordinary dice have equal probability for each face ($1/6$). But this "dice" of language is different—it has preferences (biased). For example, after you've thrown the three words "I," "love," "to," the probability of the next word being "eat" might be very high (like $0.3$), while the probability of it being "car" is very low (like $0.001$).
语言模型的工作,就是学会这些偏好。在训练过程中,模型阅读了海量的文本,学习到:“我”“爱”“吃”后面跟着“苹果”“香蕉”“米饭”等食物名词的概率,远高于跟着“汽车”“飞机”“电脑”等非食物词汇的概率。
The job of a language model is to learn these preferences. During training, the model reads massive amounts of text and learns: after "I," "love," "to," the probability of following words being food nouns like "apples," "bananas," "rice" is much higher than non-food words like "cars," "planes," "computers."
所以,当我们让语言模型“创作”一段文字时,它就像在不断抛掷这副有偏好的骰子:根据已经产生的词,选择下一个最“合理”的词。当然,这里有一个随机性元素——模型通常会在概率最高的几个词中进行抽样,而不是总是选择概率最高的那个。这就像即使骰子倾向于出现某个面,但抛掷结果仍然有一定随机性一样。
So when we let a language model "create" text, it acts like continuously throwing this biased dice: based on the words already generated, it selects the next most "reasonable" word. Of course, there's a randomness element here—the model usually samples from the top few most probable words rather than always selecting the highest probability one. This is like how even though the dice is biased toward a certain face, the throwing result still has some randomness.
```mermaid graph LR A["已生成: '太阳从东方升起'"] --> B["语言模型计算"] B --> C["'了': P=0.35"] B --> D["'来': P=0.25"] B --> E["'照耀': P=0.20"] B --> F["'照射': P=0.15"] B --> G["其他词: P=0.05"] C --> H["采样选择"] D --> H E --> H F --> H G --> H H --> I["生成下一个词"] ```
这个比喻虽然简化了很多细节,但它抓住了语言模型的核心思想:语言不是随机的字符串,而是一个有结构的、可以通过概率来描述的系统。
This metaphor, though it simplifies many details, captures the core idea of language models: **Language i