48 / 90

第48日:完全なDecoder-Only Transformer:位置符号化、重み共有とLogits射影

位置表現を統合し、トークンからLogitsまでの完全な端から端への順伝播を組み立て、重み共有(Weight Tying)を論証し、アーキテクチャ総合試験に合格する。

モデル・事前学習とLoRA 480分の集中学習(8時間)
01 120 分

位置情報の表現:学習可能埋め込み、正弦波埋め込みと回転位置埋め込み(RoPE)

Agent支援工学分析

前提条件

第43日のアテンションと第47日のTransformerブロックの積層。

学習目標

位置情報がない場合に自己注意力が置換同変性を持つ理由を説明し、加算的位置埋め込みとRoPEを比較する。

手順

位置指標のない自己注意機構において、トークン1とトークン2を入れ替えると出力も対応して入れ替わることを示せ。加算的位置埋め込み x = tok_emb + pos_emb と、複素平面上の乗算的な RoPE 回転を定義し、対比せよ。

演習と予測記録

1. 素朴な自己注意力が置換同変性を持つことを示せ。
2. GPT-2はどのように位置情報を埋め込むか?
3. RoPEが絶対位置埋め込みに対して持つ外挿上の優位性は何か?

独立習得基準

置換同変性を論述し、加算埋め込みとRoPEの幾何学的差異を正確に比較できること。

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

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.5 Positional Encoding

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

High-level visual intuition, but Vaswani et al. provides the primary mathematical specifications needed for implementation.

Annotated Transformer (Harvard NLP) ↗ compared_selected_for_day46_48 [MIT]

Co-primary: PyTorch line-by-line implementation alongside the paper equations.

linking MIT

The Annotated Transformer

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

該当セクション・タイムスタンプ: Positional Encoding (class PositionalEncoding)

検証バージョンと日付: Updated PyTorch 2.0 edition (Verified: 2026-10-30)

選定理由:

Clear reference implementation of Transformer modules, residual connections, and layer normalization.

ソース前提知識:

PyTorch tensor operations and attention theory.

安全フォールバック(オフライン回復性):

All block assembly and normalization logic are provided in runnable local Python snippets.

比較対象候補リソースと選定評価 (2)
HuggingFace Transformers library source ↗ compared_alternative [Apache 2.0]

Industrial scale with hundreds of model variations; Annotated Transformer is pedagogically focused on the core architecture.

PyTorch nn.Transformer documentation ↗ compared_alternative [Modified BSD]

Standard library implementation, but internal C++ bindings obscure the first-principles data flow.

02 120 分

完全なDecoder-Only Transformerの端から端への順伝播

Agent支援工学分析

前提条件

活動01および第46-47日の全モジュール。

学習目標

整数トークン (B, T) から次トークン予測Logits (B, T, V) に至る完全なライフサイクルを追跡する。

手順

5つのパイプライン段階を順に追え:(1) 入力 idx(形状 (B, T))-> トークン埋め込み + 位置埋め込み -> (B, T, d);(2) N個のブロックの積層 -> (B, T, d);(3) 最終 LayerNorm -> (B, T, d);(4) 出力ヘッドの射影 -> (B, T, V);(5) 次トークン損失のための Logits の取り出し。

演習と予測記録

1. PyTorchにおけるforwardメソッドのコードを示せ。
2. B=1, T=3, d=4, V=6 のときのlogitsテンソルの形状を答えよ。
3. 次トークンの予測分布はlogitsのどのスライスから得るか?

独立習得基準

トークン入力からLogits出力までのテンソル変換フローを完全に記述できること。

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

linking MIT

nanoGPT: model.py architecture implementation

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

該当セクション・タイムスタンプ: model.py: class GPT forward(idx, targets): wte + wpe, Blocks, ln_f, lm_head

検証バージョンと日付: 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)
GPT-2 TensorFlow source (OpenAI) ↗ compared_alternative [Modified MIT License]

Original legacy TensorFlow 1.x code; nanoGPT provides clean modern PyTorch equivalent.

Llama 2 / 3 PyTorch source (Meta) ↗ compared_alternative [Llama 2 Community License]

Includes advanced production features (RoPE, GQA), but nanoGPT's simplicity is ideal for the core Days 46-48 assembly.

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 Model Architecture (3.1-3.5)

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

High-level visual intuition, but Vaswani et al. provides the primary mathematical specifications needed for implementation.

Annotated Transformer (Harvard NLP) ↗ compared_selected_for_day46_48 [MIT]

