跳到主要内容

词向量与 BERT — 预训练改变 NLP

这一章讲三件事: 为什么词不能再用 one-hot 表示(任意两词的相似度都是 0); 词向量怎么从「猜邻居」这个自监督任务里长出来(word2vec/GloVe/子词); BERT 怎么把「每个词一个固定向量」升级为「词的意思由上下文决定」。 它是第 10-13 章序列模型在 NLP 的落地,也是第 13 章「预训练三种模式」里 encoder-only 那一支的完整展开。

1. one-hot 的死穴:任意两个词都「不相似」

第 10 章的语言模型用 one-hot 表示词:词表有 5 万个词, 每个词就是一根 5 万维、只有一位是 1 的向量。它有个结构性毛病: 任意两个不同词的 one-hot 向量互相垂直,点积为 01

量「两个词有多像」常用的尺子是余弦相似度(cosine similarity): 两个向量夹角的余弦,方向一致为 1,垂直为 0,相反为 −1。 one-hot 下,任何两个词的余弦相似度都是 0—— 「猫」和「猫科动物」的相似度,跟「猫」和「微积分」一模一样。 语言里「意思近的词该互相靠近」这件事,one-hot 一个字都表达不了。

解法方向:把每个词表示成一根几百维的稠密(每一位都是实数、不像 one-hot 那样几乎全零的)向量——词向量(word embedding), 让相似词的向量彼此靠近。问题是:拿什么信号来训这些向量? 没人给「相似度」打标签。下一节的答案是:从文本本身挖。

2. word2vec:用「猜邻居」学出向量

word2vec 的关键洞察:词的意思可以从它的邻居推出来—— 「近朱者赤」的语言学版。它把这句话变成一个自监督任务,免费标签遍地都是。 工具里装着两个互逆的模型2:

  • skip-gram(跳字模型):给定中心词,猜它周围窗口里的词;
  • CBOW(continuous bag of words,连续词袋):反过来,给定周围一圈词,猜中心词。

主走查:skip-gram 处理「the man loves his son」。 取中心词「loves」、窗口大小 2,它要猜的上下文词是 「the」「man」「his」「son」四个3。 每个词备两根向量:当中心词时用 v,当上下文词时用 u。 「loves」猜出「his」的概率用 softmax 算:

P(his | loves) = exp(u_his · v_loves) / Σ_w exp(u_w · v_loves)

分子:u_his 与 v_loves 的点积,比如 = 2.1 → exp(2.1) ≈ 8.2
分母:词表里每个词都算一遍点积再求和,比如 ≈ 16.4
P(his | loves) ≈ 8.2 / 16.4 = 0.5
(这几个点积数值是为演示编的,不是真实数值)

训练就是调所有词的 u、v,让真实邻居的分子尽量大。 「猜邻居」只是借口:我们根本不关心这个分类器本身, 训完留下的是副产品 v——此时意思相近的词 (出现在相似的邻居环境里)向量自然靠近。 附带的好处是向量还带方向结构: vec(son) + vec(woman) − vec(man) 算出来最像 vec(daughter), 类比题变成了向量算术4

3. 全词表 softmax 太贵:负采样(拿少数几个「假邻居」当反例的近似训练法)与层次 softmax

上面那个分母是成本黑洞:词表 5 万词,每处理一对(中心词,上下文词), softmax 就要算 5 万个点积。两招把它压下去5

负采样(negative sampling):把「5 万分类」换成「几个二分类」。 正例 (loves, his) 标 D=1;再从噪声分布里抽 K 个词(比如 K=5) 当反例标 D=0——模型只学「这对词是不是真邻居」。 一次更新只碰 K+1=6 个点积,而不是 5 万个6

这个「真邻居拉近、假邻居推远」的套路,今天有一个更响的名字: 对比学习(把「真的拉近、假的推远」当训练信号的一类方法)——负采样就是它的雏形。

