位置順適応型前向ネットワーク(FFN)のアーキテクチャ
Agent支援工学分析
前提条件
第46日のTransformerブロックと第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. 「位置ごと(position-wise)」を説明する:同じ 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 を使うため、行列が 2 つではなく 3 つ必要になる。
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. 各テンソルの形状を付けて、標準的な2層FFNの式を書け。
2. なぜFFNは「位置順適応型(position-wise)」と呼ばれるのか?
3. バイアスを含めずに、W1 (64x256) と W2 (256x64) のパラメータ数を計算せよ。
4. バイアス b1 と b2 を加えた合計を求めよ。
5. SwiGLU は 3 つの行列(d_model x d_ff' の W と V、d_ff' x d_model の W2)を使う。パラメータ数が項目 3 に近くなるよう d_ff' を選び、その数を求めよ。
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 行すべてを 1 回のバッチ行列積で計算できる。トークン間の通信は加えない。それは注意機構が担う。
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 つで 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% 以内。実際にはハードウェアに都合のよい倍数に切り上げることが多く、数が少しずれる。
よくある誤り: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.