计算图:局部导数与反向累加
只有在记录预测和观察证据之后才允许求助。
前提条件
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.