语言模型的黎明:从统计方法到神经网络
想象一下,你是一位1920年代的密码破译员,面对一堆杂乱无章的字母。你的工具只有纸、笔,以及对“字母e出现频率最高”这类统计规律的朴素认知。你试图通过计数相邻字母的组合(比如“th”比“zx”常见得多)来猜测下一个字母,进而破解信息。这,便是语言建模最古老、最直观的雏形——一种基于频率计数的、对语言表层统计结构的模仿。一个世纪后的今天,我们与能流畅对话、创作诗歌、编写代码的AI系统互动时,是否还能感受到那段从计数到理解、从静态表到动态记忆、最终迈向全局注意力的跌宕起伏的旅程?本章将带你穿越这段百年史,不是为了怀旧,而是为了看清:Transformer的诞生,并非横空出世的奇迹,而是历史必然性在算法、数据与算力交汇点上的一次精准引爆。我们将以中英双语的视角,梳理那些奠定今日大语言模型基石的关键论文、核心思想与失败教训,理解为何“注意力”最终成为破解语言奥秘的那把钥匙。
1.1 统计语言模型的黄金时代:n-gram与马尔可夫的自由
1.1.1 核心思想:世界是一个有限记忆的赌徒
中文:统计语言模型(Statistical Language Model, SLM)的哲学基石,可以浓缩为一句看似简单却影响深远的话:下一个词的出现,只依赖于它前面有限几个词。这便是马尔可夫假设(Markov Assumption)在语言建模中的应用。基于此假设,最著名的家族便是n-gram模型。一个n-gram就是一个连续的n个词的序列。例如,在句子“The cat sat on the”中,bigram(2-gram)有“The cat”、“cat sat”、“sat on”、“on the”;trigram(3-gram)则有“The cat sat”、“cat sat on”、“sat on the”。
English: The philosophical cornerstone of Statistical Language Models (SLMs) can be distilled into one deceptively simple yet profoundly influential statement: The occurrence of the next word depends only on a fixed, finite number of preceding words. This is the application of the Markov Assumption to language modeling. The most famous family stemming from this is the n-gram model. An n-gram is a contiguous sequence of n words. For example, in the sentence "The cat sat on the", the bigrams are "The cat", "cat sat", "sat on", "on the"; the trigrams are "The cat sat", "cat sat on", "sat on the".
中文:模型的核心任务,是估计这个条件概率:$P(w_t | w_{t-1}, w_{t-2}, ..., w_{t-n+1})$。例如,trigram模型计算 $P(\text{“mat”} | \text{“The cat”})$。实践中,我们通过极大似然估计(Maximum Likelihood Estimation, MLE)从大规模语料库中直接计数:$P(w_t | w_{t-1}, w_{t-2}) = \frac{\text{count}(w_{t-2}, w_{t-1}, w_t)}{\text{count}(w_{t-2}, w_{t-1})}$。这就像建立一个巨大的查询表,当你给出前两个词“The cat”时,模型就翻到对应行,看后面都跟了哪些词以及频率,然后选择概率最高的那个。
English: The model's core task is to estimate this conditional probability: $P(w_t | w_{t-1}, w_{t-2}, ..., w_{t-n+1})$. For instance, a trigram model calculates $P(\text{"mat"} | \text{"The cat"})$. In practice, we estimate this via Maximum Likelihood Estimation (MLE) by simply counting from a large corpus: $P(w_t | w_{t-1}, w_{t-2}) = \frac{\text{count}(w_{t-2}, w_{t-1}, w_t)}{\text{count}(w_{t-2}, w_{t-1})}$. It's like building a giant lookup table; when you provide the two preceding words "The cat", the model flips to that row, sees which words follow and their frequencies, and picks the one with the highest probability.
1.1.2 辉煌与致命伤:数据稀疏与泛化鸿沟
中文:n-gram模型在20世纪90年代至21世纪初统治了语音识别、机器翻译等领域的语言建模任务,其成功毋庸置疑。它简单、高效、可解释性强。然而,它的阿喀琉斯之踵——数据稀疏问题(Data Sparsity)——随着n的增大而指数级恶化。想象一个包含百万词汇的词典,trigram的可能组合数量是百万的三次方级别,这远超任何现实语料库的规模。因此,绝大多数有意义的n-gram在训练语料中根本不会出现,导致其MLE概率为零。一个零概率的上下文会“杀死”整个后续序列的概率,这在需要评估未见过句子时是灾难性的。
English: n-gram models reigned supreme in language modeling for speech recognition, machine translation, etc., from the 1990s to the early 2000s, and their success was undeniable. They were simple, efficient, and interpretable. However, their Achilles' heel—the Data Sparsity Problem—worsened exponentially as n increased. Imagine a vocabulary of a million words; the number of possible trigram combinations is on the order of a million cubed, far exceeding the scale of any realistic corpus. Consequently, the vast majority of meaningful n-grams simply never appear in the training data, leading their MLE probability to be zero. A zero probability for a context "kills" the probability of any subsequent sequence, which is catastrophic when evaluating unseen sentences.
```mermaid graph LR A[“n-gram 模型”] --> B[“优势: 简单, 高效, 可解释”] A --> C[“致命伤: 数据稀疏”] C --> D[“导致: 未登录词/OOV 问题”] C --> E[“导致: 无法泛化到新组合”] B --> F[“应用: 早期语音识别/拼写检查”] ```
中文:为了应对零概率问题,学者们发明了各种平滑技术(Smoothing Techniques),如拉普拉斯平滑(Add-one)、古德-图灵估计(Good-Turing)、Kneser-Ney平滑。这些方法的核心思想是:从未见过的n-gram中“偷”一点点概率 mass,分配给那些在上下文中看似合理但未出现的词。Kneser-Ney平滑尤为精妙,它不只考虑n-gram本身频率,还考虑低阶n-gram的多样性(即一个词作为前面多种不同词的下文的次数),能更好地为未知词分配概率。然而,平滑技术终究是“打补丁”,它们无法从根本上解决泛化能力的缺失。模型无法理解“猫坐在垫子上”和“猫坐在毯子上”在语义上的相似性,因为在n-gram的离散表中,“cat sat on the mat”和“cat sat on the blanket”是两个完全孤立的条目,尽管“mat”和“blanket”都是“on the”后面可能接的“家具类”或“覆盖物类”名词。
English: To combat zero probabilities, scholars invented various Smoothing Techniques, such as Laplace smoothing (Add-one), Good-Turing estimation, and Kneser-Ney smoothing. The core idea is to "steal" a tiny amount of probability mass from seen n-grams and redistribute it to plausible but unseen words in a given context. Kneser-Ney smoothing is particularly clever; it doesn't just consider the frequency of the n-gram itself but also the diversity of lower-order n-grams (i.e., how many different preceding words a given word follows), allowing it to better assign probability to unknown words. However, smoothing is ultimately a patch. It cannot fundamentally solve the lack of generalization. The model cannot grasp the semantic similarity between "The cat sat on the mat" and "The cat sat on the blanket". In the discrete table of n-grams, these are two completely isolated entries, even though both "mat" and "blanket" are plausible "furniture-like" or "cover-like" nouns following "on the".
中文:n-gram模型的局限性,本质上是离散、高维、稀疏的符号表示与语言连续、渐变、组合的语义本质之间的深刻矛盾。语言不是一堆孤立符号的随机拼接,而是一个具有强大组合性(Compositionality)和概括性(Generalization)的系统。n-gram像一台只能记忆固定短语的“复读机”,一旦遇到稍微变化的表达,便束手无策。它无法建立“国王 - 男人 + 女人 ≈ 女王”这样的向量空间关系,因为它根本没有“向量”,只有计数。这种局限性,在任务复杂度增加、需要长距离依赖或深层语义理解时,暴露得淋漓尽致。
English: The limitation of n-gram models is, at its core, a profound contradiction between a discrete, high-dimensional, sparse symbolic representation and the continuous, gradient, compositional nature of language semantics. Language is not a random concatenation of isolated symbols but a system with powerful compositionality and generalization. An n-gram is like a "parrot" that only memorizes fixed phrases; it is helpless when encountering slightly varied expressions. It cannot establish vector-space relationships like "king - man + woman ≈ queen" because it has no "vectors," only counts. This limitation becomes glaringly obvious as tasks grow in complexity, requiring long-range dependencies or deep semantic understanding.
1.2 神经网络的破晓:分布式假设与词向量的诞生
1.2.1 思想萌芽:一个词的意义在于它的邻居
中文:解决n-gram泛化困境的钥匙,藏在语言学的一个古老思想中:“一个词的意义,由它经常出现的上下文决定”。这便是分布假说(Distributional Hypothesis),由语言学家Zellig Harris在1954年提出。其现代版本是:语义相似的词,倾向于出现在相似的语境中。这个思想在计算领域的完美实现,就是分布式表示(Distributed Representation),也叫词嵌入(Word Embedding)。它的革命性在于:将每个词从离散的、巨大的one-hot向量(维度等于词表大小,只有一个1,其余为0),映射到一个稠密的、低维的连续向量空间(例如300维)中。在这个空间里,语义相近的词,其向量距离也相近。
English: The key to overcoming n-gram's generalization困境 lay hidden in an old linguistic idea: "You shall know a word by the company it keeps." This is the Distributional Hypothesis, proposed by linguist Zellig Harris in 1954. Its modern formulation: Words that are semantically similar tend to appear in similar contexts. The perfect computational realization of this idea is the Distributed Representation, also known as Word Embedding. Its revolutionary aspect is: representing each word not as a discrete, gigantic one-hot vector (dimension equals vocabulary size, with a single 1 and the rest 0s), but as a dense, low-dimensional, continuous vector in a space (e.g., 300 dimensions). In this space, semantically similar words have vectors that are close to each other.
1.2.2 里程碑:神经网络语言模型(NNLM)
中文:2003年,Yoshua Bengio等人在论文《A Neural Probabilistic Language Model》中,首次将神经网络与语言建模深度融合,提出了神经网络语言模型(Neural Network Language Model, NNLM)。NNLM的架构是一个经典的三层结构: 1. 输入层:将当前上下文(例如前n-1个词)的one-hot表示,通过一个共享的查找表(Lookup Table)$C$,映射为对应的词向量 $C(w)$,并拼接成一个长向量。 2. 隐藏层:将这个拼接向量输入一个或多个全连接层,并经过非线性激活函数(如tanh),得到一个固定长度的上下文表示向量 $h$。这一步是特征组合与抽象的核心,它自动学习如何将离散的词向量组合成有意义的上下文表征。 3. 输出层:$h$ 通过一个 softmax 层,计算整个词表上每个词作为下一个词 $w_t$ 的条件概率:$P(w_t | context) = \frac{\exp(h^T v'_{w_t} + b_{w_t})}{\sum_{w'