层次 softmax(hierarchical softmax)走另一条路: 把词表组织成一棵二叉树,每个词是从根到叶的一条路径, 算概率变成沿路径做 log|V| 次二分类7

工程上还有两个小动作:高频词(the、of)子采样丢弃一部分 (它们出现太勤,信息量低);反例按词频的 3/4 次幂抽样 (压低超高频词、抬高稀有词被选中的机会)。

4. GloVe 与子词:两条路线汇合,未登录词有救

word2vec 是「预测派」:从预测任务里挤出向量。 还有一支更老的「计数派」:直接统计词与词的共现次数 (「冰」常与「冷」同现),再从计数矩阵里分解出向量。 GloVe 证明两派不必二选一:它对共现计数的对数拟合一个加权平方损失, 在数学上把两条路线接了起来8

两派共同的一个软肋是未登录词:训练时没见过的词(新词、拼写变体) 没有向量可查。fastText 的解法是把词拆成更小的单位—— 子词(subword,字符级 n-gram,比如「where」拆成「<wh」「whe」「her」…): 中心词的向量 = 自身向量 + 所有子词向量之和。 没见过的词也能由零件拼出向量9。这个思想后来长成 BERT 时代的 分词(把句子切成最小单位,切出来的每个单位叫词元)工具——第 13 章的 token 化就是它的后代。

5. BERT:词的意思由上下文定

词向量解决了「相似词靠近」,但留着一个死结:每个词只有一根向量。 「crane」在「a crane is flying」(鹤)和「a crane driver came」(吊车)里 意思完全不同,词向量却只能给同一个10。 这类向量叫上下文无关(context-independent)表示。

补上这个洞的三步演进11:

  • ELMo(2018):用双向 LSTM 给每个词算「上下文里的表示」—— 但权重冻结,表示只能当额外特征拼进各任务自己的架构,任务特定;
  • GPT(2018):Transformer 架构对一切任务通用——任务无关, 但只能从左往右看:「i went to the bank to deposit cash」里, 「bank」的表示看不到右边的「deposit」,两个「bank」只得同向量;
  • BERT(2018):双向 + 任务无关,两头的好处都要。

双向的代价是:从左到右的语言模型任务(预测下一个词)没法做了—— 模型会偷看答案。BERT 换了一个任务,这是本章第二处走查。

走查:MLM 处理「this movie is great」。 MLM(masked language modeling,掩码语言模型):随机选 15% 的词, 让模型根据左右上下文把它们猜回来12。 被选中的词(比如「great」)按 80/10/10 三种方式处理13:

  • 80% 换成特殊记号 :「this movie is 」;
  • 10% 换成随机词:「this movie is drink」;
  • 10% 原样保留:「this movie is great」。

为什么不全用 ?因为微调时的真实文本里没有 , 训练与使用会脱节;掺 10% 噪声、10% 原样,模型就不能假设 「看到的词都是被遮住的」,对任何输入都得认真编码。 第二个任务 NSP(next sentence prediction,下句预测): 输入一对句子,一半是真的前后相邻(标 True),一半的第二句随机抽(标 False), 让模型学句间关系——预测用的是输入开头特殊记号 的最终表示14。 (BERT 的输入格式: + 第一句 + + 第二句 + , 每个位置的表示 = 词向量 + 句子编号向量 + 可学习位置向量,三者相加15。)

6. 作者的判断与证据

书里给了证据的: one-hot 余弦为 0 的推导;skip-gram/CBOW 的定义与 softmax 形式;负采样与层次 softmax 的成本分析;GloVe 的损失函数; fastText 的子词构成;ELMo/GPT/BERT 的对比(crane 与 bank 两个例子); MLM 的 15% 与 80/10/10;NSP 的一半相邻一半随机;类比任务的向量算术。

经验判断: 「3/4 次幂抽噪声词」「高频词子采样」是 word2vec 的祖传配方, 书里给了做法没给理论;词向量算术(man👩:son:daughter) 是经验现象,为什么会成立至今没有完全的理论解释。

