Lesson 18 · 約 28 分鐘
第一篇論文逐段讀
〈Attention Is All You Need〉 8 區塊拆解 + 讀論文肌肉養成
18 ・ 第一篇論文逐段讀:〈Attention Is All You Need〉
Lesson 10 教你「讀論文三遍法」,但沒帶你真的讀完一篇。這一講把 NLP 最重要 的論文〈Attention Is All You Need〉(Vaswani et al., 2017)拆成 8 個區塊, 每段告訴你「這段在說什麼」+「為什麼重要」+「初學者最容易誤解的地方」。 讀完你會有「讀現代 paper」的肌肉。
arXiv: https://arxiv.org/abs/1706.03762(8 頁,免費下載)
0. 為什麼挑這篇
1. Title + Abstract:30 秒抓重點
Title:「Attention Is All You Need」 — 故意誇張(原作說「半開玩笑」),但傳達的訊息精準。
Abstract 在說什麼:
- 當時主流翻譯模型 = encoder-decoder + recurrence / convolution + attention
- 「我們提出完全只用 attention 的架構,叫 Transformer」
- 「在 WMT 2014 EN→DE 翻譯任務上 BLEU 28.4(SOTA)、訓練快 N 倍」
讀 paper 的技巧:Abstract 三句話講完模型 + 一句話講結果 + 一句話講貢獻。沒看 abstract 不要往下讀,先確認「這篇跟我相關嗎」。
2. §1 Introduction:背景 + 動機
關鍵段落:
"Recurrent models typically factor computation along the symbol positions ... This inherently sequential nature precludes parallelization within training examples."
翻譯:RNN 一個一個字算,沒辦法平行 → GPU 沒法吃飽。這就是動機:我們要做平行化。
3. §3.1 Encoder-Decoder Stack:架構俯瞰
Encoder × 6 ─┐
├─ cross-attention ─→ Decoder × 6
│ │
src tokens ──→ embed + posenc ↓
tgt tokens (shifted)
Encoder(6 層,每層兩個 sub-layer):
- Multi-head self-attention:每個 token 看其他所有 token
- Position-wise FFN:每個 token 各自過一個 2-layer MLP
每個 sub-layer 外包 residual + LayerNorm(關鍵!沒這層深層訓不動)。
Decoder(6 層,三個 sub-layer):
- Masked self-attention:只能看自己 + 之前的 token(防止偷看未來)
- Cross-attention:Query 來自 decoder、Key/Value 來自 encoder 輸出
- FFN
理解技巧:把 encoder 想成「閱讀理解整段 source」,decoder 想成「一字一字寫翻譯,過程中可以回看 source」。
4. §3.2 Scaled Dot-Product Attention(心臟)
論文的核心公式:
Attention(Q, K, V) = softmax(Q·Kᵀ / √d_k) · V
逐字拆:
Q (queries):每個 token「想找的東西」K (keys):每個 token「能提供的標籤」V (values):每個 token「真正的內容」Q·Kᵀ:每個 query 跟每個 key 算「對盤分數」(內積大 = 對盤)/ √d_k:這個除法是論文最重要的工程細節
4.1 為什麼用 dot-product 不用 additive attention
論文 §3.2.1 對比兩種 attention:
- Additive(Bahdanau 等用):用 small MLP 算分數
- Dot-product(這篇):直接內積
選 dot-product 的理由:矩陣乘法在 GPU 上飛快。對小 d_k 兩者效果接近,但大 d_k 時要除 √d_k 才不會爆。
5. §3.2.2 Multi-Head Attention:8 個小頭比 1 個大頭強
為什麼分頭:單一 attention 只能學一種「對盤模式」。多頭 = 同時學多個(語法 / 指代 / 長距離依賴…)。
公式:
MultiHead(Q, K, V) = Concat(head_1, ..., head_h) · W_O
where head_i = Attention(Q·W_Q_i, K·W_K_i, V·W_V_i)
論文用 h = 8 個頭、d_k = d_v = d_model / h = 64。
6. §3.3 Position-wise FFN + §3.5 Positional Encoding
FFN
FFN(x) = max(0, x·W_1 + b_1)·W_2 + b_2
- 2-layer MLP,每個 token 獨立做(weights 跨 token 共享)
- 中間維度 d_ff = 2048(= 4 × d_model 是經驗值)
- 大量「事實知識」 存在這裡 — 後續論文證實 FFN 像 key-value memory
Positional Encoding
Attention 本身對位置不敏感({我愛你, 你愛我, 愛我你} 給 attention 看是一樣的 token bag)。怎麼讓模型知道順序?
論文用 sinusoidal positional encoding:
PE(pos, 2i) = sin(pos / 10000^(2i/d))
PE(pos, 2i+1) = cos(pos / 10000^(2i/d))
加在 token embedding 上。
7. §5 Training:三個關鍵超參數
| 細節 | 值 | 為什麼這個值 |
|---|---|---|
| Optimizer | Adam, β1=0.9, β2=0.98, ε=1e-9 | β2=0.98 不是默認 0.999 — transformer 梯度量級變化大,要更短的滑動視窗 |
| LR schedule | linear warmup(4000 steps) + inverse sqrt decay | 沒 warmup 開頭 lr 太大爆炸 |
| Regularization | dropout 0.1 + label smoothing 0.1 | label smoothing 讓 softmax 不要過度自信,改善 generalisation |
8. §6 Results + 怎麼看 table
主結果(Table 2):
Model EN-DE BLEU EN-FR BLEU Training Cost (FLOPs)
ByteNet 23.75 — —
GNMT + RL 24.6 39.92 2.3e19 (RNN)
ConvS2S 25.16 40.46 9.6e18 (CNN)
MoE 26.03 40.56 2.0e19 (mixture)
Transformer (base) 27.3 38.1 3.3e18 ← 4-7× cheaper, ~2 BLEU higher
Transformer (big) 28.4 41.8 2.3e19
怎麼讀:不是「Transformer 28.4 最高」 — 是「同樣算力 Transformer 高 2 BLEU,或同樣 BLEU 算力少 4-7×」。論文的真實貢獻是 效率 + 效果。
9. 讀完該做什麼
做到 4 = 你的內功超過 80% 自學者。
10. 「讀論文三遍法」 收尾示範
按 lesson 10 的方法:
第一遍(5 分鐘 skim):
- 看 title / abstract / figures / table 標題 / conclusion → 知道這篇在說什麼
第二遍(30 分鐘 understand):
- 認真讀 §3 (model architecture) + §5 (training) + §6 (results)
- 跳過 §1 §2 (background) 跟 §4 (why self-attention,有時間再看)
第三遍(2 小時 reproduce):
- 攤開 PyTorch 重寫 attention + multi-head + transformer block
- 對著 code/02_tiny_gpt.py 跟 paper 對照,差異記下來
Karpathy 的建議:讀完 paper 立刻 commit 一個 notes/2024-XX-XX_attention-is-all-you-need.md(用 lesson 12 範本),否則三天後忘 80%。
11. 推薦的下一篇
讀完這篇,可以接著讀:
| Paper | 為什麼接這篇 |
|---|---|
| GPT-1(2018) | 把 transformer decoder-only 推到 NLP 各任務 |
| BERT(2018) | encoder-only + masked LM,理解雙向資訊 |
| GPT-3(2020) | scale 推到 175B,few-shot learning 浮現 |
| InstructGPT / RLHF(2022) | 對齊技術根源 |
| LLaMA(2023) | 開源 LLM 的範本,RoPE + RMSNorm + SwiGLU 三件套 |
接下來
- 下一講 19 · RL 深入 — Policy gradient / PPO / DQN 比 Q-learning 進一步
- 想動手:真的重現 nanoGPT(Karpathy 8 小時影片) — 拿這篇 paper 當參考,寫到能跑能學