計算圖:局部導數與反向累加
只有在記錄預測與觀察證據之後才允許求助。
先修條件
Day 19-20 的連鎖律與梯度練習;如有步驟不熟,先開啟當日恢復連結。
學習目標
把純量表達式分解為計算圖,計算每個節點的前向值,再逐條邊乘以局部連鎖因子反向傳播導數。
操作說明
取 L = (a*b + c)^2,a = 2,b = 3,c = 1。前向:n1 = a*b = 6,n2 = n1 + c = 7,L = n2^2 = 49。反向:dL/dn2 = 2*n2 = 14;dL/dn1 = 14 * ∂n2/∂n1 = 14 * 1 = 14;dL/dc = 14 * ∂n2/∂c = 14;dL/da = 14 * ∂n1/∂a = 14 * b = 42;dL/db = 14 * ∂n1/∂b = 14 * a = 28。每一步都是 Day 19-20 連鎖律作用在一條局部邊上。在程式碼中擾動 a 之前先預測 dL/da。
練習與預測記錄
畫出 (2, 3, 1) 處 L = (a*b + c)^2 的計算圖,填入前向值 6、7、49,再手算所有輸入梯度。用中央有限差分驗證 dL/da、dL/db、dL/dc 並記錄一致性。
反向模式:n2 處的伴隨值沿進入它的前向邊 n1->n2 與 c->n2 反向傳播,到達 n1 和 c。故 dL/dn2 = dL/dn1 = dL/dc = 14;施加局部因子得輸入梯度 dL/da = 42、dL/db = 28、dL/dc = 14。
前向:n1 = 6,n2 = 7,L = 49。反向:dL/dn2 = 14;dL/dn1 = 14,dL/dc = 14;dL/da = 14*3 = 42;dL/db = 14*2 = 28。有限差分((L(2+h,3,1) - L(2-h,3,1)) / 2h,h = 1e-5,b、c 同理)回傳約 42.0000、28.0000、14.0000——這正是 micrograd 式引擎對每個輸入 .grad 報告的數字。
無幫助下重現前向值 6、7、49 與反向值 42、28、14,說出每條邊上的局部導數,且你的有限差分執行與每個手算梯度一致。
權威來源資源與出處
micrograd: A tiny scalar-valued autograd engine
相關章節/時間戳: micrograd/engine.py: class Value (_backward closures and backward() topological sort); README 'Example usage'
驗證版本與日期: 2022 (verified 2026-10-30)
The scalar computational graph in this lesson is exactly the micrograd engine walkthrough; it previews the autograd project without importing any material.
Chain rule and partial derivatives (Study Days 19-20).
The full forward and backward pass is derived locally with numeric verification; no video is required.
候選來源比較與選擇分析 (2)
Normative production documentation, but micrograd's pure Python implementation provides far superior initial clarity.
High visual quality, pairs cleanly as optional supplementary visual reference.
OpenStax Calculus Volume 3
相關章節/時間戳: Section 4.5: tree-diagram rule used to justify backward accumulation
驗證版本與日期: OpenStax Calculus Volume 3 web edition (verified 2026-10-30)
Provides the formal chain rule statement that the graph walkthrough applies node by node.
Scalar chain rule (day-19) and partial derivatives (day-20).
The derivation uses only local rules already proven on Days 19-20.
候選來源比較與選擇分析 (2)
Selected as the lecture-depth companion for partial derivatives; OpenStax Volume 3 remains the written primary reference.
Strong visual coverage of gradients, but the OpenStax text better matches the lesson's theorem-plus-hand-check structure.