判断(我们的,不是书里的): 这一章是理解「今天为什么还要学 word2vec」的关键—— 词向量作为成品已经被上下文化表示取代, 但它留下的三样东西还活着:自监督任务设计(MLM 就是「猜邻居」的升级)、 负采样/近似 softmax 的成本意识(今天的对比学习——把「真邻居拉近、假邻居推远」当训练信号的一类方法——还在用)、 子词单位(所有现代分词器的祖宗)。 如果错,会错在: 词向量在某些小数据、低资源(标注数据稀少的)场景仍是够用的廉价方案, 「被取代」指的是主流预训练范式,不是所有场景。

7. 边界与局限

  • NSP 后来被证伪了一半:RoBERTa 实验发现去掉 NSP 反而更好(第 13 章已提);
  • 词向量算术会带来偏见(「医生−男人+女人≈护士」),原书未展开公平性问题;
  • GloVe/fastText 都已淡出主流,本章讲的是思想史;
  • BERT 的预训练只在小语料上做了演示,真正的 BERT 要书籍+维基规模的数据;
  • 词相似/类比的评测只给方法,没给基准数字。

8. 可带走的

  1. one-hot 任意两词余弦=0,表达不了「意思近」;词向量让相似词靠近;
  2. word2vec:skip-gram 拿中心词猜邻居,CBOW 反过来;向量是「猜邻居」的副产品;
  3. 全词表 softmax 是成本黑洞:负采样换成 K+1 个二分类,层次 softmax 走二叉树;
  4. GloVe:计数派与预测派在加权平方损失里汇合;
  5. fastText:词 = 子词之和,未登录词也能拼出向量;
  6. ELMo 双向但任务特定,GPT 任务无关但单向,BERT 两头都要;
  7. MLM:15% 选中、80/10/10 替换,防训练-使用脱节;NSP 学句间关系, 代表整对句子。

9. 原文地图

主题原书章原文位置
one-hot 余弦为 0Word Embedding (word2vec)text/110-word-embedding-word2vec.txt:40(搜「cosine similarity between one-hot vectors」)
skip-gram 与 CBOWWord Embedding (word2vec)text/110-word-embedding-word2vec.txt:48(搜「skip-gram」)
loves 走查与窗口Word Embedding (word2vec)text/110-word-embedding-word2vec.txt:67(搜「center word」)
点积 softmaxWord Embedding (word2vec)text/110-word-embedding-word2vec.txt:98(搜「softmax operation on vector dot products」)
两种近似训练Approximate Trainingtext/111-approximate-training.txt:41(搜「hierarchical softmax」)
负采样 K 个噪声词Approximate Trainingtext/111-approximate-training.txt:105(搜「noise words」)
层次 softmax 二叉树Approximate Trainingtext/111-approximate-training.txt:166(搜「binary tree」)
GloVe 三处改动Word Embedding with Global Vectors (GloVe)text/114-word-embedding-with-global-vectors-glove.txt:140(搜「three changes」)
fastText 子词Subword Embeddingtext/115-subword-embedding.txt:38(搜「fastText」)
未登录词Subword Embeddingtext/115-subword-embedding.txt:82(搜「out-of-vocabulary」)
类比任务Word Similarity and Analogytext/116-word-similarity-and-analogy.txt:217(搜「is the form of a word analogy」)
crane 两义Bidirectional Encoder Representations from Transformers (BERT)text/117-bidirectional-encoder-representations-from-trans.txt:19(搜「crane」)
ELMo/GPT 对比Bidirectional Encoder Representations from Transformers (BERT)text/117-bidirectional-encoder-representations-from-trans.txt:69(搜「only looks forward」) · text/117-bidirectional-encoder-representations-from-trans.txt:600(搜「task-specific architectures」)
BERT 输入格式Bidirectional Encoder Representations from Transformers (BERT)text/117-bidirectional-encoder-representations-from-trans.txt:180(搜「segment embeddings, and positional embeddings」)
MLM 15% 与 80/10/10Bidirectional Encoder Representations from Transformers (BERT)text/117-bidirectional-encoder-representations-from-trans.txt:316(搜「15%」) · text/117-bidirectional-encoder-representations-from-trans.txt:325(搜「80% of the time」)
NSPBidirectional Encoder Representations from Transformers (BERT)text/117-bidirectional-encoder-representations-from-trans.txt:444(搜「next sentence prediction」)

