从感知机到可微 MLP:层、权重与激活函数
Agent 辅助工程检查
前置要求
第 33 天自动微分掌握检查,以及已验证的 Value `backward()` 语义。
学习目标
区分历史感知机(阶跃激活)与可微神经元 $z = \sum_i w_i x_i + b$、$a = \sigma(z)$;解释为何必须堆叠带非线性激活的层才能表达 XOR 等函数;并陈述本日使用的参考 2-2-1 网络的精确结构。
操作指引
先阅读下面的概念分解,再先预测后计算。
感知机计算 $z = \sum_i w_i x_i + b$,当 $z > 0$ 时输出 $1$,否则输出 $0$。其阶跃激活使输入-输出映射分段恒定:梯度几乎处处为 $0$,在拐点处无定义,因此基于梯度的训练无法改进它。可微神经元把阶跃换成 $\tanh$ 等光滑激活。
多层感知机 (MLP) 将全连接层堆叠起来:第 $l$ 层的每个神经元都接收第 $l-1$ 层的全部激活。单个线性单元只能画出一条直线决策边界,无法表示 XOR;两层以上加非线性激活则可以。
本日参考网络 N1:输入 $x=(2.0, -1.0)$;隐藏神经元 $h_1$:权重 $(0.5, -0.25)$,偏置 $0.1$,激活 $\tanh$;隐藏神经元 $h_2$:权重 $(-0.5, 1.0)$,偏置 $-0.2$,激活 $\tanh$;输出神经元 $o$:权重 $(1.0, -2.0)$,偏置 $0.3$,线性(无激活);损失 $L = (o - y)^2$,目标 $y = 1.0$。
自主演练与记录
1. 在任何计算之前,先预测 $z_1$ 与 $z_2$ 的符号,并判断是否有隐藏神经元接近饱和。
2. 数出 N1 的可训练参数总数,并拆分为权重与偏置。
3. 用一句话说明为何阶跃激活的感知机无法用梯度下降训练。
4. 预测 $o$ 会高于还是低于目标 $y = 1.0$。
先算加权和:$z_1 = 0.5(2.0) + (-0.25)(-1.0) + 0.1$,$z_2 = -0.5(2.0) + 1.0(-1.0) + (-0.2)$。参数计数要把每个权重和每个偏置分别列出。判断饱和时记住:$|z| \gtrsim 4$ 时 $\tanh$ 已饱和。
参考解答:
1. $z_1 = 1.0 + 0.25 + 0.1 = 1.35$(为正,中段区间,未饱和);$z_2 = -1.0 - 1.0 - 0.2 = -2.2$(为负,也未深度饱和)。
2. 共九个可训练参数:六个权重 ($(w_{11}, w_{12}), (w_{21}, w_{22}), (w_{o1}, w_{o2})$) 与三个偏置 ($b_1, b_2, b_o$)。
3. 阶跃函数几乎处处平坦,导数在有定义处为 $0$、在 $z = 0$ 处无定义;权重收不到任何有效梯度。
4. $o = 1.0 \cdot \tanh(1.35) + (-2.0) \cdot \tanh(-2.2) + 0.3 \approx 0.874 + 1.951 + 0.3 = 3.126$,明显高于 $y = 1.0$。
学习者能写出 $z = \sum w_i x_i + b$ 与 $a = \sigma(z)$ 的公式,正确数出 N1 的九个参数,并用自己的话说明基于梯度的学习为何要求可微性。
权威源资源与权利证明
Neural Networks and Deep Learning: How the backpropagation algorithm works
对应章节 / 时间戳: Sections: Perceptrons, Sigmoid neurons, The architecture of neural networks
验证版本与日期: 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)
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.
micrograd: A tiny scalar-valued autograd engine
对应章节 / 时间戳: 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)
Production scale, but too large and complex for tracing by hand in introductory lessons.
Vectorized autograd; micrograd's pure scalar model is pedagogical for hand backprop.