BERT 和 GPT 有什么区别?
为什么现在的 LLM 都是 decoder-only?
先弄清楚:每个概念是什么
- Bidirectional vs causal mask
- BERT 和 GPT 的 block 几乎一样,差别在 mask。BERT(encoder-only)每个词左右都能看。GPT(decoder-only)每个词只能看自己和左边,右边被 causal mask 遮住,所以能一个个往后生成(autoregressive)。例:「我 爱 吃 苹果」,BERT 的「吃」看 4 个词,GPT 的「吃」只看「我 爱 吃」。
- MLM(masked language modeling)
- 像完形填空,随机挑 15% 的 token 让模型猜。挑中的 80% 换成 [MASK]、10% 换随机词、10% 不变。例:1000 个 token 只有 150 个算 loss。
30 秒回答
BERT is an encoder-only Transformer with bidirectional self-attention, pre-trained with masked language modeling: mask 15% of tokens and predict them from both sides. GPT is decoder-only with a causal mask, trained to predict the next token. BERT gives strong representations for understanding tasks like classification, NER and retrieval, usually after fine-tuning, while GPT generates text and does few-shot in-context learning. LLMs converged on decoder-only because the objective matches generation, every token gives a training signal instead of 15%, any task can be phrased as text continuation, and the causal mask lets us reuse a KV cache.
推导 / 关键公式
BERT 只在被 mask 的位置 \(\mathcal{M}\)(约 15%)算 loss:
\[ \mathcal{L}_{\text{MLM}} = -\sum_{i\in\mathcal{M}} \log p(x_i \mid x_{\setminus\mathcal{M}}) \]GPT 每个位置都算:
\[ \mathcal{L}_{\text{LM}} = -\sum_{t=1}^{T} \log p(x_t \mid x_{<t}) \]两者只差 attention 里加不加 causal mask:
\[ \mathrm{softmax}\Big(\frac{QK^\top}{\sqrt{d_k}} + M\Big),\quad M_{ij} = -\infty \ \text{if}\ j > i \]BERT 不能直接用 next-token,因为双向时每个词能间接"看到自己",只能先遮掉再猜。
对比
| BERT | GPT | |
|---|---|---|
| 结构 | encoder-only,bidirectional | decoder-only,causal mask |
| objective | MLM(原版另有 NSP) | next-token prediction |
| 算 loss 的 token | 约 15% | 全部 |
| 用法 | 加 head 再 fine-tune | prompt(zero / few-shot,不更新 weight),也可 fine-tune |
| 擅长 | classification、NER、retrieval embedding | 生成、in-context learning |
为什么 LLM 都是 decoder-only
- objective 就是生成:pre-training 和 inference 同一个格式,任何任务都能写成续写(GPT-2 zero-shot,GPT-3 few-shot)。
- 信号密:每个 token 都是 target,BERT 每批只预测 15%。
- 推理便宜:causal 下前面 token 的 K、V 不变,可以 KV cache;双向模型每加一个词,前面全部要重算。
- 实验:只做无监督 pre-training 时,causal decoder-only 的 zero-shot 最好;但之后还要 fine-tune 的话,双向 + MLM 的模型更强。
必答点
- BERT:encoder-only、bidirectional、MLM(15%,80/10/10)
- GPT:decoder-only、causal mask、next-token prediction
- BERT 擅长理解 + fine-tune;GPT 擅长生成 + in-context learning
- decoder-only 赢在:objective = 生成、每个 token 都有 loss、KV cache、zero / few-shot
追问
为什么 80/10/10,不全换成 [MASK]?
fine-tuning 时没有 [MASK],全换会造成 pre-train / fine-tune mismatch。模型也不知道哪个词被换过,只能给每个 token 都保留好的上下文表示。
NSP 有用吗?
BERT 原文说对 QA、NLI 有帮助。RoBERTa 发现去掉 NSP 持平或略好,之后的模型基本不用。
BERT 怎么做 doc embedding 和双塔检索?(NVIDIA 原题)
query、doc 各用一个 BERT 编码,取 [CLS] 向量,相似度是 dot product。没 fine-tune 的 [CLS] 不是好的句向量,所以用 in-batch negatives 训练,loss 是正例对所有候选的 softmax cross-entropy(DPR)。inference 时 doc 向量离线建 FAISS index,线上只编码 query 再做 ANN(见 RAG)。
来源Devlin et al. 2019(BERT) §3(p.3)、§3.1(p.4)、Appendix A.1(p.12)、C.1(p.16)· Radford et al. 2019(GPT-2) §1–2(p.2)· Brown et al. 2020(GPT-3) §1(p.3)、§2.1(p.8)、§5(p.33)· Liu et al. 2019(RoBERTa) §4.2(pp.4–5)· Wang et al. 2022 Abstract(p.1)· Karpukhin et al. 2020(DPR) §3(p.3)· Raffel et al. 2020(T5) §3.1.4(pp.12–13)、§3.2.1(pp.15–16)