44 / 90

第 44 日:自己回帰的因果マスクと厳密な不変性

生成言語モデルにおいて自己回帰的制約を厳密に適用します。下三角因果マスクを構築し、Softmax が -inf を正確に確率 0 へ写像することを証明し、将来トークンの摂動が過去の表現に一切影響を与えないことを実験的に検証します。

モデルと訓練 480 分のアクティブ学習(8 時間の集中学習)
01 120 分

自己回帰制約と下三角因果マスクの構築

AI アシスタント協力エンジニアリングチェック

前提条件

第 43 日のアテンション機構とスケーリング導出。

学習目標

デコーダ言語モデルの自己回帰制約を定義し、下三角因果マスク行列 M を構築し、マスク後スコア行列 S_masked = S + M を導出する。

手順

必須タスク:因果デコーダが P(x_1, ..., x_T) = prod_{t=1}^T P(x_t | x_{ t にアクセスでき、訓練中にカンニング漏洩が発生することを論証してください。j <= i のとき M_{i,j} = 0、j > i のとき M_{i,j} = -inf となる加性因果マスク M in R^{T x T} を定義します。系列長 T=3 の具体的な M を記述し、第 43 日のスケール後スコア S = [[2.0, 0.0, 2.0], [0.0, 3.0, 2.0], [1.0, 1.5, 2.0]] に加算して S_masked = S + M を計算してください。

演習と予測記録

1. 3x3 因果マスク行列 M を具体的に記述してください。
2. マスク後スコア行列 S_masked = S + M を記述してください。
3. 対角線より上を 0、下を -inf に誤って反転させた場合、どのような致命的欠陥が生じますか?

独立習得基準

下三角マスク M を正確に定式化し、3x3 S_masked 行列を算出できること。

公式ソースリソースと権利証跡

linking arXiv.org perpetual non-exclusive license / Open Access

Attention Is All You Need

著者・提供機関: Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser, Illia Polosukhin | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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.

Karpathy nanoGPT (causal self-attention module) ↗ compared_selected_co_primary [MIT]

Selected jointly as the primary code reference: translates Section 3.2 of the Vaswani paper into minimal, idiomatic PyTorch tensor operations.

linking MIT

The Annotated Transformer

著者・提供機関: v2022: Austin Huang, Suraj Subramanian, Jonathan Sum, Khalid Almubarak, Stella Biderman; original: Sasha Rush (Harvard NLP) | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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)
Jay Alammar: The Illustrated Transformer ↗ compared_alternative [CC BY-NC 4.0]

Spectacular visual diagrams and intuitive color-coding, but lacks executable tensor code and formal numerical shape validation.

Stanford CS224N: Natural Language Processing with Deep Learning (Lecture 10) ↗ compared_alternative [Stanford University Copyright]

Rigorous academic slides and video lectures, but access to assignment grading is restricted to Stanford enrolled students.

Vaswani et al.: Attention Is All You Need ↗ compared_selected_co_primary [arXiv Open Access]

The original primary paper; Annotated Transformer annotates it directly.

02 120 分

因果マスク下での Softmax:段階的行列導出

自立型習得チェック

前提条件

第 44 日アクティビティ 1 を完了していること。

学習目標

マスクされたスコア行列に対し最大値シフト Softmax を適用し、因果アテンション重み A_causal を求め、因果加重出力を計算する。

手順

必須タスク:S_masked = [[2.0, -inf, -inf], [0.0, 3.0, -inf], [1.0, 1.5, 2.0]] に対し行単位 Softmax を計算します。第 0 行:有限値の最大値は 2.0。exp([2.0-2.0, -inf, -inf]) = [1.0, 0.0, 0.0]。正規化により A_causal[0, :] = [1.0, 0.0, 0.0]。第 1 行:最大値は 3.0。exp([-3.0, 0.0, -inf]) = [exp(-3), 1.0, 0.0] ~= [0.049787, 1.0, 0.0]、和は 1.049787。A_causal[1, :] = [0.047426, 0.952574, 0.0]。第 2 行は非マスクで [0.186324, 0.307196, 0.506480]。第 43 日の V = [[1,0], [0,2], [1,1]] を用いて因果出力 Out_causal = A_causal V を計算してください。

演習と予測記録

1. 小数点以下 6 桁まで正確な A_causal の各行を計算してください。
2. 3 行すべての因果出力 Out_causal = A_causal V を求めてください。
3. 第 43 日の未マスク Out[0] と本アクティビティの Out_causal[0] を比較し、本質的相違を説明してください。

独立習得基準

A_causal と Out_causal の全数値を参照値と 4 桁一致で算出できること。

公式ソースリソースと権利証跡

linking MIT

The Annotated Transformer

著者・提供機関: v2022: Austin Huang, Suraj Subramanian, Jonathan Sum, Khalid Almubarak, Stella Biderman; original: Sasha Rush (Harvard NLP) | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Attention masking implementation: masked_fill with -1e9 before softmax

検証バージョンと日付: 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)
Jay Alammar: The Illustrated Transformer ↗ compared_alternative [CC BY-NC 4.0]