Footnotes

  1. 出处:「Word Embedding (word2vec)」第 33-41 段(text/110-word-embedding-word2vec.txt:40,搜「cosine similarity between one-hot vectors」)。

  2. 出处:「Word Embedding (word2vec)」第 48 段(text/110-word-embedding-word2vec.txt:48,搜「skip-gram」)。Mikolov 等 2013 年的两篇论文各提出一个。

  3. 出处:「Word Embedding (word2vec)」第 67-72 段(text/110-word-embedding-word2vec.txt:67,搜「center word」)与第 97-102 段(text/110-word-embedding-word2vec.txt:98,搜「softmax operation on vector dot products」)。

  4. 出处:「Word Similarity and Analogy」第 210-217 段(text/116-word-similarity-and-analogy.txt:217,搜「is the form of a word analogy」)。原书的例子是 man👩:son:daughter,规则是找与 vec(c)+vec(b)−vec(a) 最相似的词。

  5. 出处:「Approximate Training」第 41 段(text/111-approximate-training.txt:41,搜「hierarchical softmax」)。

  6. 出处:「Approximate Training」第 105-109 段(text/111-approximate-training.txt:105,搜「noise words」)与第 136-143 段的损失推导。

  7. 出处:「Approximate Training」第 166 段(text/111-approximate-training.txt:166,搜「binary tree」)。

  8. 出处:「Word Embedding with Global Vectors (GloVe)」第 137-152 段(text/114-word-embedding-with-global-vectors-glove.txt:140,搜「three changes」)。GloVe = Global Vectors,对 log 共现计数做加权最小二乘。

  9. 出处:「Subword Embedding」第 38-43 段(text/115-subword-embedding.txt:38,搜「fastText」)与第 82 段(text/115-subword-embedding.txt:82,搜「out-of-vocabulary」)。

  10. 出处:「Bidirectional Encoder Representations from Transformers (BERT)」第 7 段(text/117-bidirectional-encoder-representations-from-trans.txt:7,搜「context-independent」)与第 19-20 段(text/117-bidirectional-encoder-representations-from-trans.txt:19,搜「crane」)。

  11. 出处:「Bidirectional Encoder Representations from Transformers (BERT)」第 69-80 段(text/117-bidirectional-encoder-representations-from-trans.txt:69,搜「only looks forward」)与第 600 段(text/117-bidirectional-encoder-representations-from-trans.txt:600,搜「task-specific architectures」)。

  12. 出处:「Bidirectional Encoder Representations from Transformers (BERT)」第 312-316 段(text/117-bidirectional-encoder-representations-from-trans.txt:316,搜「15%」)。

  13. 出处:「Bidirectional Encoder Representations from Transformers (BERT)」第 322-330 段(text/117-bidirectional-encoder-representations-from-trans.txt:325,搜「80% of the time」)。

  14. 出处:「Bidirectional Encoder Representations from Transformers (BERT)」第 437-448 段(text/117-bidirectional-encoder-representations-from-trans.txt:444,搜「next sentence prediction」)。一半样本是真相邻句(True),一半第二句随机抽(False)。

  15. 出处:「Bidirectional Encoder Representations from Transformers (BERT)」第 180-189 段(text/117-bidirectional-encoder-representations-from-trans.txt:180,搜「segment embeddings, and positional embeddings」)。