35 / 90

第35日:手計算による誤差逆伝播と勾配流

連鎖律を用いて 2-2-1 ニューラルネットワーク N1 の全パラメータ勾配を導出し、スカラー自動微分エンジンの結果と一致させ、飽和と層の深さが勾配に与える影響を測定し、第31-34日を間隔反復で定着させる。

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

手計算による誤差逆伝播:出力層誤差項とパラメータ勾配

Agent支援エンジニアリングチェック

前提条件

第34日のN1順伝播表:$o=3.1255$、$L=4.5179$。

学習目標

N1の線形出力と二乗損失 $L=(o - y)^2$ に連鎖律を適用し、出力誤差項と3つの出力層勾配を求める。

手順

先に記号で導出し、その後で数値を代入する。出力は $o=w_{o1}a_1+w_{o2}a_2+b_o$ という線形単位で、損失に $1/2$ の係数はない。

1. $\delta_o=\partial L/\partial o=2(o-y)$。
2. 局所入力微分は $w_{o1}$ と $w_{o2}$。
3. 局所パラメータ微分は $a_1$、$a_2$、$1$。各勾配は上流の $\delta_o$ と局所微分の積になる。
4. $a_1=0.8741$、$a_2=-0.9757$、$o=3.1255$、$y=1.0$ を使い、答えを見る前に予測する。

演習と予測記録

1. $\delta_o$ を導出する。2. 出力重み2個とバイアスの勾配を求める。3. バイアスの局所微分が1となる理由を述べる。4. $a_1$ への勾配が $z_1$ に届く前に通る活性化微分を答える。

独立習得基準

答えを見ずに3勾配(3.7157、-4.1480、4.2511)を $10^{-3}$ 以内で再現し、上流×局所の構造を説明する。

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

linking CC BY-NC 3.0 noncommercial

Neural Networks and Deep Learning: How the backpropagation algorithm works

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

該当セクション・タイムスタンプ: The four fundamental equations of backpropagation

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

選定理由:

Clear four-equation derivation of backpropagation with scalar and matrix notation.

ソース前提知識:

Matrix multiplication and the multivariable chain rule from Days 19-24.

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

All four backprop equations and derivations are self-contained within the lesson text.

比較対象候補リソースと選定評価 (2)
Goodfellow et al. Deep Learning (Chapter 6) ↗ compared_alternative [All Rights Reserved (free to view online)]

Comprehensive graduate reference, but heavier tensor notation than needed for first MLP derivations.

Modular computational graph view; used as secondary source on Days 35-36.

linking MIT

CS231n: Backpropagation and Intuitions

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

該当セクション・タイムスタンプ: Patterns in backward flow

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

選定理由:

Modular local-gradient interpretation (gate operations: add, multiply, max) and gradient flow intuition.

ソース前提知識:

Multivariable calculus and basic Python.

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

Local gate definitions and circuit flow diagrams in the lesson provide full coverage.

比較対象候補リソースと選定評価 (2)

Raw circuit view; CS231n notes are more structured with explicit gate derivations.

Fast.ai Practical Deep Learning for Coders ↗ compared_alternative [GPL-3.0]

Top-down approach; CS231n bottom-up modular view matches our first-principles curriculum.

02 120 分

隠れ層の誤差逆伝播:手計算で全9個のパラメータ勾配を完成させる

独立習熟度チェック

前提条件

活動35-01の出力誤差項。

学習目標

tanh隠れ層に誤差を伝え、残り6勾配を求めて全9項を `loss.backward()` と照合する。

手順

独立に $\delta_{z_1}=(\delta_o w_{o1})(1-a_1^2)$、$\delta_{z_2}=(\delta_o w_{o2})(1-a_2^2)$ を計算する。$w_{o1}=1.0$、$w_{o2}=-2.0$、局所微分は約0.2360と0.0479。隠れ重みの勾配は誤差項×対応する入力、バイアス勾配は誤差項自身。手計算後、次の同一コマンドで9項を検証する。