Spectacular visual diagrams and intuitive color-coding, but lacks executable tensor code and formal numerical shape validation.

Stanford CS224N: Natural Language Processing with Deep Learning (Lecture 10) ↗ compared_alternative [Stanford University Copyright]

Rigorous academic slides and video lectures, but access to assignment grading is restricted to Stanford enrolled students.

Vaswani et al.: Attention Is All You Need ↗ compared_selected_co_primary [arXiv Open Access]

The original primary paper; Annotated Transformer annotates it directly.

linking MIT

nanoGPT: Simplest, Fastest Repository for Training and Finetuning Medium GPTs

著者・提供機関: Andrej Karpathy | オリジナル出所URL ↗

該当セクション・タイムスタンプ: model.py: att = att.masked_fill(self.bias[:,:,:T,:T] == 0, float("-inf"))

検証バージョンと日付: 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)
Hugging Face transformers (GPT2Attention) ↗ compared_rejected_for_foundations [Apache-2.0]

Includes extensive production abstraction layers, configuration polymorphism, and cache tracking that obscure direct mathematical tensor operations for beginner learners.

Harvard NLP: The Annotated Transformer ↗ compared_selected_co_primary [MIT]

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.

03 120 分

因果不変性摂動実験と代数的証明

AI アシスタント協力エンジニアリングチェック

前提条件

第 44 日アクティビティ 2 を完了していること。

学習目標

将来トークンを摂動させた際に、過去位置のアテンション分布および出力表現が厳密に不変であることを実験と代数で証明する。

手順

必須タスク:系列に対する厳密な摂動テストを実施します。将来トークン(t=2)を極端な値に置換します:q_2' = [99.0, -42.0, 13.0, 7.0], k_2' = [-50.0, 12.0, 34.0, -8.0], v_2' = [100.0, -200.0]。S_masked、A_causal、Out_causal を再計算し、diff(A_causal[0], A_causal_pert[0]) = 0.0、diff(A_causal[1], A_causal_pert[1]) = 0.0、diff(Out[0], Out_pert[0]) = 0.0、diff(Out[1], Out_pert[1]) = 0.0 を証明してください。過去全行が汚染される未マスク層と対比します。

演習と予測記録

1. Softmax の分子・分母の式から、A_causal の行 i が j > i の任意の k_j や v_j に依存しない理由を証明してください。
2. トークン 2 の摂動前後における第 0、1、2 行の最大絶対誤差を報告してください。
3. この因果不変性が、自己回帰推論における KV-Cache(Key-Value キャッシュ)の正当性をどのように担保するか説明してください。

独立習得基準

将来独立性の代数的証明を提示し、実験的に先行位置の差が正確に 0.0 であることを確認できること。

公式ソースリソースと権利証跡

linking arXiv.org perpetual non-exclusive license / Open Access

Attention Is All You Need

著者・提供機関: Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser, Illia Polosukhin | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Section 3.2.3: Autoregressive self-attention property ensuring causal factorization

検証バージョンと日付: 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.

Karpathy nanoGPT (causal self-attention module) ↗ compared_selected_co_primary [MIT]

Selected jointly as the primary code reference: translates Section 3.2 of the Vaswani paper into minimal, idiomatic PyTorch tensor operations.

linking MIT

nanoGPT: Simplest, Fastest Repository for Training and Finetuning Medium GPTs

著者・提供機関: Andrej Karpathy | オリジナル出所URL ↗

