按位置前饋網絡 (FFN) 架構
Agent 輔助工程分析
前置要求
第 46 日 Transformer Block 與第 41 日 MLP 基礎知識。
學習目標
解釋 FFN 作為詞元鍵值記憶庫的角色,及其典型的 4 倍隱層擴展維度 d_ff = 4 * d_model。
操作指引
1. 寫出帶全部形狀的 FFN:x 為 (T, d_model),W1 為 (d_model, d_ff),b1 為 (d_ff),W2 為 (d_ff, d_model),b2 為 (d_model),因此 FFN(x) = act(x W1 + b1) W2 + b2 仍為 (T, d_model)。
2. 解釋「按位置」:同一組 W1, b1, W2, b2 分別作用於每一行 x_t;詞元 s 的值不會進入詞元 t 的 FFN 輸出。跨詞元的混合由注意力負責。
3. 比較激活函數:ReLU max(0, z) 有硬性的零;GELU z * Phi(z) 平滑,允許小的負值通過;SwiGLU 使用門控 (Swish(x W) ⊙ (x V)) W2,因此需要三個矩陣而不是兩個。
4. 對 d_model = 64、d_ff = 256 計算參數量,先不含偏置,再含偏置。
5. 用 d_ff' ≈ (2/3) d_ff 並取整,求出使 SwiGLU 參數量大致相同的隱藏寬度。
時間安排(120 分鐘):閱讀 30 + 練習 60 + 檢查 30。 閱讀:Vaswani et al. 第 3.3 節(按位置前饋網絡)和 nanoGPT model.py 中的 MLP 類別。練習:第 1-5 項。檢查:獨立完成精熟題中的兩種參數量計算。
自主實作與記錄
1. 寫出標準兩層 FFN 數學公式,並標出每個張量的形狀。
2. 為何 FFN 被稱為 '按位置 (position-wise)'?
3. 計算無偏置下 W1 (64x256) 與 W2 (256x64) 的總參數量。
4. 加上偏置 b1 和 b2,給出新的總數。
5. SwiGLU 使用三個矩陣(d_model x d_ff' 的 W 和 V,以及 d_ff' x d_model 的 W2)。選擇 d_ff' 使其參數量接近第 3 項,並給出該數量。
FFN 獨立應用於序列中的每個詞元位置 t=1..T,在不同位置之間完全沒有信息交互,但共享同一套權重。
1. FFN(x) = act(x W1 + b1) W2 + b2,其中 x: (T, 64),W1: (64, 256),b1: (256),x W1 + b1: (T, 256),W2: (256, 64),b2: (64),輸出: (T, 64)。原始 Transformer 使用 ReLU;GPT-2 和 nanoGPT 使用 GELU。
2. 按位置意味著 FFN 以相同的權重分別作用於每個位置的詞元向量 x_t。輸出的第 t 行只取決於輸入的第 t 行,所以 T 行可以用一次批次矩陣乘法算完。它不在詞元之間傳遞資訊;那是注意力的工作。
3. W1: 64 * 256 = 16,384。W2: 256 * 64 = 16,384。無偏置總數: 32,768。即 d_ff = 4 * d_model 時的 2 * d_model * d_ff。
4. b1 增加 256,b2 增加 64,總數為 32,768 + 320 = 33,088 個參數。
5. 三個 d_model x d_ff' 矩陣共 3 * 64 * d_ff'。令其等於 2 * 64 * 256,得 d_ff' = (2/3) * 256 ≈ 170.7;取整為 171,得 3 * 64 * 171 = 32,832 個參數(與 LLaMA 式 SwiGLU 一樣不含偏置),與 32,768 相差不到 0.2%。實際中 d_ff' 常向上取到便於硬體的倍數,參數量會略有變化。
常見錯誤:只計算 W1;把 W1 的偏置寫成 64 個;以為 SwiGLU 仍保持 d_ff = 256,從而得到 49,152 個參數。
闡明按位置特性並準確推導出 4 倍擴展下的參數賬目。
權威來源資源與權利證明
Attention Is All You Need
對應章節 / 時間戳: Section 3.3 Position-wise Feed-Forward Networks
驗證版本與日期: 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)
High-level visual intuition, but Vaswani et al. provides the primary mathematical specifications needed for implementation.
Co-primary: PyTorch line-by-line implementation alongside the paper equations.
nanoGPT: model.py architecture implementation
對應章節 / 時間戳: model.py: class MLP (c_fc expands to 4 * n_embd, GELU, c_proj back to n_embd)
驗證版本與日期: 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)
Original legacy TensorFlow 1.x code; nanoGPT provides clean modern PyTorch equivalent.
Includes advanced production features (RoPE, GQA), but nanoGPT's simplicity is ideal for the core Days 46-48 assembly.