48 / 90

第 48 日:完整 Decoder-Only Transformer:位置編碼、權重綁定與 Logits 投影

整合位置表徵,組裝完整的詞元到 Logits 端到端前向傳播,論證權重綁定 (Weight Tying),並完成架構綜合掌握度考核。

模型、預訓練與微調 (LoRA) 480 分鐘專注學習(8 小時)
01 120 分鐘

位置信息表示:可學習編碼、正弦編碼與旋轉位置編碼 (RoPE)

Agent 輔助工程分析

前置要求

第 43 日自注意力與第 47 日 Transformer 模組堆疊。

學習目標

解釋為何在無位置信息時自注意力具有排列等變性,對比可學習絕對編碼、正弦絕對編碼與旋轉位置編碼 (RoPE)。

操作指引

證明調換詞元 1 與詞元 2 將導致無位置編碼的自注意力輸出發生等變交換。定義加性位置編碼 x = tok_emb + pos_emb,並對比 RoPE 在複數平面上的乘性旋轉機制。

自主實作與記錄

1. 證明樸素點積注意力具有排列等變性。
2. GPT-2 如何將位置向量注入到詞元嵌入中?
3. RoPE 相比可學習絕對位置編碼具有什麼關鍵的外推優勢?

獨立掌握標準

清晰闡明排列等變性,準確對比加性編碼與 RoPE 的幾何本質。

權威來源資源與權利證明

linking arXiv.org perpetual non-exclusive license / Open Access

Attention Is All You Need

作者 / 機構: Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser, Illia Polosukhin | 原始出處連結 ↗

對應章節 / 時間戳: Section 3.5 Positional Encoding

驗證版本與日期: arXiv:1706.03762v7 (Verified: 2026-10-30)

入選理由:

Foundational primary literature defining the Transformer architecture, multi-head attention, and scaled dot-product attention.

來源前置條件:

Matrix multiplication and basic neural network concepts from Days 22-24 and 34-36.

安全回退方案(離線彈性):

All mathematical formulas and structural definitions are self-contained in the lesson text.

對比候選資源與選型分析 (2)
Illustrated Transformer (Jay Alammar) ↗ compared_alternative [CC BY-NC-SA 4.0]

High-level visual intuition, but Vaswani et al. provides the primary mathematical specifications needed for implementation.

Annotated Transformer (Harvard NLP) ↗ compared_selected_for_day46_48 [MIT]

Co-primary: PyTorch line-by-line implementation alongside the paper equations.

linking MIT

The Annotated Transformer

作者 / 機構: v2022: Austin Huang, Suraj Subramanian, Jonathan Sum, Khalid Almubarak, Stella Biderman; original: Sasha Rush (Harvard NLP) | 原始出處連結 ↗

對應章節 / 時間戳: Positional Encoding (class PositionalEncoding)

驗證版本與日期: Updated PyTorch 2.0 edition (Verified: 2026-10-30)

入選理由:

Clear reference implementation of Transformer modules, residual connections, and layer normalization.

來源前置條件:

PyTorch tensor operations and attention theory.

安全回退方案(離線彈性):

All block assembly and normalization logic are provided in runnable local Python snippets.

對比候選資源與選型分析 (2)
HuggingFace Transformers library source ↗ compared_alternative [Apache 2.0]

Industrial scale with hundreds of model variations; Annotated Transformer is pedagogically focused on the core architecture.

PyTorch nn.Transformer documentation ↗ compared_alternative [Modified BSD]

Standard library implementation, but internal C++ bindings obscure the first-principles data flow.

02 120 分鐘

端到端 Decoder-Only Transformer 前向全流程

Agent 輔助工程分析

前置要求

活動 01 及第 46-47 日所有模組。

學習目標

追蹤從整數詞元索引張量 (B, T) 到下一詞預測 Logits 張量 (B, T, V) 的完整張量生命週期。

操作指引

梳理五大核心階段:(1) 輸入詞元與位置編碼相加 -> (B, T, d);(2) 連續通過 N 個 Pre-LN Transformer Block -> (B, T, d);(3) 最終 LayerNorm 歸一化 -> (B, T, d);(4) LM Head 投影 -> (B, T, V);(5) 提取最後一個時間步切片計算損失。

自主實作與記錄

1. 給出 PyTorch 中 forward 方法的張量變遷簽名與核心代碼。
2. 對於 B=1, T=3, d=4, V=6,輸出 logits 的精確張量形狀是什麼?
3. 如何從 logits 中提取時間步 t=3 的下一詞預測分佈?

