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$ 分步應用鏈式法則,求輸出誤差項與三個輸出層參數梯度。

操作指引

先推符號,再代數值。N1 的輸出是線性單元 $o=w_{o1}a_1+w_{o2}a_2+b_o$,不是 sigmoid;損失沒有 $1/2$ 係數。

1. $\delta_o=\partial L/\partial o=2(o-y)$。
2. 局部輸入導數為 $\partial o/\partial a_1=w_{o1}$、$\partial o/\partial a_2=w_{o2}$。
3. 局部參數導數為 $a_1$、$a_2$、$1$,因此參數梯度是上游 $\delta_o$ 乘局部導數。
4. 使用第34日 $a_1=0.8741$、$a_2=-0.9757$、$o=3.1255$、$y=1.0$ 預測三個輸出梯度,再看答案。

自主實作與記錄

1. 推導 $\delta_o$。2. 算出兩個輸出權重與偏置的梯度。3. 解釋線性單元偏置的局部導數為何是1。4. 指出傳到 $a_1$ 的梯度還需通過哪個激活導數才能到達 $z_1$。

獨立掌握標準

不看答案重現全部三個輸出梯度(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 | 原始出處連結 ↗

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

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

隱藏層反向傳播:手算完成全部九個參數梯度

獨立掌握檢查

前置要求

活動35-01的輸出誤差項。

學習目標

把誤差通過 tanh 隱藏層傳播,完成其餘六個參數梯度,與 `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。每個隱藏權重梯度是隱藏誤差項乘對應輸入,偏置梯度是誤差項自身。完成手算後運行同一引擎命令覈驗全部九項:

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) 給出九項梯度表;隱藏權重必須展示“上游×激活導數×輸入”三因子,並標記引擎確認的每項。

獨立掌握標準

九項與引擎誤差均小於 $10^{-3}$;隱藏權重三因子完整,w12和w22符號正確。

權威來源資源與權利證明

linking MIT

CS231n: Backpropagation and Intuitions

作者 / 機構: Andrej Karpathy / Stanford University | 原始出處連結 ↗

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

對應章節 / 時間戳: 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的九梯度推導。

學習目標

通過飽和測量與十層鏈的界,解釋梯度消失;閉卷提取第31—34日的四個關鍵事實。

操作指引

測量A:運行下面的命令,記錄 $\tanh'(4)$ 與 $\tanh'(8)$。每層飽和激活都會乘入 $1-\tanh^2(z)$。

測量B:十層 tanh 鏈每層權重0.5,每層梯度因子 $|w\tanh'(z_i)|\le0.5$,最早層的傳入梯度因子上界是 $0.5^{10}$。

閉卷提取:為什麼 _backward 用 +=;逆序拓撲傳播何時允許一個節點反傳;$f=(a+b)a$ 在 $(a,b)=(3,4)$ 時對 a 的導數;N1 的四個前向值。記錄查閱過的知識點供間隔複習。

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. 記錄兩個飽和導數並解釋 $|z|=8$ 的影響。2. 計算 $0.5^{10}$ 並解釋十層衰減。3. 閉卷寫出四個提取事實,誠實標記需要查閱的部分。

獨立掌握標準

兩個測量數量級、十層上界、四個提取事實均正確;未掌握部分進入複習計劃。

權威來源資源與權利證明

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

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

對應章節 / 時間戳: 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 的全部九個梯度,關聯向量—雅可比積,並獨立完成客觀測驗。

操作指引

不使用 AI 或筆記:對 N1 輸入 $(1.0, 1.0)$ 重算前向和全部九個梯度($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$ 開始完成九項。2. 寫出輸出誤差、隱藏誤差、偏置梯度、權重梯度四條 delta 公式。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.

為什麼到達共享節點的梯度必須累加來自每條下游路徑的貢獻?

作答應證與歷史記錄

    獨立掌握標準

    新輸入的全部九項在 $10^{-3}$ 內匹配引擎,正確寫出四條公式,獨立測驗100%。

    權威來源資源與權利證明

    linking CC BY-NC 3.0 noncommercial

    Neural Networks and Deep Learning: How the backpropagation algorithm works

    作者 / 機構: Michael Nielsen | 原始出處連結 ↗

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

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