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.