31 / 90

第 31 日:順方向計算グラフ、局所導関数と逆伝播連鎖律

自動微分の基礎を習得する:スカラー有向非巡回グラフ(DAG)の構築、基本演算子の局所導関数の導出、および逆伝播の手計算トレース。

数学とアルゴリズム基礎 480 分(8 時間の集中学習)
01 120 分

計算グラフの構築とスカラー順方向評価

Agent支援エンジニアリング確認

前提条件

第30日の数学基礎、Pythonオブジェクト指向の基礎、参照モデルの理解。

学習目標

スカラー算術式がどのように `Value` ノードからなる有向非巡回グラフ(DAG)にマッピングされ、順方向計算が評価されるかを理解する。

手順

自動微分では、計算グラフは数学演算を内部ノード、オペランドを入力エッジとして表現します。次のコマンドを実行してグラフ構造を確認してください:

python3 projects/autograd/cli.py graph --preset neuron --format ascii

$x_1 w_1 + x_2 w_2 + b$ が活性化関数 $o = \tanh(n)$ に流れ込む様子と、順方向の計算値を確認します。

演習と予測記録

$f(x, y) = (x \cdot y + 2)^2$ を $x = 1.5, y = -2.0$ において評価します:
1. 中間ノードを定義します: $u = x \cdot y$, $v = u + 2$, $f = v^2$。
2. $u, v, f$ の数値を計算します。
3. コードを実行する前に、予測値をエディタに記録してください。

独立習得基準

スカラー算術演算を親依存ノード (`_prev`) とスカラー値 (`data`) を持つ DAG ノードとして正確に分解できること。

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

linking MIT

micrograd: A tiny scalar-valued autograd engine

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

該当セクション・タイムスタンプ: micrograd/engine.py: class Value (_backward closures and backward() topological sort); README 'Example usage'

検証バージョンと日付: commit a9a9f94 (Verified: 2026-10-30)

選定理由:

Clear, minimal scalar DAG implementation demonstrating fundamental autograd data structures.

ソース前提知識:

Basic Python and calculus fundamentals

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

Local engine implementation in projects/autograd/engine.py and offline documentation.

比較対象候補リソースと選定評価 (2)
PyTorch autograd C++ engine ↗ compared_alternative [Modified BSD]

Production scale, but too large and complex for tracing by hand in introductory lessons.

Autograd (HIPS) ↗ compared_alternative [MIT]

Vectorized autograd; micrograd's pure scalar model is pedagogical for hand backprop.

02 120 分

局所導関数の導出と基本演算子の微分規則

独立習得確認

前提条件

一変数微積分の基礎(冪乗、指数、対数、tanhの微分)。

学習目標

主要演算子 (+, -, *, /, pow, exp, log, relu, tanh) の局所偏導関数を手計算で導出し、`_backward` クロージャを設計する。

手順

演算ノード $z = f(x, y)$ ごとに、上流からの勾配 (out.grad) に局所導関数 $\frac{\partial z}{\partial x}$ を掛ける _backward() クロージャを実装します。projects/autograd/docs/guide.ja.md の演算子微分一覧表を参照してください。

演習と予測記録

以下の局所導関数を手計算で導出してください:
1. 乗算 $z = x \cdot y$: $\frac{\partial z}{\partial x}$ と $\frac{\partial z}{\partial y}$。
2. 冪乗 $z = x^n$($n$ は定数): $\frac{\partial z}{\partial x}$。
3. 双曲線正接 $z = \tanh(x)$: $z$ 自身を用いて $\frac{\partial z}{\partial x}$ を表す。

独立習得基準

表の参照やAI支援なしで、主要演算子の局所導関数および勾配更新式を独立して記述できること。

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

linking SIAM Copyright / Academic Linking Only No text extraction or distribution permitted

Evaluating Derivatives: Principles and Techniques of Algorithmic Differentiation

著者・提供機関: Andreas Griewank, Andrea Walther | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Chapter 3: Reverse Mode and Computational Graphs

検証バージョンと日付: 2nd Edition, SIAM 2008 (Verified: 2026-10-30)

選定理由:

Detailed mathematical reference for algorithmic differentiation theory and complexity.

ソース前提知識:

Multivariate calculus and linear algebra

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

Self-contained mathematical derivations in activity instructions and offline guide.

比較対象候補リソースと選定評価 (2)
micrograd: A tiny scalar-valued autograd engine ↗ compared_alternative [MIT License]

Executable scalar reference implementation the learner later rebuilds; the textbook supplies the formal reverse-mode theory.

Broad survey taxonomy of the field; the textbook was selected for its rigorous treatment of reverse-mode complexity.

03 120 分

逆伝播連鎖律の直感と単一ニューロンの逆方向手計算トレース

Agent支援エンジニアリング確認

前提条件

アクティビティ 31-01 および 31-02 の完了。

学習目標

多変数連鎖律が損失から入力重みへと勾配をどのように伝播するかを理解し、単一ニューロンの順方向・逆方向を手計算で追跡する。

手順

ニューロンのプリセットに対して順方向および逆方向コマンドを実行します:

python3 projects/autograd/cli.py forward-backward --preset neuron