該当セクション・タイムスタンプ: model.py: CausalSelfAttention buffer verification ensuring past tokens remain strictly invariant

検証バージョンと日付: 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)
Hugging Face transformers (GPT2Attention) ↗ compared_rejected_for_foundations [Apache-2.0]

Includes extensive production abstraction layers, configuration polymorphism, and cache tracking that obscure direct mathematical tensor operations for beginner learners.

Harvard NLP: The Annotated Transformer ↗ compared_selected_co_primary [MIT]

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.

04 120 分

自立型習得チェック:因果漏洩、マスクの不具合と不変性表明

自立型習得チェック

前提条件

アクティビティ 44-01 から 44-03。

学習目標

AI 支援なしの宣言条件下で、因果マスクのバグを検出し、2 トークン系列の因果アテンションを手計算し、厳密な不変性を表明する。

手順

必須タスク:自力で解答してください。T=2、スケール後スコア S = [[3.0, 1.0], [2.0, 4.0]]、V = [[1.0, 2.0], [3.0, 0.0]] に対し:1. 2x2 因果マスク M を構築し S_masked を記述する。2. 正確な 2x2 因果アテンション行列 A_causal を計算する。3. Out_causal を計算する。4. トークン 1 を任意の値に置換した際の Out_causal[0] の変化量を証明する。5. 客観テストに解答する。

演習と予測記録

1. 2x2 配置における S_masked を記述してください。
2. 小数点以下 6 桁まで正確な A_causal を計算してください。
3. Out_causal = A_causal V を計算してください。
4. トークン 1 を摂動させた際の Out_causal[0] の厳密な変化量を述べてください。
5. 3 問の客観クイズに解答してください。

客観的理解度チェック:因果マスクと厳密な不変性

以下の設問に解答して理解度を確認してください。提出前に利用した補助条件を誠実に申告してください。

申告補助条件

注記: AI不使用は誠実な申告条件であり、監視や強制検知ではありません。

1.

自己回帰生成における下三角因果マスク(Causal Mask)の主たる操作目的は何ですか?

2.

アテンションスコアに加算する因果マスクを構築する際、主対角線より上の上三角成分(j > i)に設定すべき値は何ですか?

3.

系列長 3(t in {0, 1, 2})の系列においてトークン 2 を任意の値で摂動させた場合、厳密な因果マスク下で位置 0 のアテンション重みと出力表現はどう変化しますか?

解答根拠と履歴

    独立習得基準

    2x2 因果アテンションと出力を正確に導出し、歴史的不変性を正しく証明してクイズで 100% 満点を獲得できること。

    公式ソースリソースと権利証跡

    linking MIT

    The Annotated Transformer

    著者・提供機関: v2022: Austin Huang, Suraj Subramanian, Jonathan Sum, Khalid Almubarak, Stella Biderman; original: Sasha Rush (Harvard NLP) | オリジナル出所URL ↗

    該当セクション・タイムスタンプ: subsequent_mask unit tests and triangular boundary checks

    検証バージョンと日付: 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)
    Jay Alammar: The Illustrated Transformer ↗ compared_alternative [CC BY-NC 4.0]

    Spectacular visual diagrams and intuitive color-coding, but lacks executable tensor code and formal numerical shape validation.

    Stanford CS224N: Natural Language Processing with Deep Learning (Lecture 10) ↗ compared_alternative [Stanford University Copyright]

    Rigorous academic slides and video lectures, but access to assignment grading is restricted to Stanford enrolled students.

    Vaswani et al.: Attention Is All You Need ↗ compared_selected_co_primary [arXiv Open Access]

    The original primary paper; Annotated Transformer annotates it directly.

    linking MIT

    nanoGPT: Simplest, Fastest Repository for Training and Finetuning Medium GPTs

    著者・提供機関: Andrej Karpathy | オリジナル出所URL ↗

    該当セクション・タイムスタンプ: model.py: causal mask shape registration and forward assertion

    検証バージョンと日付: 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)
    Hugging Face transformers (GPT2Attention) ↗ compared_rejected_for_foundations [Apache-2.0]

    Includes extensive production abstraction layers, configuration polymorphism, and cache tracking that obscure direct mathematical tensor operations for beginner learners.

    Harvard NLP: The Annotated Transformer ↗ compared_selected_co_primary [MIT]

    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.