從感知機到可微 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.