獨立掌握標準

完整準確推導出從整數輸入到最終 Logits 的全套張量變遷流程。

權威來源資源與權利證明

linking MIT

nanoGPT: model.py architecture implementation

作者 / 機構: Andrej Karpathy | 原始出處連結 ↗

對應章節 / 時間戳: model.py: class GPT forward(idx, targets): wte + wpe, Blocks, ln_f, lm_head

驗證版本與日期: Git master (Verified: 2026-10-30)

入選理由:

Compact historical code reference for the named implementation functions; CS336 spring2025 provides the lead component exercises. Do not infer top-p, atomic persistence or cloud budget enforcement from nanoGPT.

來源前置條件:

Python, PyTorch, and Day 43-45 attention fundamentals.

安全回退方案(離線彈性):

Local models and classes in the curriculum mirror nanoGPT semantics directly.

對比候選資源與選型分析 (2)
GPT-2 TensorFlow source (OpenAI) ↗ compared_alternative [Modified MIT License]

Original legacy TensorFlow 1.x code; nanoGPT provides clean modern PyTorch equivalent.

Llama 2 / 3 PyTorch source (Meta) ↗ compared_alternative [Llama 2 Community License]

Includes advanced production features (RoPE, GQA), but nanoGPT's simplicity is ideal for the core Days 46-48 assembly.

linking arXiv.org perpetual non-exclusive license / Open Access

Attention Is All You Need

作者 / 機構: Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser, Illia Polosukhin | 原始出處連結 ↗

對應章節 / 時間戳: Section 3 Model Architecture (3.1-3.5)

驗證版本與日期: arXiv:1706.03762v7 (Verified: 2026-10-30)

入選理由:

Foundational primary literature defining the Transformer architecture, multi-head attention, and scaled dot-product attention.

來源前置條件:

Matrix multiplication and basic neural network concepts from Days 22-24 and 34-36.

安全回退方案(離線彈性):

All mathematical formulas and structural definitions are self-contained in the lesson text.

對比候選資源與選型分析 (2)
Illustrated Transformer (Jay Alammar) ↗ compared_alternative [CC BY-NC-SA 4.0]

High-level visual intuition, but Vaswani et al. provides the primary mathematical specifications needed for implementation.

Annotated Transformer (Harvard NLP) ↗ compared_selected_for_day46_48 [MIT]

Co-primary: PyTorch line-by-line implementation alongside the paper equations.

03 120 分鐘

權重綁定 (Weight Tying) 與輸出 Logit 投影頭

Agent 輔助工程分析

前置要求

活動 02 與第 40 日嵌入表原理。

學習目標

解釋權重綁定假設:讓詞元輸入嵌入表與輸出語言模型投影頭共享同一套權重矩陣。

操作指引

分析 self.lm_head.weight = self.transformer.wte.weight。計算參數節省量(如 GPT-2 節省 50,257 * 768 ≈ 38.6M 參數)。論述詞元作為輸入特徵與其作為輸出類別的語義對稱性。

自主實作與記錄

1. 權重綁定在數學上對詞元輸入和輸出空間作出了什麼對稱性假設?
2. 在 V=32,000, d=4096 的模型中,權重綁定能直接省下多少參數?
3. 為什麼權重綁定有助於防止低頻生僻詞的表徵漂移?

獨立掌握標準

能準確計算參數節省規模並深刻解釋語義對稱性與梯度共享機理。

權威來源資源與權利證明

linking MIT

nanoGPT: model.py architecture implementation

作者 / 機構: Andrej Karpathy | 原始出處連結 ↗

對應章節 / 時間戳: model.py: class GPT __init__ weight tying (transformer.wte.weight = lm_head.weight)

驗證版本與日期: Git master (Verified: 2026-10-30)

入選理由:

Compact historical code reference for the named implementation functions; CS336 spring2025 provides the lead component exercises. Do not infer top-p, atomic persistence or cloud budget enforcement from nanoGPT.

來源前置條件:

Python, PyTorch, and Day 43-45 attention fundamentals.

安全回退方案(離線彈性):

Local models and classes in the curriculum mirror nanoGPT semantics directly.

對比候選資源與選型分析 (2)
GPT-2 TensorFlow source (OpenAI) ↗ compared_alternative [Modified MIT License]

Original legacy TensorFlow 1.x code; nanoGPT provides clean modern PyTorch equivalent.

