33 / 90

第 33 天:梯度缺陷診斷分析與無 AI 獨立掌握標準評估

診斷真實自動微分實現缺陷、評估多次反向傳播與向量輸出等邊界情況、理解引擎擴展性邊界,並完成無 AI 獨立掌握標準評估。

數學與演算法基礎 480 分鐘(8 小時專注學習)
01 120 分鐘

缺陷案例實戰:精準定位梯度覆蓋 Bug

Agent 輔助工程檢查

前置要求

第 32 天梯度累加機制與數值梯度檢驗。

學習目標

透過構造極簡復現用例 (MRE),在數值差分比對下精準定位出因覆蓋賦值導致隱蔽梯度錯誤的具體節點與程式碼缺陷。

操作指引

使用 CLI 執行梯度缺陷診斷排查指令:

python3 projects/autograd/cli.py diagnose --bug-type overwrite-accumulation

觀察診斷程式如何在表達式 $f = (a + b) \cdot a$ 中隔離出節點 a,展示其解析梯度 $7.0$ 與數值差分梯度 $10.0$ 之間高達 $30\%$ 的相對誤差。

自主實作與記錄

1. 寫出能夠可靠觸發梯度覆蓋缺陷的極簡復現程式碼 (MRE)。
2. 解釋為何類似 $z = x \cdot y$ 這樣的單分支測試用例即使在存在該缺陷時依然能夠通過 gradcheck。
3. 在 _backward() 中指出修復該缺陷的關鍵程式碼修改。

獨立掌握標準

學習者能夠構造出區分累加與覆蓋實現的極簡測試用例,並準確解釋其數學原理。

權威來源資源與權利證明

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)

入選理由:

Direct demonstration of how simple variable reuse creates branching DAGs requiring accumulation.

來源前置條件:

Autograd backward pass and multivariable calculus

安全回退方案(離線彈性):

Local diagnostic harness in projects/autograd/diagnostics.py.

對比候選資源與選型分析 (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 分鐘

自動微分邊界情況:多次反向傳播與向量輸出

獨立掌握檢查

前置要求

活動 33-01 與拓撲逆序執行機制。

學習目標

理解連續呼叫 `backward()` 時的梯度累加語義,解釋為何優化器每輪迭代必須顯式清零梯度 (`zero_grad()`),並掌握多輸出圖的向量雅可比積 (VJP)。

操作指引

執行多輸出預設的前向與反向指令:

python3 projects/autograd/cli.py forward-backward --preset multi-output

觀察 backward_multiple([y1, y2], grad_outputs=[1.0, 1.0]) 如何同時為多個終端輸出節點提供初始種子梯度並完成反向傳播。

自主實作與記錄

1. 如果在未呼叫 grad = 0.0 清零的情況下,對同一個計算圖連續呼叫兩次 out.backward(),輸入葉節點的梯度數值會變成多少?
2. 為什麼主流深度學習框架(如 PyTorch)預設採取梯度累加策略,而不是每次自動清零?
3. 寫出多輸出向量向輸入反向求導時的向量-雅可比積 (VJP) 表達式。

獨立掌握標準

學習者能夠清晰解釋訓練循環中清零梯度的必要性,以及梯度累加在大模型分佈式訓練中的工程價值。

權威來源資源與權利證明

linking Modified BSD

PyTorch Documentation: Autograd Mechanics

作者 / 機構: PyTorch Contributors | 原始出處連結 ↗

對應章節 / 時間戳: How autograd encodes the history

驗證版本與日期: PyTorch 2.5 (Verified: 2026-10-30)

入選理由:

Clarifies semantics of gradient zeroing before repeated backward passes and vector-Jacobian products.

來源前置條件:

Computational graph basics

安全回退方案(離線彈性):

Stand-alone CLI support in projects/autograd/cli.py.

對比候選資源與選型分析 (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.

03 120 分鐘

來源歸屬、開源合規與引擎擴展性邊界

Agent 輔助工程檢查

前置要求

活動 33-01 與 33-02。

學習目標

嚴格遵守開源許可規範,清晰區分教學型純量自動微分引擎與工業級張量編譯計算系統(PyTorch、JAX)的系統邊界。

操作指引

閱讀 projects/autograd/docs/guide.zh-Hant.md 與 Stanford CS336 作業規範。注意 micrograd 採用 MIT 許可證,要求保留版權與許可聲明;講義課程資源具有獨立的版權保護。

自主實作與記錄

1. 為什麼純 Python 純量自動微分引擎不能用於預訓練一個 70 億參數 (7B) 的語言模型?
2. 對比 Python 堆物件開銷與編譯型 C++/CUDA 連續張量記憶體緩衝區的差異。
3. 說明在引用與複用 Andrej Karpathy 的 micrograd 程式碼時應遵守的開源合規要求。

獨立掌握標準

學習者能夠清晰闡明純量教學引擎與張量編譯框架在底層資料結構、記憶體佈局與計算效率上的本質區別。

權威來源資源與權利證明

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.

來源前置條件:

Software licensing awareness

安全回退方案(離線彈性):

Local documentation in projects/autograd/docs/.

對比候選資源與選型分析 (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 分鐘

獨立掌握專案標準評估與無 AI 驗證

獨立掌握檢查

前置要求

活動 33-01 至 33-03。

學習目標

對照三項核心獨立掌握標準,完成自動微分學習專案的綜合自評與無 AI 客觀驗證。

操作指引

請在無大型語言模型、無自動補全的條件下完成下方的診斷測驗,並對照三大掌握標準評估自己的專案實現。

自主實作與記錄

回顧獨立掌握三大評估維度:
1. 白板推導:手寫多元微積分鏈式法則,證明分支 DAG 節點為何必須使用 +=。
2. 白板實現:在空白文字檔案中獨立實現 Value、算子多載、拓撲排序與 backward()。
3. 差分排查:編寫有限差分 gradcheck 函數,並成功定位並修復故意植入的梯度覆蓋 Bug。
在實作區確認並記錄你的獨立達標情況。

客觀知識測驗:缺陷診斷、多次反向傳播與掌握標準

請回答以下診斷題目檢驗掌握情況。提交前請誠實聲明你的輔助條件。

聲明輔助條件

說明:無 AI 輔助屬於誠實聲明條件,非平台監控或強制檢測手段。

1.

在模型訓練循環中,如果呼叫 loss.backward() 前未執行 optimizer.zero_grad() 清零梯度,會引發什麼後果?

2.

為什麼像 micrograd 這樣的純 Python 純量自動微分引擎根本無法用於預訓練大型語言模型?

3.

在本專案的無 AI 獨立掌握標準 (Independent Mastery Check) 中,哪種情況代表真正的獨立掌握?

作答應證與歷史記錄

    獨立掌握標準

    在無外部 AI 輔助的情況下,客觀測試獲得 100% 得分,並滿足全部三項獨立掌握評估標準。

    權威來源資源與權利證明

    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 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.