マルチヘッド部分空間:なぜ、どのように d_model を分割するか
AI アシスタント協力エンジニアリングチェック
前提条件
第 43-44 日のスケール化内積アテンションと因果マスク機構。
学習目標
マルチヘッドアテンションの表現上の必要性を説明し、線形射影 W^Q, W^K, W^V, W^O を定式化し、微小規模および標準 GPT 規模のパラメータ台帳を導出する。
手順
必須タスク:単一のアテンションヘッドが異なる種類の関係(構文的依存と長距離の意味的照応など)を単一の加重平均に圧縮してしまう限界を説明します。MultiHead(Q,K,V) = Concat(head_1, ..., head_h) W^O(各 head_i = Attention(Q W_i^Q, K W_i^K, V W_i^V))を定式化してください。d_model = 4, h = 2 のとき、d_k = d_v = 2 です。パラメータ台帳:W^Q, W^K, W^V, W^O はそれぞれ (4, 4) 行列で各 16 パラメータ(バイアスなし計 64、バイアスあり計 80)。GPT-2 Small(d_model = 768, h = 12, d_k = 64)の台帳:重み 4 * 768*768 = 2,359,296、バイアス 4*768 = 3,072、合計 2,362,368 パラメータを算出してください。
演習と予測記録
1. 単一のアテンションヘッドが局所的近接性と長距離構文一致を同時に捕捉できないのはなぜですか?
2. d_model = 4, h = 2 のバイアス有無両方での正確なパラメータ台帳を記述してください。
3. GPT-2 Small アテンション層(d_model = 768, h = 12)のパラメータ台帳を導出してください。
単一ヘッドはトークン対ごとに 1 つのスカラー重みしか出力できません。マルチヘッドは h 個の独立したルーティング重みを提供し、独立した部分空間表現を可能にします。
単一ヘッドはトークンごとに 1 つの値ベクトルの凸結合しか生成できず、近接単語に重みを割くと長距離の主語に注目できなくなります。d_model=4, h=2 では:4 つの (4,4) 行列で 64 重み、バイアス(+16)込みで 80。GPT-2 Small では:W^Q, W^K, W^V, W^O の 4 つの (768,768) 行列 = 2,359,296 重み、4 つの 768 次元バイアス = 3,072、総アテンションパラメータ数は 2,362,368 です。
部分空間ルーティングの概念を明確に説明し、微小規模および GPT-2 規模のパラメータ台帳を誤りなく導出できること。
公式ソースリソースと権利証跡
Attention Is All You Need
該当セクション・タイムスタンプ: Section 3.2.2: Multi-Head Attention architecture, linear projections, and parameter equations
検証バージョンと日付: NeurIPS 2017 / arXiv:1706.03762v7 (Verified: 2026-10-30)
The foundational primary literature establishing scaled dot-product attention, multi-head projection mechanisms, and autoregressive causal masking.
Linear algebra, matrix multiplication, softmax probability distributions
Complete original algebraic derivations, matrix examples, variance proofs, and tensor diagrams are self-contained in the curriculum text without external document dependency.
比較対象候補リソースと選定評価 (3)
Pioneered additive attention in RNNs, but Vaswani scaled dot-product attention replaced additive alignment in modern LLM architectures due to parallel matrix multiplication speed.
Introduces relative positional biases into attention; deferred to subsequent positional embedding modules after absolute scaled attention is thoroughly mastered.
Selected jointly as the primary code reference: translates Section 3.2 of the Vaswani paper into minimal, idiomatic PyTorch tensor operations.
The Annotated Transformer
該当セクション・タイムスタンプ: MultiHeadedAttention module implementation: linear projections and subspace splitting
検証バージョンと日付: Updated 2022 edition (Verified: 2026-10-30)
Widely used pedagogical commentary that pairs every paragraph of the original Vaswani paper with an executable, testable code cell.
Intermediate Python, PyTorch broadcasting and tensor slicing semantics
The curriculum provides standalone hand-worked numerical tables, explicit causal mask verification scripts, and offline analytical proofs.
比較対象候補リソースと選定評価 (3)
Spectacular visual diagrams and intuitive color-coding, but lacks executable tensor code and formal numerical shape validation.
Rigorous academic slides and video lectures, but access to assignment grading is restricted to Stanford enrolled students.
The original primary paper; Annotated Transformer annotates it directly.