Llama 2 / 3 PyTorch source (Meta) ↗ compared_alternative [Llama 2 Community License]

Includes advanced production features (RoPE, GQA), but nanoGPT's simplicity is ideal for the core Days 46-48 assembly.

linking MIT

The Annotated Transformer

作者 / 機構: v2022: Austin Huang, Suraj Subramanian, Jonathan Sum, Khalid Almubarak, Stella Biderman; original: Sasha Rush (Harvard NLP) | 原始出處連結 ↗

對應章節 / 時間戳: Embeddings and Softmax (class Generator)

驗證版本與日期: Updated PyTorch 2.0 edition (Verified: 2026-10-30)

入選理由:

Clear reference implementation of Transformer modules, residual connections, and layer normalization.

來源前置條件:

PyTorch tensor operations and attention theory.

安全回退方案(離線彈性):

All block assembly and normalization logic are provided in runnable local Python snippets.

對比候選資源與選型分析 (2)
HuggingFace Transformers library source ↗ compared_alternative [Apache 2.0]

Industrial scale with hundreds of model variations; Annotated Transformer is pedagogically focused on the core architecture.

PyTorch nn.Transformer documentation ↗ compared_alternative [Modified BSD]

Standard library implementation, but internal C++ bindings obscure the first-principles data flow.

04 120 分鐘

Transformer 架構綜合掌握度終結考核

完全獨立閉卷考核

前置要求

完成活動 01 至 03。

學習目標

完成閉卷綜合考核:獨立手算一個 2 詞元微型序列通過 1 層玩具 Transformer 的端到端全流程。

操作指引

在 V=4, d_model=2, n_heads=1, N_layers=1 的極小配置下,手算輸入序列 [0, 2] 的嵌入、因果自注意力、殘差相加、歸一化及最終 logits。

自主實作與記錄

1. 用 5 個簡潔的核心數學等式總結端到端前向推理過程。
2. 解釋為何在因果解碼下詞元 0 絕不能關注到詞元 2。
3. 寫出當監督目標為 [2, 1] 時的交叉熵損失計算公式。

獨立掌握標準

獨立完成全部端到端公式閉卷默寫,準確掌握因果約束與損失計算。

權威來源資源與權利證明

linking arXiv.org perpetual non-exclusive license / Open Access

Attention Is All You Need

作者 / 機構: Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser, Illia Polosukhin | 原始出處連結 ↗

對應章節 / 時間戳: Section 3 Model Architecture (3.1-3.5)

驗證版本與日期: arXiv:1706.03762v7 (Verified: 2026-10-30)

入選理由:

Foundational primary literature defining the Transformer architecture, multi-head attention, and scaled dot-product attention.

來源前置條件:

Matrix multiplication and basic neural network concepts from Days 22-24 and 34-36.

安全回退方案(離線彈性):

All mathematical formulas and structural definitions are self-contained in the lesson text.

對比候選資源與選型分析 (2)
Illustrated Transformer (Jay Alammar) ↗ compared_alternative [CC BY-NC-SA 4.0]

High-level visual intuition, but Vaswani et al. provides the primary mathematical specifications needed for implementation.

Annotated Transformer (Harvard NLP) ↗ compared_selected_for_day46_48 [MIT]

Co-primary: PyTorch line-by-line implementation alongside the paper equations.

linking MIT

nanoGPT: model.py architecture implementation

作者 / 機構: Andrej Karpathy | 原始出處連結 ↗

對應章節 / 時間戳: model.py: class GPT forward(idx, targets): wte + wpe, Blocks, ln_f, lm_head

驗證版本與日期: Git master (Verified: 2026-10-30)

入選理由:

Compact historical code reference for the named implementation functions; CS336 spring2025 provides the lead component exercises. Do not infer top-p, atomic persistence or cloud budget enforcement from nanoGPT.

來源前置條件:

Python, PyTorch, and Day 43-45 attention fundamentals.

安全回退方案(離線彈性):

Local models and classes in the curriculum mirror nanoGPT semantics directly.

對比候選資源與選型分析 (2)
GPT-2 TensorFlow source (OpenAI) ↗ compared_alternative [Modified MIT License]

Original legacy TensorFlow 1.x code; nanoGPT provides clean modern PyTorch equivalent.

Llama 2 / 3 PyTorch source (Meta) ↗ compared_alternative [Llama 2 Community License]

Includes advanced production features (RoPE, GQA), but nanoGPT's simplicity is ideal for the core Days 46-48 assembly.