python3 - <<'EOF'
import sys; sys.path.insert(0, '.')
from projects.autograd.engine import Value
x1, x2 = Value(2.0), Value(-1.0)
W = [Value(v) for v in (0.5,-0.25,0.1, -0.5,1.0,-0.2, 1.0,-2.0,0.3)]
a1 = (x1*W[0] + x2*W[1] + W[2]).tanh()
a2 = (x1*W[3] + x2*W[4] + W[5]).tanh()
loss = (a1*W[6] + a2*W[7] + W[8] - 1.0)**2
loss.backward()
print([round(w.grad, 4) for w in W])
EOF

演習と予測記録

(w11,w12,b1,w21,w22,b2,w_o1,w_o2,b_o)の9勾配表を作る。隠れ重みには上流×活性化微分×入力の3因子を示し、エンジンで確認した項を記す。

独立習得基準

全9項がエンジンと $10^{-3}$ 以内で一致し、隠れ重みの3因子とw12、w22の符号が正しい。

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

linking MIT

CS231n: Backpropagation and Intuitions

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

該当セクション・タイムスタンプ: Patterns in backward flow

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

選定理由:

Modular local-gradient interpretation (gate operations: add, multiply, max) and gradient flow intuition.

ソース前提知識:

Multivariable calculus and basic Python.

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

Local gate definitions and circuit flow diagrams in the lesson provide full coverage.

比較対象候補リソースと選定評価 (2)

Raw circuit view; CS231n notes are more structured with explicit gate derivations.

Fast.ai Practical Deep Learning for Coders ↗ compared_alternative [GPL-3.0]

Top-down approach; CS231n bottom-up modular view matches our first-principles curriculum.

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.

03 120 分

勾配の流れ、飽和現象、勾配消失の直感と間隔反復想起

Agent支援エンジニアリングチェック

前提条件

活動35-02の9勾配導出。

学習目標

飽和測定と10層の上限から勾配消失を説明し、第31~34日の4事実を閉本で想起する。

手順

測定A:下記コマンドで $\tanh'(4)$ と $\tanh'(8)$ を測る。各飽和層は勾配に $1-\tanh^2(z)$ を掛ける。

測定B:重み0.5のtanh10層では各因子 $|w\tanh'(z_i)|\le0.5$ なので最初の層への入力勾配因子は $0.5^{10}$ 以下。

閉本想起:_backward の += が必要な理由、逆位相順でノードが反伝播できる時点、$f=(a+b)a$ の $(a,b)=(3,4)$ でのa微分、N1の4つの順伝播値。調べ直した項目を復習計画に記録する。

python3 - <<'EOF'
import sys; sys.path.insert(0, '.')
from projects.autograd.engine import Value
for z0 in (4.0, 8.0):
    z = Value(z0); a = z.tanh(); a.backward()
    print(z0, round(a.data, 6), z.grad)
EOF

演習と予測記録

1. 飽和微分2つと $|z|=8$ の意味を記す。2. $0.5^{10}$ を計算し10層の減衰を解釈する。3. 4つの事実を閉本で書き、参照した項目は正直に記録する。

独立習得基準

測定2つの桁、10層上限、4つの想起事実が正しく、不足項目を復習に回す。

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

linking 3Blue1Brown copyright (all rights reserved); site FAQ permits only short clips under stated conditions No video, audio, transcript or frame redistribution permitted

What is backpropagation really doing? (Chapter 3, Deep Learning)

著者・提供機関: Grant Sanderson (3Blue1Brown) | オリジナル出所URL ↗

該当セクション・タイムスタンプ: The Intuition for Backpropagation

検証バージョンと日付: Online video and article (Verified: 2026-10-30)

選定理由:

Visual and geometric intuition for how nudging weights impacts the final cost function.

ソース前提知識:

Basic derivative intuition from Days 16-18.

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

The lesson derives the network gradient equations step by step without requiring video playback.

比較対象候補リソースと選定評価 (2)
StatQuest Neural Networks Part 3 ↗ compared_alternative [Proprietary]

Step-by-step arithmetic without calculus notation; 3Blue1Brown provides the calculus-visual link needed.

Khan Academy Multivariable Chain Rule ↗ compared_alternative [CC BY-NC-SA 3.0]

Pure math focus; 3Blue1Brown contextualizes the chain rule specifically for neural network graphs.

linking CC BY-NC 3.0 noncommercial

Neural Networks and Deep Learning: How the backpropagation algorithm works

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

該当セクション・タイムスタンプ: The vanishing gradient problem

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

選定理由:

Clear four-equation derivation of backpropagation with scalar and matrix notation.

ソース前提知識:

Matrix multiplication and the multivariable chain rule from Days 19-24.

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

All four backprop equations and derivations are self-contained within the lesson text.

比較対象候補リソースと選定評価 (2)
Goodfellow et al. Deep Learning (Chapter 6) ↗ compared_alternative [All Rights Reserved (free to view online)]

Comprehensive graduate reference, but heavier tensor notation than needed for first MLP derivations.

Modular computational graph view; used as secondary source on Days 35-36.

04 120 分

定着復習:独立した誤差逆伝播の導出とブロック想起

独立習熟度チェック

前提条件

活動35-01~35-03。

学習目標

新しい入力でN1の全9勾配を閉本で導出し、ベクトル・ヤコビ積と結び、独立クイズを完了する。

手順

AIやノートを使わず、N1の入力 $(1.0, 1.0)$ で順伝播と全9勾配を再計算する($a_1=0.3364$、$a_2=0.2913$、$o=0.0538$)。VJP $\mathbf v^T J$ を書き、スカラーの場合の上流微分×局所微分を対応付ける。独立モードのクイズを受け、誤答ごとに回復リンクを選ぶ。

演習と予測記録

1. $\delta_o=2(o-1.0)=-1.8925$ から全9項を求める。2. 出力誤差、隠れ誤差、バイアス勾配、重み勾配の4式を書く。3. 独立モードで3問100%を達成する。

客観的知識チェック:誤差逆伝播の導出と勾配流

理解度を確認するために次の質問に答えてください。提出前に申告する支援条件を選択してください。

申告する支援条件

注意:AI 不使用は正直な申告条件であり、監視を主張するものではありません。

1.

隠れ層の漸化式 $\delta^l = (W^{l+1})^T \delta^{l+1} \odot \sigma'(z^l)$ において、演算子 $\odot$ は何を表しますか?

2.

エンジンで測定すると、$\tanh'(8) = 1 - \tanh^2(8)$ はおおよそいくらですか?

3.

共有ノードに到達した勾配が、下流のすべての経路からの寄与を累積しなければならないのはなぜですか?

受験履歴と証拠

    独立習得基準

    新入力の全9勾配が $10^{-3}$ 以内で一致し、4式を正しく書き、独立クイズ100%を達成する。

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

    linking CC BY-NC 3.0 noncommercial

    Neural Networks and Deep Learning: How the backpropagation algorithm works

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

    該当セクション・タイムスタンプ: Proof of the four fundamental equations

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

    選定理由:

    Clear four-equation derivation of backpropagation with scalar and matrix notation.

    ソース前提知識:

    Matrix multiplication and the multivariable chain rule from Days 19-24.

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

    All four backprop equations and derivations are self-contained within the lesson text.

    比較対象候補リソースと選定評価 (2)
    Goodfellow et al. Deep Learning (Chapter 6) ↗ compared_alternative [All Rights Reserved (free to view online)]

    Comprehensive graduate reference, but heavier tensor notation than needed for first MLP derivations.

    Modular computational graph view; used as secondary source on Days 35-36.

    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.