逆伝播が $o.\text{grad} = 1.0$ から開始され、$n = x_1 w_1 + x_2 w_2 + b$ を経由して $\frac{\partial o}{\partial w_1} = 1.0$ および $\frac{\partial o}{\partial x_1} = -1.5$ に到達することを確認します。

演習と予測記録

$o = \tanh(n)$、$n = 0.8814$($o \approx 0.7071$)とします:
1. $\frac{\partial o}{\partial n} = 1 - o^2$ を計算します。
2. $n = x_1 w_1 + x_2 w_2 + b$($x_1 = 2.0, w_1 = -3.0$)において、$\frac{\partial o}{\partial x_1}$ と $\frac{\partial o}{\partial w_1}$ を計算します。
3. 手計算の数値を CLI の出力結果と比較します。

独立習得基準

階層的な計算ノードを通じて連鎖律の積を漏れなく適用し、中間スケール係数を見落とさずに追跡できること。

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

linking MIT

CS336 Assignment 1: Basics (Spring 2025 tagged release)

著者・提供機関: Stanford University CS336 Staff | オリジナル出所URL ↗

該当セクション・タイムスタンプ: README setup; tests/adapters.py; tests/test_optimizer.py; tests/test_nn_utils.py; tests/test_serialization.py

検証バージョンと日付: spring2025 tag; README, tests and MIT LICENSE checked 2026-11-01

選定理由:

Lead assignment-driven source for pretraining component practice and tests. nanoGPT remains a compact historical code-reading companion, not the current recommended training curriculum.

ソース前提知識:

Python systems programming and multivariable calculus

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

Stand-alone projects/autograd engine.

比較対象候補リソースと選定評価 (2)
Karpathy nanoGPT ↗ compared_alternative [MIT]

Simpler code layout, useful complementary reference for minimal PyTorch Transformer.

Hugging Face Transformers Course ↗ compared_alternative [Apache-2.0]

High-level API focus rather than building from raw tensor fundamentals.

04 120 分

独立習得確認:スカラー計算グラフの手計算導出と逆伝播クイズ

独立習得確認

前提条件

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

学習目標

AI支援のない宣言された環境下で、スカラー計算グラフと連鎖律の逆伝播を独立して導出できる能力を検証する。

手順

LLMや数式処理ツールを使用せず、独立した環境で以下の導出を行ってください。演習エリアに途中式と結果を記録してください。

演習と予測記録

損失関数 $L = (x \cdot y + 2)^2$ を $x = 1.5, y = -2.0$ において評価します:
解析的偏導関数 $\frac{\partial L}{\partial x}$ および $\frac{\partial L}{\partial y}$ を連鎖律を用いて厳密に計算してください。

客観的理解度チェック:計算グラフと逆伝播連鎖律

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

申告補助条件

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

1.

逆伝播自動微分において、DAG を逆方向に巡回する前に、根損失ノード(Loss)の勾配 loss.grad は初期値として何に設定する必要がありますか?

2.

演算ノード $z = x \cdot y$ において、現在の z.grad が $5.0$、入力が $x = 2.0, y = -3.0$ である場合、入力 $x$ に伝播される勾配はいくらですか?

3.

ニューラルネットワークの学習において、逆伝播型自動微分が順方向微分に対して漸近計算量で優れている本質的な理由は何ですか?

解答根拠と履歴

    独立習得基準

    中間変数の定義と連鎖律の積を示し、$\frac{\partial L}{\partial x} = 4.0$ および $\frac{\partial L}{\partial y} = -3.0$ を正しく導出できること。

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

    linking MIT

    micrograd: A tiny scalar-valued autograd engine

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

    該当セクション・タイムスタンプ: micrograd/engine.py: class Value (_backward closures and backward() topological sort); README 'Example usage'

    検証バージョンと日付: Git commit master (Verified: 2026-10-30)

    選定理由:

    Minimal, readable implementation of scalar autograd and MLP layers directly matched to our project.

    ソース前提知識:

    Python object-oriented programming and operator overloading from Day 31.

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

    The local projects/autograd engine is runnable offline and matches micrograd semantics.

    比較対象候補リソースと選定評価 (2)
    PyTorch autograd C++ engine ↗ compared_alternative [Modified BSD]

    Production scale, but too large and complex for tracing by hand in introductory lessons.

    Autograd (HIPS) ↗ compared_alternative [MIT]

    Vectorized autograd; micrograd's pure scalar model is pedagogical for hand backprop.

    linking Modified BSD

    PyTorch Documentation: Autograd Mechanics

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

    該当セクション・タイムスタンプ: How autograd encodes the history

    検証バージョンと日付: PyTorch 2.5 Documentation (Verified: 2026-10-30)

    選定理由:

    Official reference for computational graph construction, backward pass execution, and in-place mutation pitfalls.

    ソース前提知識:

    Basic understanding of computational graphs and tensors.

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

    The training loop mechanics and zero_grad requirements are illustrated with runnable Python code.

    比較対象候補リソースと選定評価 (2)
    TensorFlow tf.GradientTape Documentation ↗ compared_alternative [Apache 2.0]

    Alternative tape-based execution; PyTorch dynamic graph semantics are standard in modern LLM research.

    JAX autodiff cookbook ↗ compared_alternative [Apache 2.0]

    Functional transformations (grad, vjp); PyTorch object-based graph is closer to the beginner micrograd model.