クエリ、キー、バリュー:ベクトルの役割とアテンションの定式化
AI アシスタント協力エンジニアリングチェック
前提条件
第 41-42 日の順伝播、行列積、行単位 Softmax の仕組み。
学習目標
アテンションをクエリとキーの照合によるバリューの動的集約として分解し、テンソル次元を導出し、生の内積スコア行列を計算する。
手順
参照資料: この活動に割り当てられた既存の読解・練習時間内で、Stanford CS224N: Natural Language Processing with Deep Learning の Winter 2026 Lecture 05 Transformers (2026-01-20); attention and self-attention slides を参照し、ローカル課題とのつながりを一つ自分の言葉で説明する。既存の演習と合格基準は維持し、外部資料が開けない場合は本コース独自の説明を使う。必須タスク:スケール化内積アテンション Attention(Q, K, V) = softmax(Q K^T / sqrt(d_k)) V を定式化します。系列長 T=3、キー次元 d_k=4、バリュー次元 d_v=2 とします。Q の各行を q_0=[2.0, 0.0, 2.0, 0.0], q_1=[0.0, 2.0, 0.0, 2.0], q_2=[1.0, 1.0, 1.0, 1.0];K の各行を k_0=[1.0, 0.0, 1.0, 0.0], k_1=[0.0, 2.0, 0.0, 1.0], k_2=[2.0, 1.0, 0.0, 1.0];V の各行を v_0=[1.0, 0.0], v_1=[0.0, 2.0], v_2=[1.0, 1.0] と設定します。生の内積行列 S_raw = Q K^T を段階的に手計算してください。クエリ(何を探しているか)、キー(何を提供しているか)、バリュー(渡される具体的情報)の役割を説明してください。
演習と予測記録
1. S_raw = Q K^T の 3x3 行列の全要素を計算してください。
2. Q、K、V、Q K^T、および最終出力行列のテンソル形状を答えてください。
3. なぜアテンションは固定線形層ではなく、データ駆動型の連想検索機構と言えるのですか?
S_raw の行 i 列 j は q_i と k_j の内積です。例えば S_raw[0,0] = 2*1 + 0*0 + 2*1 + 0*0 = 4 です。
S_raw 行列:第 0 行 [4.0, 0.0, 4.0];第 1 行 [0.0, 6.0, 4.0];第 2 行 [2.0, 3.0, 4.0]。テンソル形状:Q は (3,4)、K は (3,4)、V は (3,2)、Q K^T は (3,3)、出力は (3,2)。固定層の重みは学習後固定ですが、アテンション重みは現在の入力文脈に応じて動的に算出される適応的ルーティング係数です。
行列積の各ステップを正しく計算し、正確な 3x3 S_raw 行列とテンソル形状を導出できること。
公式ソースリソースと権利証跡
Stanford CS224N: Natural Language Processing with Deep Learning
該当セクション・タイムスタンプ: Winter 2026 Lecture 05 Transformers (2026-01-20); attention and self-attention slides
検証バージョンと日付: Public edition checked 2026-11-01; moving pages may change
Selected attention/Transformer lecture slides provide a coherent teaching check on Q/K/V and architecture; original papers remain definition references and local code exercises remain required.
Linear algebra, gradients, basic neural language models and PyTorch.
Use the original localized explanations, exercises and checks already present in this activity; no external account or purchase is required for that fallback.
比較対象候補リソースと選定評価 (2)
Runnable code commentary remains the implementation spine; its sublayer convention must not be mistaken for the original paper.
Concise diagrams are useful, but provide neither the wider lecture sequence nor checked implementation exercises.
Attention Is All You Need
該当セクション・タイムスタンプ: Section 3.2.1: Scaled Dot-Product Attention equation and query/key/value matrix definitions
検証バージョンと日付: 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.
nanoGPT: Simplest, Fastest Repository for Training and Finetuning Medium GPTs
該当セクション・タイムスタンプ: model.py: CausalSelfAttention forward pass query/key/value projection extraction
検証バージョンと日付: GitHub master commit (Verified: 2026-10-30)
Detailed modern reference implementation of clean, idiomatic batched causal self-attention in fewer than 60 lines of code.
Python, NumPy/PyTorch tensor fundamentals, and feed-forward network basics
Self-contained step-by-step tensor derivations, numeric matrix traces, and pure Python/NumPy implementations are directly provided in curriculum exercises.
比較対象候補リソースと選定評価 (3)
Includes extensive production abstraction layers, configuration polymorphism, and cache tracking that obscure direct mathematical tensor operations for beginner learners.
Selected jointly: provides line-by-line literary commentary mapping equations directly to PyTorch tensors.
Expanded video walkthrough; software is MIT-licensed while video audio/subtitles remain linking-only without transcript import.