Co-primary: PyTorch line-by-line implementation alongside the paper equations.

03 120 分

重み共有(Weight Tying)と出力Logit射影ヘッド

Agent支援工学分析

前提条件

活動02および第40日の埋め込み表。

学習目標

入力埋め込み行列と出力言語モデルヘッドの重みを共有する重み共有仮説を解説する。

手順

self.lm_head.weight = self.transformer.wte.weight を分析せよ。パラメータ削減量を示せ:V * d_model 個のパラメータを削減できる(例:GPT-2 では 50,257 * 768 = 38.6M パラメータ)。トークンの入力埋め込みと Logit スコアリングとの間にある意味的対称性を説明せよ。

演習と予測記録

1. 重み共有は入出力空間にどのような対称性を仮定しているか?
2. V=32,000, d=4096 のとき何個のパラメータが削減されるか?
3. 重み共有が低頻度語の表現の乖離を防ぐ理由は何か?

独立習得基準

パラメータ削減数とセマンティクス対称性の理由を正確に説明できること。

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

linking MIT

nanoGPT: model.py architecture implementation

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

該当セクション・タイムスタンプ: model.py: class GPT __init__ weight tying (transformer.wte.weight = lm_head.weight)

検証バージョンと日付: 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)
GPT-2 TensorFlow source (OpenAI) ↗ compared_alternative [Modified MIT License]

Original legacy TensorFlow 1.x code; nanoGPT provides clean modern PyTorch equivalent.

Llama 2 / 3 PyTorch source (Meta) ↗ compared_alternative [Llama 2 Community License]

Includes advanced production features (RoPE, GQA), but nanoGPT's simplicity is ideal for the core Days 46-48 assembly.

linking MIT

The Annotated Transformer

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

該当セクション・タイムスタンプ: Embeddings and Softmax (class Generator)

検証バージョンと日付: Updated PyTorch 2.0 edition (Verified: 2026-10-30)

選定理由:

Clear reference implementation of Transformer modules, residual connections, and layer normalization.

ソース前提知識:

PyTorch tensor operations and attention theory.

安全フォールバック(オフライン回復性):

All block assembly and normalization logic are provided in runnable local Python snippets.

比較対象候補リソースと選定評価 (2)
HuggingFace Transformers library source ↗ compared_alternative [Apache 2.0]

Industrial scale with hundreds of model variations; Annotated Transformer is pedagogically focused on the core architecture.

PyTorch nn.Transformer documentation ↗ compared_alternative [Modified BSD]

Standard library implementation, but internal C++ bindings obscure the first-principles data flow.

04 120 分

Transformerアーキテクチャ総合習熟度試験

完全自立閉本テスト

前提条件

活動01-03の完了。

学習目標

閉本総合テスト:2トークンの極小系列が1層Transformerを通過する全工程を手計算で再現する。

手順

極小語彙 V=4、d_model=2、n_heads=1、N_layers=1 の設定で、入力系列 [0, 2] に対する埋め込み、因果的自己注意機構、残差加算、LayerNorm、および最終的な Logits を手計算せよ。

演習と予測記録

1. 端から端への推論を5つの基本等式で要約せよ。
2. 因果デコードにおいてトークン0がトークン2を参照できない理由を述べよ。
3. 正解が [2, 1] のときの交差エントロピー損失式を示せ。

独立習得基準

全工程の数式を閉本で自力再現し、因果制約と損失計算を論理的に解説できること。

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

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 Model Architecture (3.1-3.5)

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

High-level visual intuition, but Vaswani et al. provides the primary mathematical specifications needed for implementation.

Annotated Transformer (Harvard NLP) ↗ compared_selected_for_day46_48 [MIT]

Co-primary: PyTorch line-by-line implementation alongside the paper equations.

linking MIT

nanoGPT: model.py architecture implementation

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

該当セクション・タイムスタンプ: model.py: class GPT forward(idx, targets): wte + wpe, Blocks, ln_f, lm_head

検証バージョンと日付: 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)
GPT-2 TensorFlow source (OpenAI) ↗ compared_alternative [Modified MIT License]

Original legacy TensorFlow 1.x code; nanoGPT provides clean modern PyTorch equivalent.

Llama 2 / 3 PyTorch source (Meta) ↗ compared_alternative [Llama 2 Community License]

Includes advanced production features (RoPE, GQA), but nanoGPT's simplicity is ideal for the core Days 46-48 assembly.