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 | 原始出處連結 ↗

對應章節 / 時間戳: 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 分鐘

局部導數推導與基礎算子微分表

獨立掌握檢查

前置要求

一元微積分基礎(冪函數、指數、對數、雙曲正切導數)。

學習目標

手算推導所有核心算子 (+, -, *, /, pow, exp, log, relu, tanh) 的局部偏導數,並設計 `_backward` 閉包。

操作指引

對每一個運算節點 $z = f(x, y)$,節點需要封裝 _backward() 閉包,將上游傳入梯度 (out.grad) 乘以局部導數 $\frac{\partial z}{\partial x}$。查閱 projects/autograd/docs/guide.zh-Hant.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 | 原始出處連結 ↗

對應章節 / 時間戳: 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 | 原始出處連結 ↗

對應章節 / 時間戳: 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 輔助的條件下,驗證獨立推導純量計算圖與鏈式法則反向傳播的能力。

操作指引

請在無大型語言模型、無自動代數計算器的獨立條件下完成下列推導。在實作草稿區寫下推導步驟與結果。

自主實作與記錄

對於損失函數 $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 | 原始出處連結 ↗

    對應章節 / 時間戳: 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 | 原始出處連結 ↗

    對應章節 / 時間戳: 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.