自己回帰制約と下三角因果マスクの構築
AI アシスタント協力エンジニアリングチェック
前提条件
第 43 日のアテンション機構とスケーリング導出。
学習目標
デコーダ言語モデルの自己回帰制約を定義し、下三角因果マスク行列 M を構築し、マスク後スコア行列 S_masked = S + M を導出する。
手順
必須タスク:因果デコーダが P(x_1, ..., x_T) = prod_{t=1}^T P(x_t | x_{
演習と予測記録
1. 3x3 因果マスク行列 M を具体的に記述してください。
2. マスク後スコア行列 S_masked = S + M を記述してください。
3. 対角線より上を 0、下を -inf に誤って反転させた場合、どのような致命的欠陥が生じますか?
M の主対角線上および下側(下三角)はすべて 0、主対角線より上はすべて -inf です。第 0 行の要素 1, 2 は -inf となります。
マスク行列 M:
[[0.0, -inf, -inf],
[0.0, 0.0, -inf],
[0.0, 0.0, 0.0]]。
マスク後スコア S_masked:
[[2.0, -inf, -inf],
[0.0, 3.0, -inf],
[1.0, 1.5, 2.0]]。
上下を反転させた場合、過去の文脈を見ずに将来のみに注目することになり、自己回帰生成時にまったく推論不能となります。
下三角マスク M を正確に定式化し、3x3 S_masked 行列を算出できること。
公式ソースリソースと権利証跡
Attention Is All You Need
該当セクション・タイムスタンプ: Section 3.2.3: Masked Multi-Head Attention in the decoder preventing leftward information flow
検証バージョンと日付: 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
該当セクション・タイムスタンプ: subsequent_mask implementation using torch.triu and triangular boolean masking
検証バージョンと日付: 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.