# 圖解 CNN：卷積神經網路

> CNN 的一句話版本：<strong>用很多小 filter 逐層「找特徵 → 壓縮 → 組合」，最後把影像轉成分類結果。</strong><br />本筆記從一張 5×5 的黑白小圖開始，把卷積、ReLU、池化、全連接一路算到「狗 0.93」。<br />所有數字都是本機用 NumPy 算出來的（Python 3.15 / NumPy 2.5.4），原始輸出在附錄 G。<br />本頁沒有互動測驗（附錄 E／F 從缺）；延伸閱讀與站內其他 CNN 筆記看附錄 C。

<!-- CNNI-FIGURE:pipeline -->
<figure style="margin:22px 0;padding:14px 12px;background:#0d1117;border:1px solid #222b36;border-radius:12px;text-align:center;-webkit-print-color-adjust:exact;print-color-adjust:exact">
<svg style="width:100%;height:auto;display:block" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 250" role="img" aria-label="一個影像分類 CNN 的資料流：輸入 28×28×1 → Conv+ReLU 16 → Pool → Conv+ReLU 32 → Pool → Flatten 1568 維 → Dense+Softmax → 10 個類別機率，前段重複 N 次抽特徵，後段做分類"><defs><marker id="ar" markerWidth="8" markerHeight="8" refX="6" refY="3" orient="auto"><path d="M0,0 L6,3 L0,6 z" fill="#8b949e"/></marker></defs><rect x="0.75" y="0.75" width="998.5" height="248.5" rx="16" fill="#0d1117" stroke="#222b36" stroke-width="1.5"/><text x="40" y="44" font-size="20" fill="#e6edf3" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">一個影像分類 CNN 的資料流</text><text x="40" y="70" font-size="13" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">前段抽特徵，後段做判斷；中間那兩個 Pool 讓面積一路砍半</text><rect x="44" y="104" width="96" height="64" rx="8" fill="#12181f" stroke="#4ecdc4" stroke-width="1.6" opacity="0.9"/><text x="92.0" y="134" font-size="12" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">輸入影像</text><text x="92.0" y="153" font-size="10" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">28×28×1</text><line x1="143" y1="136.0" x2="161" y2="136.0" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="166" y="104" width="96" height="64" rx="8" fill="#12181f" stroke="#4ecdc4" stroke-width="1.6" opacity="0.9"/><text x="214.0" y="134" font-size="12" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">Conv+ReLU</text><text x="214.0" y="153" font-size="10" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">3×3 ×16</text><line x1="265" y1="136.0" x2="283" y2="136.0" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="288" y="104" width="96" height="64" rx="8" fill="#12181f" stroke="#58a6ff" stroke-width="1.6" opacity="0.9"/><text x="336.0" y="134" font-size="12" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">Pool 2×2</text><text x="336.0" y="153" font-size="10" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">14×14×16</text><line x1="387" y1="136.0" x2="405" y2="136.0" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="410" y="104" width="96" height="64" rx="8" fill="#12181f" stroke="#4ecdc4" stroke-width="1.6" opacity="0.9"/><text x="458.0" y="134" font-size="12" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">Conv+ReLU</text><text x="458.0" y="153" font-size="10" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">3×3 ×32</text><line x1="509" y1="136.0" x2="527" y2="136.0" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="532" y="104" width="96" height="64" rx="8" fill="#12181f" stroke="#58a6ff" stroke-width="1.6" opacity="0.9"/><text x="580.0" y="134" font-size="12" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">Pool 2×2</text><text x="580.0" y="153" font-size="10" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">7×7×32</text><line x1="631" y1="136.0" x2="649" y2="136.0" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="654" y="104" width="96" height="64" rx="8" fill="#12181f" stroke="#e0a458" stroke-width="1.6" opacity="0.9"/><text x="702.0" y="134" font-size="12" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">Flatten</text><text x="702.0" y="153" font-size="10" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1,568 維</text><line x1="753" y1="136.0" x2="771" y2="136.0" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="776" y="104" width="96" height="64" rx="8" fill="#12181f" stroke="#e0a458" stroke-width="1.6" opacity="0.9"/><text x="824.0" y="134" font-size="12" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">Dense</text><text x="824.0" y="153" font-size="10" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">128 + softmax</text><line x1="875" y1="136.0" x2="893" y2="136.0" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="898" y="104" width="96" height="64" rx="8" fill="#12181f" stroke="#ff7b72" stroke-width="1.6" opacity="0.9"/><text x="946.0" y="134" font-size="12" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">類別機率</text><text x="946.0" y="153" font-size="10" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">10 個數字</text><line x1="166" y1="96" x2="628" y2="96" stroke="#30363d" stroke-width="1"/><text x="397.0" y="88" font-size="11" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">特徵抽取：重複 N 次（卷積 → ReLU → 池化）</text><line x1="654" y1="96" x2="994" y2="96" stroke="#30363d" stroke-width="1"/><text x="824.0" y="88" font-size="11" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">分類頭</text><text x="40" y="212" font-size="12" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">同一組 filter 滑過整張圖（參數共享）→ 參數量與影像大小無關；池化只降維，沒有可學的參數。</text></svg>
<figcaption style="font-size:13.5px;color:#9aa7b4;margin-top:10px;line-height:1.65;text-align:left">整個分類網路：前段（卷積 → ReLU → 池化）重複 N 次抽特徵，後段攤平後接全連接層與 softmax；可訓練的參數只在 Conv 與 Dense。</figcaption>
</figure>
<!-- /CNNI-FIGURE:pipeline -->

## 1. 三個元件，一句話一個

一個影像分類 CNN 只有四種層，其中兩種有參數、兩種沒有：

| 層 | 做的事 | 學到的東西 | 有可訓練參數嗎 |
|---|---|---|---|
| 卷積層 | 用小 filter 掃過整張圖 | 邊緣、紋理、部件、物件 | 有（filter 的權重） |
| 激活函數 | 把負值砍成 0 | 讓「多層」真的比一層強 | 沒有 |
| 池化層 | 每 2×2 取一個代表值 | 位置不重要的那一面 | 沒有 |
| 全連接層 | 把特徵向量映到類別分數 | 類別之間的界線 | 有 |

最上面那張圖在做的事，就是重複「卷積 → 激活 → 池化」，把影像的面積一路砍小、通道一路加寬，
最後攤平成一條向量交給全連接層。

## 2. 影像就是一堆數字

一張 28×28 的灰階手寫數字，在電腦裡就是 784 個 0–255 的整數；彩色圖再多一個維度：
紅、綠、藍各一張 28×28，所以是 `28×28×3`。這件事沒有玄機，CNN 的「看」從頭到尾都在做加減乘除。

真正關鍵的是**相鄰像素有意義**：一個筆畫的轉角一定牽動旁邊幾格。CNN 的所有設計，
都是為了不要一開始就把這個關係弄丟。

### 2.1 為什麼不直接把圖攤平

把 28×28 攤成 784 維、接一個全連接層，數學上可行，代價是參數爆炸。用兩個實測數字比：

| 比較 | 全連接 | 卷積 | 差距 |
|---|---|---|---|
| 1024 維 → 120 個神經元 | `(1024 + 1) × 120 = 123,000` | `5 × 5 = 25`（一層 1 通道） | **4,920 倍** |
| LeNet-5 第一層同樣輸出 4,704 個單元 | `1024 × 4,704 = 4,816,896` | `(25 + 1) × 6 = 156` | **30,878 倍** |

兩件事同時發生：

- **攤平會丟掉空間結構**：784 維向量裡，第 1 個數字和第 2 個數字是鄰居這件事沒有被編碼進去。
- **全連接的參數隨像素數線性爆炸**：影像從 28×28 換成 224×224，參數量就多 64 倍；卷積不會。

## 3. 卷積層：一個 3×3 的放大鏡

<!-- CNNI-FIGURE:conv -->
<figure style="margin:22px 0;padding:14px 12px;background:#0d1117;border:1px solid #222b36;border-radius:12px;text-align:center;-webkit-print-color-adjust:exact;print-color-adjust:exact">
<svg style="width:100%;height:auto;display:block" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 560" role="img" aria-label="卷積運算：3×3 核心在 5×5 影像上滑動，左上角 3×3 區塊與核心逐元素相乘再相加得到 4，整張輸出為 3×3 特徵圖"><defs><marker id="ar" markerWidth="8" markerHeight="8" refX="6" refY="3" orient="auto"><path d="M0,0 L6,3 L0,6 z" fill="#8b949e"/></marker></defs><rect x="0.75" y="0.75" width="998.5" height="558.5" rx="16" fill="#0d1117" stroke="#222b36" stroke-width="1.5"/><text x="40" y="44" font-size="20" fill="#e6edf3" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">卷積：逐元素相乘再相加</text><text x="40" y="72" font-size="13" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">3×3 核心滑過 5×5 影像，每一步在特徵圖上留下一個數字</text><text x="60" y="126" font-size="13" fill="#e6edf3" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">輸入影像 5×5</text><text x="356" y="126" font-size="13" fill="#e6edf3" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">3×3 核心</text><text x="532" y="126" font-size="13" fill="#e6edf3" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">特徵圖 3×3</text><rect x="60" y="140" width="46" height="46" rx="4" fill="#e0a45826" stroke="#e0a458" stroke-width="1.6"/><text x="83.0" y="168.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="106" y="140" width="46" height="46" rx="4" fill="#e0a45826" stroke="#e0a458" stroke-width="1.6"/><text x="129.0" y="168.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="152" y="140" width="46" height="46" rx="4" fill="#e0a45826" stroke="#e0a458" stroke-width="1.6"/><text x="175.0" y="168.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="198" y="140" width="46" height="46" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="221.0" y="168.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="244" y="140" width="46" height="46" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="267.0" y="168.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="60" y="186" width="46" height="46" rx="4" fill="#e0a45826" stroke="#e0a458" stroke-width="1.6"/><text x="83.0" y="214.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="106" y="186" width="46" height="46" rx="4" fill="#e0a45826" stroke="#e0a458" stroke-width="1.6"/><text x="129.0" y="214.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="152" y="186" width="46" height="46" rx="4" fill="#e0a45826" stroke="#e0a458" stroke-width="1.6"/><text x="175.0" y="214.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="198" y="186" width="46" height="46" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="221.0" y="214.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="244" y="186" width="46" height="46" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="267.0" y="214.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="60" y="232" width="46" height="46" rx="4" fill="#e0a45826" stroke="#e0a458" stroke-width="1.6"/><text x="83.0" y="260.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="106" y="232" width="46" height="46" rx="4" fill="#e0a45826" stroke="#e0a458" stroke-width="1.6"/><text x="129.0" y="260.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="152" y="232" width="46" height="46" rx="4" fill="#e0a45826" stroke="#e0a458" stroke-width="1.6"/><text x="175.0" y="260.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="198" y="232" width="46" height="46" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="221.0" y="260.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="244" y="232" width="46" height="46" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="267.0" y="260.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="60" y="278" width="46" height="46" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="83.0" y="306.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="106" y="278" width="46" height="46" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="129.0" y="306.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="152" y="278" width="46" height="46" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="175.0" y="306.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="198" y="278" width="46" height="46" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="221.0" y="306.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="244" y="278" width="46" height="46" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="267.0" y="306.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="60" y="324" width="46" height="46" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="83.0" y="352.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="106" y="324" width="46" height="46" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="129.0" y="352.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="152" y="324" width="46" height="46" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="175.0" y="352.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="198" y="324" width="46" height="46" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="221.0" y="352.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="244" y="324" width="46" height="46" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="267.0" y="352.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="60" y="140" width="138" height="138" rx="6" fill="none" stroke="#e0a458" stroke-width="2.4"/><rect x="356" y="140" width="40" height="40" rx="4" fill="#58a6ff26" stroke="#58a6ff" stroke-width="1.6"/><text x="376.0" y="164.86" font-size="13.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="396" y="140" width="40" height="40" rx="4" fill="#58a6ff26" stroke="#58a6ff" stroke-width="1.6"/><text x="416.0" y="164.86" font-size="13.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="436" y="140" width="40" height="40" rx="4" fill="#58a6ff26" stroke="#58a6ff" stroke-width="1.6"/><text x="456.0" y="164.86" font-size="13.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="356" y="180" width="40" height="40" rx="4" fill="#58a6ff26" stroke="#58a6ff" stroke-width="1.6"/><text x="376.0" y="204.86" font-size="13.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="396" y="180" width="40" height="40" rx="4" fill="#58a6ff26" stroke="#58a6ff" stroke-width="1.6"/><text x="416.0" y="204.86" font-size="13.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="436" y="180" width="40" height="40" rx="4" fill="#58a6ff26" stroke="#58a6ff" stroke-width="1.6"/><text x="456.0" y="204.86" font-size="13.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="356" y="220" width="40" height="40" rx="4" fill="#58a6ff26" stroke="#58a6ff" stroke-width="1.6"/><text x="376.0" y="244.86" font-size="13.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="396" y="220" width="40" height="40" rx="4" fill="#58a6ff26" stroke="#58a6ff" stroke-width="1.6"/><text x="416.0" y="244.86" font-size="13.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="436" y="220" width="40" height="40" rx="4" fill="#58a6ff26" stroke="#58a6ff" stroke-width="1.6"/><text x="456.0" y="244.86" font-size="13.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="532" y="140" width="44" height="44" rx="4" fill="#e0a45826" stroke="#e0a458" stroke-width="1.6"/><text x="554.0" y="166.86" font-size="13.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">4</text><rect x="576" y="140" width="44" height="44" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="598.0" y="166.86" font-size="13.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">3</text><rect x="620" y="140" width="44" height="44" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="642.0" y="166.86" font-size="13.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">4</text><rect x="532" y="184" width="44" height="44" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="554.0" y="210.86" font-size="13.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">2</text><rect x="576" y="184" width="44" height="44" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="598.0" y="210.86" font-size="13.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">4</text><rect x="620" y="184" width="44" height="44" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="642.0" y="210.86" font-size="13.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">3</text><rect x="532" y="228" width="44" height="44" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="554.0" y="254.86" font-size="13.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">2</text><rect x="576" y="228" width="44" height="44" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="598.0" y="254.86" font-size="13.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">3</text><rect x="620" y="228" width="44" height="44" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="642.0" y="254.86" font-size="13.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">4</text><line x1="294" y1="200" x2="346" y2="200" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><line x1="482" y1="206" x2="524" y2="206" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><text x="700" y="180" font-size="11.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">每一步都用同一組 9 個權重</text><text x="700" y="202" font-size="11.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">→ 參數量與影像大小無關</text><rect x="48" y="396" width="904" height="96" rx="12" fill="#1c2230" stroke="#58a6ff55" stroke-width="1.4"/><text x="68" y="424" font-size="13.5" fill="#e6edf3" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">特徵圖左上角那一格怎麼算出來？</text><text x="68" y="452" font-size="13" fill="#e0a458" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1×1 + 0×1 + 1×1  ＋  0×0 + 1×1 + 0×1  ＋  1×0 + 0×0 + 1×1  =  4</text><text x="68" y="476" font-size="12" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">九組乘積全部相加 = 一格；換到下一個位置，同樣九個權重再算一次。</text><text x="48" y="528" font-size="12" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">輸出尺寸（不補邊、步幅 1）：(5 − 3) / 1 + 1 = 3 → 3×3</text></svg>
<figcaption style="font-size:13.5px;color:#9aa7b4;margin-top:10px;line-height:1.65;text-align:left">一個 3×3 核心在 5×5 影像上滑動。左上角那一格是九組乘積相加得到的 4；同一組 9 個權重會被每個位置重用。</figcaption>
</figure>
<!-- /CNNI-FIGURE:conv -->

### 3.1 一格是怎麼算出來的

卷積的定義只有三步：**逐元素相乘再相加**。

1. 在影像上取一塊與核心同樣大的區塊（這裡是 3×3）。
2. 把區塊的 9 個數字與核心的 9 個權重**一對一相乘**。
3. 把 9 個乘積**全部相加**，得到特徵圖上的一格。

用本機實測的例子走一遍，核心是

```text
1 0 1
0 1 0
1 0 1
```

影像左上角的 3×3 是

```text
1 1 1
0 1 1
0 0 1
```

逐元素相乘再相加：

```text
(1×1 + 0×1 + 1×1) + (0×0 + 1×1 + 0×1) + (1×0 + 0×0 + 1×1) = 4
```

這一格就是特徵圖左上角的 **4**。把核心往右移一格、再算一次得到 **3**；滑完整張 5×5 影像，
特徵圖就是實測的這三列：

```text
[[4, 3, 4],
 [2, 4, 3],
 [2, 3, 4]]
```

> **一個常被漏掉的細節**：深度學習講的「卷積」，實作上是**互相關（cross-correlation）**——
> 核心不翻轉。差別只在核心是否上下左右顛倒，而權重本來就是學出來的，翻不翻轉對模型能力沒有影響。
> 這也是為什麼 PyTorch 的函式叫 `conv2d`，行為卻是不翻轉的版本。

### 3.2 參數共享：同一組 9 個權重，每個位置都重用

核心滑到哪裡都用**同一組**權重，這件事叫參數共享（parameter sharing），它帶來兩個結果：

- **參數量與影像大小無關**：5×5 的圖和 5000×5000 的圖，這一層都是 9 個權重。
- **同一個特徵在任何位置都會被偵測到**：貓耳朵出現在左上角或右下角，同一組權重都認得。

代價是「位置資訊變模糊」——特徵圖只記錄「這裡有這種反應」，不太記錄「這在第 3 列第 5 行」。
後面的池化層會再把這件事推得更遠。

### 3.3 輸出尺寸只看四個數字

<!-- CNNI-FIGURE:hyper -->
<figure style="margin:22px 0;padding:14px 12px;background:#0d1117;border:1px solid #222b36;border-radius:12px;text-align:center;-webkit-print-color-adjust:exact;print-color-adjust:exact">
<svg style="width:100%;height:auto;display:block" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 340" role="img" aria-label="三種卷積設定對輸出尺寸的影響：不補邊 5×5 得 3×3、補一圈邊得 5×5、步幅 2 得 2×2"><defs><marker id="ar" markerWidth="8" markerHeight="8" refX="6" refY="3" orient="auto"><path d="M0,0 L6,3 L0,6 z" fill="#8b949e"/></marker></defs><rect x="0.75" y="0.75" width="998.5" height="338.5" rx="16" fill="#0d1117" stroke="#222b36" stroke-width="1.5"/><text x="40" y="44" font-size="20" fill="#e6edf3" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">同樣的輸入，換 padding 與 stride 就換輸出尺寸</text><text x="40" y="72" font-size="13" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">唯一要記的公式：O = (N + 2P − F) / S + 1</text><rect x="30" y="96" width="300" height="176" rx="8" fill="#12181f" stroke="#4ecdc488" stroke-width="1.5" opacity="0.9"/><text x="46" y="124" font-size="12.5" fill="#4ecdc4" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">不補邊（valid）</text><text x="46" y="148" font-size="11.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">(5 − 3) / 1 + 1 = 3</text><rect x="46" y="170" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="58" y="170" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="70" y="170" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="82" y="170" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="94" y="170" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="46" y="182" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="58" y="182" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="70" y="182" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="82" y="182" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="94" y="182" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="46" y="194" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="58" y="194" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="70" y="194" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="82" y="194" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="94" y="194" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="46" y="206" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="58" y="206" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="70" y="206" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="82" y="206" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="94" y="206" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="46" y="218" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="58" y="218" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="70" y="218" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="82" y="218" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="94" y="218" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><line x1="112" y1="200" x2="138" y2="200" stroke="#4ecdc4" stroke-width="1.5" marker-end="url(#ar)"/><rect x="142" y="182.0" width="12" height="12" rx="3" fill="none" stroke="#4ecdc4" stroke-width="1.1" opacity="1"/><rect x="154" y="182.0" width="12" height="12" rx="3" fill="none" stroke="#4ecdc4" stroke-width="1.1" opacity="1"/><rect x="166" y="182.0" width="12" height="12" rx="3" fill="none" stroke="#4ecdc4" stroke-width="1.1" opacity="1"/><rect x="142" y="194.0" width="12" height="12" rx="3" fill="none" stroke="#4ecdc4" stroke-width="1.1" opacity="1"/><rect x="154" y="194.0" width="12" height="12" rx="3" fill="none" stroke="#4ecdc4" stroke-width="1.1" opacity="1"/><rect x="166" y="194.0" width="12" height="12" rx="3" fill="none" stroke="#4ecdc4" stroke-width="1.1" opacity="1"/><rect x="142" y="206.0" width="12" height="12" rx="3" fill="none" stroke="#4ecdc4" stroke-width="1.1" opacity="1"/><rect x="154" y="206.0" width="12" height="12" rx="3" fill="none" stroke="#4ecdc4" stroke-width="1.1" opacity="1"/><rect x="166" y="206.0" width="12" height="12" rx="3" fill="none" stroke="#4ecdc4" stroke-width="1.1" opacity="1"/><text x="212" y="205" font-size="11.5" fill="#e6edf3" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">輸出 3×3</text><text x="46" y="252" font-size="10.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">邊角像素只被掃到 1 次</text><rect x="350" y="96" width="300" height="176" rx="8" fill="#12181f" stroke="#58a6ff88" stroke-width="1.5" opacity="0.9"/><text x="366" y="124" font-size="12.5" fill="#58a6ff" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">補一圈邊（same）</text><text x="366" y="148" font-size="11.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">(5 + 2 − 3) / 1 + 1 = 5</text><rect x="366" y="170" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="378" y="170" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="390" y="170" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="402" y="170" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="414" y="170" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="366" y="182" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="378" y="182" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="390" y="182" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="402" y="182" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="414" y="182" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="366" y="194" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="378" y="194" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="390" y="194" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="402" y="194" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="414" y="194" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="366" y="206" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="378" y="206" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="390" y="206" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="402" y="206" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="414" y="206" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="366" y="218" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="378" y="218" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="390" y="218" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="402" y="218" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="414" y="218" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><line x1="432" y1="200" x2="458" y2="200" stroke="#58a6ff" stroke-width="1.5" marker-end="url(#ar)"/><rect x="462" y="170.0" width="12" height="12" rx="3" fill="none" stroke="#58a6ff" stroke-width="1.1" opacity="1"/><rect x="474" y="170.0" width="12" height="12" rx="3" fill="none" stroke="#58a6ff" stroke-width="1.1" opacity="1"/><rect x="486" y="170.0" width="12" height="12" rx="3" fill="none" stroke="#58a6ff" stroke-width="1.1" opacity="1"/><rect x="498" y="170.0" width="12" height="12" rx="3" fill="none" stroke="#58a6ff" stroke-width="1.1" opacity="1"/><rect x="510" y="170.0" width="12" height="12" rx="3" fill="none" stroke="#58a6ff" stroke-width="1.1" opacity="1"/><rect x="462" y="182.0" width="12" height="12" rx="3" fill="none" stroke="#58a6ff" stroke-width="1.1" opacity="1"/><rect x="474" y="182.0" width="12" height="12" rx="3" fill="none" stroke="#58a6ff" stroke-width="1.1" opacity="1"/><rect x="486" y="182.0" width="12" height="12" rx="3" fill="none" stroke="#58a6ff" stroke-width="1.1" opacity="1"/><rect x="498" y="182.0" width="12" height="12" rx="3" fill="none" stroke="#58a6ff" stroke-width="1.1" opacity="1"/><rect x="510" y="182.0" width="12" height="12" rx="3" fill="none" stroke="#58a6ff" stroke-width="1.1" opacity="1"/><rect x="462" y="194.0" width="12" height="12" rx="3" fill="none" stroke="#58a6ff" stroke-width="1.1" opacity="1"/><rect x="474" y="194.0" width="12" height="12" rx="3" fill="none" stroke="#58a6ff" stroke-width="1.1" opacity="1"/><rect x="486" y="194.0" width="12" height="12" rx="3" fill="none" stroke="#58a6ff" stroke-width="1.1" opacity="1"/><rect x="498" y="194.0" width="12" height="12" rx="3" fill="none" stroke="#58a6ff" stroke-width="1.1" opacity="1"/><rect x="510" y="194.0" width="12" height="12" rx="3" fill="none" stroke="#58a6ff" stroke-width="1.1" opacity="1"/><rect x="462" y="206.0" width="12" height="12" rx="3" fill="none" stroke="#58a6ff" stroke-width="1.1" opacity="1"/><rect x="474" y="206.0" width="12" height="12" rx="3" fill="none" stroke="#58a6ff" stroke-width="1.1" opacity="1"/><rect x="486" y="206.0" width="12" height="12" rx="3" fill="none" stroke="#58a6ff" stroke-width="1.1" opacity="1"/><rect x="498" y="206.0" width="12" height="12" rx="3" fill="none" stroke="#58a6ff" stroke-width="1.1" opacity="1"/><rect x="510" y="206.0" width="12" height="12" rx="3" fill="none" stroke="#58a6ff" stroke-width="1.1" opacity="1"/><rect x="462" y="218.0" width="12" height="12" rx="3" fill="none" stroke="#58a6ff" stroke-width="1.1" opacity="1"/><rect x="474" y="218.0" width="12" height="12" rx="3" fill="none" stroke="#58a6ff" stroke-width="1.1" opacity="1"/><rect x="486" y="218.0" width="12" height="12" rx="3" fill="none" stroke="#58a6ff" stroke-width="1.1" opacity="1"/><rect x="498" y="218.0" width="12" height="12" rx="3" fill="none" stroke="#58a6ff" stroke-width="1.1" opacity="1"/><rect x="510" y="218.0" width="12" height="12" rx="3" fill="none" stroke="#58a6ff" stroke-width="1.1" opacity="1"/><text x="532" y="205" font-size="11.5" fill="#e6edf3" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">輸出 5×5</text><text x="366" y="252" font-size="10.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">邊界用 0 補，輸出不縮小</text><rect x="670" y="96" width="300" height="176" rx="8" fill="#12181f" stroke="#e0a45888" stroke-width="1.5" opacity="0.9"/><text x="686" y="124" font-size="12.5" fill="#e0a458" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">步幅 2（stride 2）</text><text x="686" y="148" font-size="11.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">(5 − 3) / 2 + 1 = 2</text><rect x="686" y="170" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="698" y="170" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="710" y="170" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="722" y="170" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="734" y="170" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="686" y="182" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="698" y="182" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="710" y="182" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="722" y="182" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="734" y="182" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="686" y="194" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="698" y="194" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="710" y="194" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="722" y="194" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="734" y="194" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="686" y="206" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="698" y="206" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="710" y="206" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="722" y="206" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="734" y="206" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="686" y="218" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="698" y="218" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="710" y="218" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="722" y="218" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><rect x="734" y="218" width="12" height="12" rx="3" fill="none" stroke="#30363d" stroke-width="0.9" opacity="0.75"/><line x1="752" y1="200" x2="778" y2="200" stroke="#e0a458" stroke-width="1.5" marker-end="url(#ar)"/><rect x="782" y="188.0" width="12" height="12" rx="3" fill="none" stroke="#e0a458" stroke-width="1.1" opacity="1"/><rect x="794" y="188.0" width="12" height="12" rx="3" fill="none" stroke="#e0a458" stroke-width="1.1" opacity="1"/><rect x="782" y="200.0" width="12" height="12" rx="3" fill="none" stroke="#e0a458" stroke-width="1.1" opacity="1"/><rect x="794" y="200.0" width="12" height="12" rx="3" fill="none" stroke="#e0a458" stroke-width="1.1" opacity="1"/><text x="852" y="205" font-size="11.5" fill="#e6edf3" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">輸出 2×2</text><text x="686" y="252" font-size="10.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">一次跳 2 格，資訊少一半</text><text x="30" y="312" font-size="12.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">N 輸入邊長、F 核邊長、P 補幾圈、S 步幅；除不盡就往下取整（floor）。</text></svg>
<figcaption style="font-size:13.5px;color:#9aa7b4;margin-top:10px;line-height:1.65;text-align:left">同樣的 5×5 輸入、同樣的 3×3 核心，只改 padding 與 stride，輸出大小就完全不同——這就是 `O = (N + 2P − F) / S + 1` 的全部內容。</figcaption>
</figure>
<!-- /CNNI-FIGURE:hyper -->

| 符號 | 意思 |
|---|---|
| `N` | 輸入邊長（正方形） |
| `F` | 核心邊長 |
| `P` | 邊緣補幾圈（padding） |
| `S` | 步幅（stride，每次滑幾格） |

```text
O = (N + 2P − F) / S + 1      （除不盡就往下取整）
```

實測三種常見設定（5×5 輸入、3×3 核心）：

| 設定 | 算式 | 輸出 |
|---|---|---|
| 不補邊、步幅 1 | (5 − 3) / 1 + 1 | 3×3 |
| 補一圈邊、步幅 1 | (5 + 2 − 3) / 1 + 1 | 5×5（尺寸不變，所以叫 same） |
| 不補邊、步幅 2 | (5 − 3) / 2 + 1 | 2×2 |

同一個公式套在 MNIST 上：`28×28` 用 3×3 核心補一圈邊 → 還是 `28×28`；
接一個 2×2、步幅 2 的池化 → `14×14`。整條網路的尺寸就是這樣一層一層手算出來的。

## 4. 多個 filter：通道是「不同 filter 的答案」

<!-- CNNI-FIGURE:multi -->
<figure style="margin:22px 0;padding:14px 12px;background:#0d1117;border:1px solid #222b36;border-radius:12px;text-align:center;-webkit-print-color-adjust:exact;print-color-adjust:exact">
<svg style="width:100%;height:auto;display:block" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 440" role="img" aria-label="多個濾波器：同一張 5×5 輸入分別用垂直邊緣、斜線、斑塊三個核心，得到三張 3×3 特徵圖，疊成三通道再送進下一層"><defs><marker id="ar" markerWidth="8" markerHeight="8" refX="6" refY="3" orient="auto"><path d="M0,0 L6,3 L0,6 z" fill="#8b949e"/></marker></defs><rect x="0.75" y="0.75" width="998.5" height="438.5" rx="16" fill="#0d1117" stroke="#222b36" stroke-width="1.5"/><text x="40" y="44" font-size="20" fill="#e6edf3" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">多個 filter：一層產生多張特徵圖</text><text x="40" y="72" font-size="13" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">三個核心各自掃過同一張 5×5 影像，得到三張 3×3 特徵圖（三通道）</text><text x="40" y="136" font-size="12.5" fill="#e6edf3" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">輸入 5×5×1</text><rect x="40" y="150" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="60.0" y="174.68" font-size="13" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="80" y="150" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="100.0" y="174.68" font-size="13" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="120" y="150" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="140.0" y="174.68" font-size="13" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="160" y="150" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="180.0" y="174.68" font-size="13" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="200" y="150" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="220.0" y="174.68" font-size="13" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="40" y="190" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="60.0" y="214.68" font-size="13" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="80" y="190" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="100.0" y="214.68" font-size="13" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="120" y="190" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="140.0" y="214.68" font-size="13" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="160" y="190" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="180.0" y="214.68" font-size="13" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="200" y="190" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="220.0" y="214.68" font-size="13" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="40" y="230" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="60.0" y="254.68" font-size="13" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="80" y="230" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="100.0" y="254.68" font-size="13" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="120" y="230" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="140.0" y="254.68" font-size="13" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="160" y="230" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="180.0" y="254.68" font-size="13" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="200" y="230" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="220.0" y="254.68" font-size="13" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="40" y="270" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="60.0" y="294.68" font-size="13" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="80" y="270" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="100.0" y="294.68" font-size="13" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="120" y="270" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="140.0" y="294.68" font-size="13" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="160" y="270" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="180.0" y="294.68" font-size="13" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="200" y="270" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="220.0" y="294.68" font-size="13" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="40" y="310" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="60.0" y="334.68" font-size="13" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="80" y="310" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="100.0" y="334.68" font-size="13" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="120" y="310" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="140.0" y="334.68" font-size="13" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="160" y="310" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="180.0" y="334.68" font-size="13" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="200" y="310" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="220.0" y="334.68" font-size="13" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><text x="140" y="366" font-size="11" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">同一張影像</text><line x1="268" y1="141" x2="268" y2="349" stroke="#30363d" stroke-width="1.4"/><line x1="240" y1="250" x2="268" y2="250" stroke="#30363d" stroke-width="1.4"/><line x1="268" y1="141" x2="294" y2="141" stroke="#30363d" stroke-width="1.4"/><rect x="300" y="96" width="30" height="30" rx="4" fill="#4ecdc426" stroke="#4ecdc4" stroke-width="1.6"/><text x="315.0" y="114.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="330" y="96" width="30" height="30" rx="4" fill="#4ecdc426" stroke="#4ecdc4" stroke-width="1.6"/><text x="345.0" y="114.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="360" y="96" width="30" height="30" rx="4" fill="#4ecdc426" stroke="#4ecdc4" stroke-width="1.6"/><text x="375.0" y="114.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="300" y="126" width="30" height="30" rx="4" fill="#4ecdc426" stroke="#4ecdc4" stroke-width="1.6"/><text x="315.0" y="144.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="330" y="126" width="30" height="30" rx="4" fill="#4ecdc426" stroke="#4ecdc4" stroke-width="1.6"/><text x="345.0" y="144.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="360" y="126" width="30" height="30" rx="4" fill="#4ecdc426" stroke="#4ecdc4" stroke-width="1.6"/><text x="375.0" y="144.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="300" y="156" width="30" height="30" rx="4" fill="#4ecdc426" stroke="#4ecdc4" stroke-width="1.6"/><text x="315.0" y="174.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="330" y="156" width="30" height="30" rx="4" fill="#4ecdc426" stroke="#4ecdc4" stroke-width="1.6"/><text x="345.0" y="174.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="360" y="156" width="30" height="30" rx="4" fill="#4ecdc426" stroke="#4ecdc4" stroke-width="1.6"/><text x="375.0" y="174.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><line x1="394" y1="141" x2="418" y2="141" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="424" y="96" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="439.0" y="114.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">-2</text><rect x="454" y="96" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="469.0" y="114.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="484" y="96" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="499.0" y="114.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">2</text><rect x="424" y="126" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="439.0" y="144.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">-3</text><rect x="454" y="126" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="469.0" y="144.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">-2</text><rect x="484" y="126" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="499.0" y="144.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">2</text><rect x="424" y="156" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="439.0" y="174.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">-3</text><rect x="454" y="156" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="469.0" y="174.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">-1</text><rect x="484" y="156" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="499.0" y="174.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">2</text><text x="522" y="145" font-size="11.5" fill="#4ecdc4" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">垂直邊緣</text><line x1="576" y1="141" x2="588" y2="141" stroke="#30363d" stroke-width="1.2"/><line x1="268" y1="245" x2="294" y2="245" stroke="#30363d" stroke-width="1.4"/><rect x="300" y="200" width="30" height="30" rx="4" fill="#58a6ff26" stroke="#58a6ff" stroke-width="1.6"/><text x="315.0" y="218.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="330" y="200" width="30" height="30" rx="4" fill="#58a6ff26" stroke="#58a6ff" stroke-width="1.6"/><text x="345.0" y="218.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="360" y="200" width="30" height="30" rx="4" fill="#58a6ff26" stroke="#58a6ff" stroke-width="1.6"/><text x="375.0" y="218.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="300" y="230" width="30" height="30" rx="4" fill="#58a6ff26" stroke="#58a6ff" stroke-width="1.6"/><text x="315.0" y="248.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="330" y="230" width="30" height="30" rx="4" fill="#58a6ff26" stroke="#58a6ff" stroke-width="1.6"/><text x="345.0" y="248.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="360" y="230" width="30" height="30" rx="4" fill="#58a6ff26" stroke="#58a6ff" stroke-width="1.6"/><text x="375.0" y="248.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="300" y="260" width="30" height="30" rx="4" fill="#58a6ff26" stroke="#58a6ff" stroke-width="1.6"/><text x="315.0" y="278.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="330" y="260" width="30" height="30" rx="4" fill="#58a6ff26" stroke="#58a6ff" stroke-width="1.6"/><text x="345.0" y="278.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="360" y="260" width="30" height="30" rx="4" fill="#58a6ff26" stroke="#58a6ff" stroke-width="1.6"/><text x="375.0" y="278.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><line x1="394" y1="245" x2="418" y2="245" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="424" y="200" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="439.0" y="218.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="454" y="200" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="469.0" y="218.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="484" y="200" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="499.0" y="218.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="424" y="230" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="439.0" y="248.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="454" y="230" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="469.0" y="248.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">-1</text><rect x="484" y="230" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="499.0" y="248.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="424" y="260" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="439.0" y="278.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">-2</text><rect x="454" y="260" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="469.0" y="278.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">-1</text><rect x="484" y="260" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="499.0" y="278.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">2</text><text x="522" y="249" font-size="11.5" fill="#58a6ff" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">斜線</text><line x1="576" y1="245" x2="588" y2="245" stroke="#30363d" stroke-width="1.2"/><line x1="268" y1="349" x2="294" y2="349" stroke="#30363d" stroke-width="1.4"/><rect x="300" y="304" width="30" height="30" rx="4" fill="#c084fc26" stroke="#c084fc" stroke-width="1.6"/><text x="315.0" y="322.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="330" y="304" width="30" height="30" rx="4" fill="#c084fc26" stroke="#c084fc" stroke-width="1.6"/><text x="345.0" y="322.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="360" y="304" width="30" height="30" rx="4" fill="#c084fc26" stroke="#c084fc" stroke-width="1.6"/><text x="375.0" y="322.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="300" y="334" width="30" height="30" rx="4" fill="#c084fc26" stroke="#c084fc" stroke-width="1.6"/><text x="315.0" y="352.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="330" y="334" width="30" height="30" rx="4" fill="#c084fc26" stroke="#c084fc" stroke-width="1.6"/><text x="345.0" y="352.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="360" y="334" width="30" height="30" rx="4" fill="#c084fc26" stroke="#c084fc" stroke-width="1.6"/><text x="375.0" y="352.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="300" y="364" width="30" height="30" rx="4" fill="#c084fc26" stroke="#c084fc" stroke-width="1.6"/><text x="315.0" y="382.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="330" y="364" width="30" height="30" rx="4" fill="#c084fc26" stroke="#c084fc" stroke-width="1.6"/><text x="345.0" y="382.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="360" y="364" width="30" height="30" rx="4" fill="#c084fc26" stroke="#c084fc" stroke-width="1.6"/><text x="375.0" y="382.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><line x1="394" y1="349" x2="418" y2="349" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="424" y="304" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="439.0" y="322.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">2</text><rect x="454" y="304" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="469.0" y="322.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="484" y="304" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="499.0" y="322.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">2</text><rect x="424" y="334" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="439.0" y="352.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">-2</text><rect x="454" y="334" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="469.0" y="352.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="484" y="334" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="499.0" y="352.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="424" y="364" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="439.0" y="382.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">-2</text><rect x="454" y="364" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="469.0" y="382.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="484" y="364" width="30" height="30" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="499.0" y="382.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">2</text><text x="522" y="353" font-size="11.5" fill="#c084fc" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">斑塊</text><line x1="576" y1="349" x2="588" y2="349" stroke="#30363d" stroke-width="1.2"/><line x1="588" y1="141" x2="588" y2="349" stroke="#30363d" stroke-width="1.4"/><line x1="588" y1="245" x2="628" y2="245" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><text x="640" y="158" font-size="12" fill="#e6edf3" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">疊成 3 通道</text><rect x="640" y="170" width="150" height="46" rx="6" fill="#12181f" stroke="#30363d" stroke-width="1.2" opacity="0.9"/><text x="652" y="198" font-size="11" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">特徵圖 1</text><rect x="640" y="224" width="150" height="46" rx="6" fill="#12181f" stroke="#30363d" stroke-width="1.2" opacity="0.9"/><text x="652" y="252" font-size="11" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">特徵圖 2</text><rect x="640" y="278" width="150" height="46" rx="6" fill="#12181f" stroke="#30363d" stroke-width="1.2" opacity="0.9"/><text x="652" y="306" font-size="11" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">特徵圖 3</text><text x="40" y="412" font-size="12" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">下一層的核心會同時讀這 3 個通道 → C_in = 3；參數量 (F·F·C_in + 1) · C_out。</text></svg>
<figcaption style="font-size:13.5px;color:#9aa7b4;margin-top:10px;line-height:1.65;text-align:left">一層用三個核心就得到三張特徵圖（三通道）。下一層的核心會同時讀這三個通道，所以「組合」是在通道維度上發生的。</figcaption>
</figure>
<!-- /CNNI-FIGURE:multi -->

一層只用一個 filter，只會學到一種特徵。所以一層通常用幾十到幾百個 filter，每個 filter 各產生一張
特徵圖，這些特徵圖疊起來就是這一層的輸出，深度等於 filter 的數量。

於是「多通道」不是什麼神祕的東西，它只是**同一個位置的答案有好幾份**：

- 輸入是彩色圖 → 3 個通道（R、G、B）。
- 這一層用 64 個 filter → 輸出 64 個通道。
- **下一層的一個 filter，會同時讀上一層的全部 64 個通道**，所以它的權重數量是 `F×F×64`。

參數量的公式因此長這樣：

```text
卷積參數   = (F × F × C_in + 1) × C_out      ← 只跟核大小與通道數有關
全連接參數 = (n_in + 1) × n_out             ← 隨像素數線性爆炸
```

實測：`3×3` 核心、輸入 3 通道、輸出 64 通道 → `(3×3×3 + 1) × 64 = 1,792` 個參數。
同樣「3,136 維 → 10 個輸出」若用全連接做，需要 `(3136 + 1) × 10 = 31,370` 個參數，是前者的 17.5 倍。

## 5. ReLU：沒有它，深層等於一層

<!-- CNNI-FIGURE:relu -->
<figure style="margin:22px 0;padding:14px 12px;background:#0d1117;border:1px solid #222b36;border-radius:12px;text-align:center;-webkit-print-color-adjust:exact;print-color-adjust:exact">
<svg style="width:100%;height:auto;display:block" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 900 340" role="img" aria-label="ReLU 激活函數：輸入負值全部變成 0，正值原樣保留；右側是折線圖 y = max(0, x)"><defs><marker id="ar" markerWidth="8" markerHeight="8" refX="6" refY="3" orient="auto"><path d="M0,0 L6,3 L0,6 z" fill="#8b949e"/></marker></defs><rect x="0.75" y="0.75" width="898.5" height="338.5" rx="16" fill="#0d1117" stroke="#222b36" stroke-width="1.5"/><text x="40" y="44" font-size="20" fill="#e6edf3" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">ReLU：把負的丟掉，正的放行</text><text x="40" y="72" font-size="13" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">沒有這個非線性，疊再多層卷積仍然只等於一層線性變換</text><text x="60" y="108" font-size="11.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">輸入 x</text><text x="206" y="108" font-size="11.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">輸出 max(0, x)</text><text x="130" y="140" font-size="13" fill="#ff7b72" text-anchor="end" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">-3</text><text x="168" y="140" font-size="13" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">→</text><text x="206" y="140" font-size="13" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><text x="130" y="170" font-size="13" fill="#ff7b72" text-anchor="end" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">-1</text><text x="168" y="170" font-size="13" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">→</text><text x="206" y="170" font-size="13" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><text x="130" y="200" font-size="13" fill="#ff7b72" text-anchor="end" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">-0.25</text><text x="168" y="200" font-size="13" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">→</text><text x="206" y="200" font-size="13" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><text x="130" y="230" font-size="13" fill="#4ecdc4" text-anchor="end" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0.5</text><text x="168" y="230" font-size="13" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">→</text><text x="206" y="230" font-size="13" fill="#e6edf3" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0.5</text><text x="130" y="260" font-size="13" fill="#4ecdc4" text-anchor="end" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">2</text><text x="168" y="260" font-size="13" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">→</text><text x="206" y="260" font-size="13" fill="#e6edf3" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">2</text><text x="60" y="312" font-size="11.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">負值一律變 0，正值原樣通過。</text><text x="380" y="112" font-size="12.5" fill="#e6edf3" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">折線圖：y = max(0, x)</text><line x1="370" y1="250" x2="880" y2="250" stroke="#8b949e" stroke-width="1"/><line x1="620" y1="96" x2="620" y2="304" stroke="#8b949e" stroke-width="1"/><polyline points="380,250 620,250 860,98" fill="none" stroke="#4ecdc4" stroke-width="2.6" stroke-linejoin="round"/><text x="380" y="270" font-size="11" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">-4</text><text x="620" y="270" font-size="11" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><text x="860" y="270" font-size="11" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">4</text><text x="608" y="178" font-size="11" fill="#8b949e" text-anchor="end" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">2</text><text x="608" y="102" font-size="11" fill="#8b949e" text-anchor="end" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">4</text><text x="876" y="268" font-size="11.5" fill="#8b949e" text-anchor="end" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">x</text><text x="632" y="92" font-size="11.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">y</text><text x="470" y="226" font-size="11" fill="#ff7b72" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">負值區間全為 0</text><text x="724" y="150" font-size="11" fill="#4ecdc4" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">斜率 1</text></svg>
<figcaption style="font-size:13.5px;color:#9aa7b4;margin-top:10px;line-height:1.65;text-align:left">ReLU 只做一件事：把負的丟掉。沒有它，再多層卷積疊起來仍然等於一層線性變換。</figcaption>
</figure>
<!-- /CNNI-FIGURE:relu -->

卷積本身是**線性**運算：一層卷積接一層卷積，等於一個大矩陣，中間那層白做了。
所以每個卷積後面要接一個非線性函數，最常用的是 ReLU：

```text
ReLU(x) = max(0, x)
```

它只做一件事：負的變 0，正的照原樣。本機實測：

| 輸入 x | −3 | −1 | −0.25 | 0.5 | 2 |
|---|---|---|---|---|---|
| ReLU(x) | 0 | 0 | 0 | 0.5 | 2 |

三個要記住的副作用：

- **梯度也會被砍**：`x < 0` 的地方梯度是 0，那個權重就收不到更新訊號。
- **「沒反應」不等於「死了」**：一顆神經元對某張圖輸出 0 很正常（稀疏是常態）；
  真正的 dead ReLU 是**對所有輸入**都輸出 0，那顆就永遠不會再被訓練。
- **它不是「激活」什麼**：名字裡的 activation 是歷史包袱，它實際做的是篩選。

## 6. 池化層：把 2×2 壓成一格

<!-- CNNI-FIGURE:pool -->
<figure style="margin:22px 0;padding:14px 12px;background:#0d1117;border:1px solid #222b36;border-radius:12px;text-align:center;-webkit-print-color-adjust:exact;print-color-adjust:exact">
<svg style="width:100%;height:auto;display:block" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 380" role="img" aria-label="2×2 最大池化與平均池化：4×4 輸入切成四塊，最大池化取 6、8、4、9，平均池化取 3.75、5.25、2.5、3.75"><defs><marker id="ar" markerWidth="8" markerHeight="8" refX="6" refY="3" orient="auto"><path d="M0,0 L6,3 L0,6 z" fill="#8b949e"/></marker></defs><rect x="0.75" y="0.75" width="998.5" height="378.5" rx="16" fill="#0d1117" stroke="#222b36" stroke-width="1.5"/><text x="40" y="44" font-size="20" fill="#e6edf3" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">池化：把每個小區塊壓成一格</text><text x="40" y="72" font-size="13" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">2×2 視窗、步幅 2：面積剩 1/4，而且沒有任何可訓練參數</text><text x="60" y="106" font-size="12.5" fill="#e6edf3" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">輸入 4×4</text><rect x="60" y="120" width="52" height="52" rx="4" fill="#4ecdc426" stroke="#4ecdc4" stroke-width="1.6"/><text x="86.0" y="151.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="112" y="120" width="52" height="52" rx="4" fill="#4ecdc426" stroke="#4ecdc4" stroke-width="1.6"/><text x="138.0" y="151.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">3</text><rect x="60" y="172" width="52" height="52" rx="4" fill="#4ecdc426" stroke="#4ecdc4" stroke-width="1.6"/><text x="86.0" y="203.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">5</text><rect x="112" y="172" width="52" height="52" rx="4" fill="#4ecdc426" stroke="#4ecdc4" stroke-width="1.6"/><text x="138.0" y="203.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">6</text><rect x="164" y="120" width="52" height="52" rx="4" fill="#58a6ff26" stroke="#58a6ff" stroke-width="1.6"/><text x="190.0" y="151.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">2</text><rect x="216" y="120" width="52" height="52" rx="4" fill="#58a6ff26" stroke="#58a6ff" stroke-width="1.6"/><text x="242.0" y="151.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">4</text><rect x="164" y="172" width="52" height="52" rx="4" fill="#58a6ff26" stroke="#58a6ff" stroke-width="1.6"/><text x="190.0" y="203.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">7</text><rect x="216" y="172" width="52" height="52" rx="4" fill="#58a6ff26" stroke="#58a6ff" stroke-width="1.6"/><text x="242.0" y="203.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">8</text><rect x="60" y="224" width="52" height="52" rx="4" fill="#c084fc26" stroke="#c084fc" stroke-width="1.6"/><text x="86.0" y="255.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">3</text><rect x="112" y="224" width="52" height="52" rx="4" fill="#c084fc26" stroke="#c084fc" stroke-width="1.6"/><text x="138.0" y="255.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">2</text><rect x="60" y="276" width="52" height="52" rx="4" fill="#c084fc26" stroke="#c084fc" stroke-width="1.6"/><text x="86.0" y="307.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="112" y="276" width="52" height="52" rx="4" fill="#c084fc26" stroke="#c084fc" stroke-width="1.6"/><text x="138.0" y="307.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">4</text><rect x="164" y="224" width="52" height="52" rx="4" fill="#e0a45826" stroke="#e0a458" stroke-width="1.6"/><text x="190.0" y="255.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="216" y="224" width="52" height="52" rx="4" fill="#e0a45826" stroke="#e0a458" stroke-width="1.6"/><text x="242.0" y="255.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0</text><rect x="164" y="276" width="52" height="52" rx="4" fill="#e0a45826" stroke="#e0a458" stroke-width="1.6"/><text x="190.0" y="307.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">9</text><rect x="216" y="276" width="52" height="52" rx="4" fill="#e0a45826" stroke="#e0a458" stroke-width="1.6"/><text x="242.0" y="307.04" font-size="14" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">5</text><rect x="112" y="172" width="52" height="52" rx="3" fill="none" stroke="#e6edf3" stroke-width="1.8"/><rect x="216" y="172" width="52" height="52" rx="3" fill="none" stroke="#e6edf3" stroke-width="1.8"/><rect x="112" y="276" width="52" height="52" rx="3" fill="none" stroke="#e6edf3" stroke-width="1.8"/><rect x="164" y="224" width="52" height="52" rx="3" fill="none" stroke="#e6edf3" stroke-width="1.8"/><line x1="276" y1="224" x2="340" y2="224" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><text x="308" y="202" font-size="11.5" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">取最大</text><text x="350" y="152" font-size="12" fill="#4ecdc4" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">最大池化 2×2</text><rect x="350" y="168" width="56" height="56" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="378.0" y="201.4" font-size="15" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">6</text><rect x="406" y="168" width="56" height="56" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="434.0" y="201.4" font-size="15" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">8</text><rect x="350" y="224" width="56" height="56" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="378.0" y="257.4" font-size="15" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">4</text><rect x="406" y="224" width="56" height="56" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="434.0" y="257.4" font-size="15" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">9</text><text x="350" y="300" font-size="11.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">留最強響應</text><text x="530" y="228" font-size="12" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">或</text><text x="600" y="152" font-size="12" fill="#58a6ff" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">平均池化 2×2</text><rect x="600" y="168" width="56" height="56" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="628.0" y="200.32" font-size="12" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">3.75</text><rect x="656" y="168" width="56" height="56" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="684.0" y="200.32" font-size="12" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">5.25</text><rect x="600" y="224" width="56" height="56" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="628.0" y="256.32" font-size="12" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">2.5</text><rect x="656" y="224" width="56" height="56" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="684.0" y="256.32" font-size="12" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">3.75</text><text x="600" y="300" font-size="11.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">留平均亮度</text><text x="40" y="352" font-size="12" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">最大池化保留紋理與邊緣，平均池化保留整體亮度；兩者都不從資料學東西。</text></svg>
<figcaption style="font-size:13.5px;color:#9aa7b4;margin-top:10px;line-height:1.65;text-align:left">池化把 2×2 壓成一格：最大池化留下最強響應，平均池化留下平均亮度；兩者都沒有可訓練參數。</figcaption>
</figure>
<!-- /CNNI-FIGURE:pool -->

池化（pooling）是下採樣：把一小塊區域壓縮成一個值。最常見的是 2×2 視窗、步幅 2，
面積直接剩四分之一。本機實測（4×4 輸入）：

| 做法 | 四塊的結果 | 特性 |
|---|---|---|
| 最大池化 | 6、8、4、9 | 留下每個區域最強的響應（紋理、邊緣） |
| 平均池化 | 3.75、5.25、2.5、3.75 | 留下整個區域的平均亮度 |

三件容易搞混的事：

- **池化沒有參數**。它不做加權，只是固定地取最大或取平均，所以反向傳播時也沒有東西要學。
- **它給的是「局部容忍」，不是全域不變**。在 2×2 視窗內把圖挪一格，最大值通常不變；
  但挪得夠遠，特徵圖還是會變。說「池化讓 CNN 對平移不變」是過度簡化。
- **不是每家都用最大池化**。LeNet-5 用的是**平均池化**，而且原版那兩層池化還帶了可訓練的參數
  （`6×2 + 16×2 = 44` 個，見 §8）。

## 7. 全連接層與 softmax：把特徵變成機率

<!-- CNNI-FIGURE:head -->
<figure style="margin:22px 0;padding:14px 12px;background:#0d1117;border:1px solid #222b36;border-radius:12px;text-align:center;-webkit-print-color-adjust:exact;print-color-adjust:exact">
<svg style="width:100%;height:auto;display:block" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 360" role="img" aria-label="分類頭：7×7×16 攤平成 784 維，接 64 個神經元，再由 softmax 把 logits 4.2、1.3、0.4 變成 0.9282、0.0511、0.0208 的機率"><defs><marker id="ar" markerWidth="8" markerHeight="8" refX="6" refY="3" orient="auto"><path d="M0,0 L6,3 L0,6 z" fill="#8b949e"/></marker></defs><rect x="0.75" y="0.75" width="998.5" height="358.5" rx="16" fill="#0d1117" stroke="#222b36" stroke-width="1.5"/><text x="40" y="44" font-size="20" fill="#e6edf3" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">分類頭：攤平 → 全連接 → softmax</text><text x="40" y="72" font-size="13" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">前面抽到的特徵在這裡被壓成「每個類別一個分數」，再轉成機率</text><text x="60" y="136" font-size="12" fill="#e6edf3" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">Flatten：7×7×16 = 784 維</text><rect x="60" y="150" width="18" height="18" rx="3" fill="#12181f" stroke="#30363d" stroke-width="1" opacity="0.9"/><rect x="80" y="150" width="18" height="18" rx="3" fill="#12181f" stroke="#30363d" stroke-width="1" opacity="0.9"/><rect x="100" y="150" width="18" height="18" rx="3" fill="#12181f" stroke="#30363d" stroke-width="1" opacity="0.9"/><rect x="120" y="150" width="18" height="18" rx="3" fill="#12181f" stroke="#30363d" stroke-width="1" opacity="0.9"/><rect x="140" y="150" width="18" height="18" rx="3" fill="#12181f" stroke="#30363d" stroke-width="1" opacity="0.9"/><rect x="160" y="150" width="18" height="18" rx="3" fill="#12181f" stroke="#30363d" stroke-width="1" opacity="0.9"/><rect x="180" y="150" width="18" height="18" rx="3" fill="#12181f" stroke="#30363d" stroke-width="1" opacity="0.9"/><rect x="200" y="150" width="18" height="18" rx="3" fill="#12181f" stroke="#30363d" stroke-width="1" opacity="0.9"/><rect x="220" y="150" width="18" height="18" rx="3" fill="#12181f" stroke="#30363d" stroke-width="1" opacity="0.9"/><rect x="240" y="150" width="18" height="18" rx="3" fill="#12181f" stroke="#30363d" stroke-width="1" opacity="0.9"/><rect x="260" y="150" width="18" height="18" rx="3" fill="#12181f" stroke="#30363d" stroke-width="1" opacity="0.9"/><rect x="280" y="150" width="18" height="18" rx="3" fill="#12181f" stroke="#30363d" stroke-width="1" opacity="0.9"/><text x="306" y="165" font-size="14" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">…</text><line x1="322" y1="159" x2="352" y2="159" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="356" y="124" width="110" height="72" rx="8" fill="#12181f" stroke="#4ecdc4" stroke-width="1.6"/><text x="411" y="154" font-size="12" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">Dense 64</text><text x="411" y="174" font-size="10.5" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">+ ReLU</text><line x1="472" y1="159" x2="502" y2="159" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><text x="540" y="112" font-size="12" fill="#e6edf3" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">softmax → 每個類別的機率</text><text x="600" y="155" font-size="13" fill="#e6edf3" font-weight="700" text-anchor="end" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">狗</text><rect x="612" y="141" width="259.896" height="20" rx="3" fill="#4ecdc4" stroke="none" stroke-width="1" opacity="0.9"/><text x="879.896" y="155" font-size="12" fill="#4ecdc4" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0.9282</text><text x="600" y="191" font-size="13" fill="#e6edf3" font-weight="700" text-anchor="end" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">貓</text><rect x="612" y="177" width="14.308" height="20" rx="3" fill="#58a6ff" stroke="none" stroke-width="1" opacity="0.9"/><text x="634.308" y="191" font-size="12" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0.0511</text><text x="600" y="227" font-size="13" fill="#e6edf3" font-weight="700" text-anchor="end" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">鳥</text><rect x="612" y="213" width="5.824" height="20" rx="3" fill="#58a6ff" stroke="none" stroke-width="1" opacity="0.9"/><text x="625.824" y="227" font-size="12" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0.0208</text><text x="612" y="262" font-size="11.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0.9282 + 0.0511 + 0.0208 = 1.0000</text><text x="40" y="320" font-size="12" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">logits [4.2, 1.3, 0.4] 只是分數；softmax 才把它變成加總為 1 的機率，預測結果取最大那一格。</text></svg>
<figcaption style="font-size:13.5px;color:#9aa7b4;margin-top:10px;line-height:1.65;text-align:left">分類頭的工作：把攤平後的特徵向量壓成 10 個分數（logits），再用 softmax 轉成加總為 1 的機率。</figcaption>
</figure>
<!-- /CNNI-FIGURE:head -->

特徵圖被攤平成一條向量後，交給全連接層整合，最後一層輸出「每個類別一個分數」（logits）。
logits 有正有負、加起來不是 1，所以要用 softmax 轉成機率：

```text
softmax(z)_i = exp(z_i) / Σ exp(z_j)
```

本機實測，三個類別的 logits 是 `[4.2, 1.3, 0.4]`：

| 類別 | logit | softmax 之後 |
|---|---|---|
| 狗 | 4.2 | **0.9282** |
| 貓 | 1.3 | 0.0511 |
| 鳥 | 0.4 | 0.0208 |
| 合計 | — | 1.0000 |

兩個性質值得記住：softmax **不改變排序**（最大的 logit 還是最大的機率），
它只是把差距拉開、把總和壓成 1。所以推論時直接取 `argmax` 就好，不一定要算 softmax。

## 8. 完整例子：手寫數字 0–9

<!-- CNNI-FIGURE:mnist -->
<figure style="margin:22px 0;padding:14px 12px;background:#0d1117;border:1px solid #222b36;border-radius:12px;text-align:center;-webkit-print-color-adjust:exact;print-color-adjust:exact">
<svg style="width:100%;height:auto;display:block" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 300" role="img" aria-label="MNIST 小 CNN 的形狀階梯：28×28×1 → 28×28×8 → 14×14×8 → 14×14×16 → 7×7×16 → 784 維 → 64 維 → 10 個類別"><defs><marker id="ar" markerWidth="8" markerHeight="8" refX="6" refY="3" orient="auto"><path d="M0,0 L6,3 L0,6 z" fill="#8b949e"/></marker></defs><rect x="0.75" y="0.75" width="998.5" height="298.5" rx="16" fill="#0d1117" stroke="#222b36" stroke-width="1.5"/><text x="40" y="44" font-size="20" fill="#e6edf3" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">一個能跑 MNIST 的小 CNN</text><text x="40" y="72" font-size="13" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">形狀一路變小、通道一路變多，最後交出 10 個數字</text><rect x="40" y="104" width="88" height="56" rx="8" fill="#12181f" stroke="#4ecdc4" stroke-width="1.6" opacity="0.9"/><text x="84.0" y="138" font-size="11.5" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">28×28×1</text><text x="84.0" y="178" font-size="10" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">輸入</text><line x1="131" y1="132.0" x2="153" y2="132.0" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><text x="143.0" y="94" font-size="9.5" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">Conv</text><rect x="158" y="104" width="88" height="56" rx="8" fill="#12181f" stroke="#4ecdc4" stroke-width="1.6" opacity="0.9"/><text x="202.0" y="138" font-size="11.5" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">28×28×8</text><text x="202.0" y="178" font-size="10" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">卷積</text><line x1="249" y1="132.0" x2="271" y2="132.0" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><text x="261.0" y="94" font-size="9.5" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">Pool</text><rect x="276" y="104" width="88" height="56" rx="8" fill="#12181f" stroke="#58a6ff" stroke-width="1.6" opacity="0.9"/><text x="320.0" y="138" font-size="11.5" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">14×14×8</text><text x="320.0" y="178" font-size="10" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">池化</text><line x1="367" y1="132.0" x2="389" y2="132.0" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><text x="379.0" y="94" font-size="9.5" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">Conv</text><rect x="394" y="104" width="88" height="56" rx="8" fill="#12181f" stroke="#4ecdc4" stroke-width="1.6" opacity="0.9"/><text x="438.0" y="138" font-size="11.5" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">14×14×16</text><text x="438.0" y="178" font-size="10" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">卷積</text><line x1="485" y1="132.0" x2="507" y2="132.0" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><text x="497.0" y="94" font-size="9.5" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">Pool</text><rect x="512" y="104" width="88" height="56" rx="8" fill="#12181f" stroke="#58a6ff" stroke-width="1.6" opacity="0.9"/><text x="556.0" y="138" font-size="11.5" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">7×7×16</text><text x="556.0" y="178" font-size="10" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">池化</text><line x1="603" y1="132.0" x2="625" y2="132.0" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><text x="615.0" y="94" font-size="9.5" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">Flatten</text><rect x="630" y="104" width="88" height="56" rx="8" fill="#12181f" stroke="#e0a458" stroke-width="1.6" opacity="0.9"/><text x="674.0" y="138" font-size="11.5" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">784 維</text><text x="674.0" y="178" font-size="10" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">攤平</text><line x1="721" y1="132.0" x2="743" y2="132.0" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><text x="733.0" y="94" font-size="9.5" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">Dense</text><rect x="748" y="104" width="88" height="56" rx="8" fill="#12181f" stroke="#e0a458" stroke-width="1.6" opacity="0.9"/><text x="792.0" y="138" font-size="11.5" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">64 維</text><text x="792.0" y="178" font-size="10" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">全連接</text><line x1="839" y1="132.0" x2="861" y2="132.0" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><text x="851.0" y="94" font-size="9.5" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">Softmax</text><rect x="866" y="104" width="88" height="56" rx="8" fill="#12181f" stroke="#ff7b72" stroke-width="1.6" opacity="0.9"/><text x="910.0" y="138" font-size="11.5" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">10 維</text><text x="910.0" y="178" font-size="10" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">softmax</text><text x="40" y="226" font-size="12" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">卷積改通道數（filter 數量）、池化砍面積、Flatten 只攤平、Dense 才做判斷。</text><text x="40" y="250" font-size="12" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">光第一層全連接（784 → 64）就有 50,240 個參數，比兩個卷積層加起來的 1,248 個多 40 倍。</text></svg>
<figcaption style="font-size:13.5px;color:#9aa7b4;margin-top:10px;line-height:1.65;text-align:left">一個能跑 MNIST 的小 CNN 的形狀階梯：卷積改通道數、池化砍面積、Flatten 只負責攤平、Dense 才做判斷。</figcaption>
</figure>
<!-- /CNNI-FIGURE:mnist -->

1. 輸入一張 `28×28` 的灰階影像（784 個 0–255 的數字）。
2. 第一層卷積用 8 個 `3×3` filter、補一圈邊 → `28×28×8`；每個 filter 學一種筆畫方向的邊緣。
3. ReLU 把負響應砍成 0，只留下「這裡真的有這種邊緣」。
4. 2×2 最大池化 → `14×14×8`，面積剩四分之一。
5. 再一輪卷積（16 個 filter）＋池化 → `7×7×16`，這時的單一格已經看得到弧線、交叉、封閉圈。
6. 攤平成 `7×7×16 = 784` 維，接 64 個神經元，最後輸出 10 個 logits。
7. softmax → 10 個數字相加為 1，最大的那個就是預測。

這條流程的參數分佈值得看一眼：兩個卷積層一共 `(3×3×1+1)×8 + (3×3×8+1)×16 = 80 + 1,168 = 1,248` 個參數，
而**第一層全連接（784 → 64）一個就有 `(784+1)×64 = 50,240` 個**——是卷積層的 40 倍。
「CNN 參數少」說的是卷積層，不是整個網路。

要對照真正的歷史版本，LeNet-5 的逐層數字是（實測加總）：

| 層 | 形狀 | 參數 |
|---|---|---|
| C1 卷積 5×5×6 | 32×32×1 → 28×28×6 | 156 |
| S2 平均池化（原版可訓練） | 28×28×6 → 14×14×6 | 12 |
| C3 卷積 5×5×16 | 14×14×6 → 10×10×16 | 2,416 |
| S4 平均池化（原版可訓練） | 10×10×16 → 5×5×16 | 32 |
| C5 卷積 5×5×120 | 5×5×16 → 1×1×120 | 48,120 |
| F6 全連接 84 | 120 → 84 | 10,164 |
| 輸出層 | 84 → 10 | 850 |
| **合計** | 只算卷積＋全連接 | **61,706** |
| **合計** | 加上可訓練池化 | **61,750** |

## 9. 兩種常見畫法：概念架構與三種任務

<!-- CNNI-FIGURE:concept-arch -->
<figure style="margin:22px 0;padding:14px 12px;background:#0d1117;border:1px solid #222b36;border-radius:12px;text-align:center;-webkit-print-color-adjust:exact;print-color-adjust:exact">
<svg style="width:100%;height:auto;display:block" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 410" role="img" aria-label="概念架構：輸入 224×224×3 一路縮到 14×14×256 再攤平，最後由 Dense 加 softmax 給出貓 0.93、狗 0.05、鳥 0.02"><defs><marker id="ar" markerWidth="8" markerHeight="8" refX="6" refY="3" orient="auto"><path d="M0,0 L6,3 L0,6 z" fill="#8b949e"/></marker></defs><rect x="0.75" y="0.75" width="998.5" height="408.5" rx="16" fill="#0d1117" stroke="#222b36" stroke-width="1.5"/><text x="40" y="44" font-size="20" fill="#e6edf3" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">概念架構：一路變小，最後才做決定</text><text x="40" y="72" font-size="13" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">每個方塊的「高度」就是空間解析度；通道數正好相反，一路變多</text><rect x="50" y="160" width="96" height="160" rx="6" fill="#12181f" stroke="#4ecdc4aa" stroke-width="1.4" opacity="0.95"/><line x1="50" y1="200.0" x2="146" y2="200.0" stroke="#4ecdc4" stroke-width="0.7" opacity="0.35"/><line x1="50" y1="240.0" x2="146" y2="240.0" stroke="#4ecdc4" stroke-width="0.7" opacity="0.35"/><line x1="50" y1="280.0" x2="146" y2="280.0" stroke="#4ecdc4" stroke-width="0.7" opacity="0.35"/><text x="98.0" y="342" font-size="9.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">224×224×3</text><text x="98.0" y="360" font-size="9.5" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">輸入</text><line x1="149" y1="240.0" x2="173" y2="240.0" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="178" y="186" width="96" height="134" rx="6" fill="#12181f" stroke="#4ecdc4aa" stroke-width="1.4" opacity="0.95"/><line x1="178" y1="219.5" x2="274" y2="219.5" stroke="#4ecdc4" stroke-width="0.7" opacity="0.35"/><line x1="178" y1="253.0" x2="274" y2="253.0" stroke="#4ecdc4" stroke-width="0.7" opacity="0.35"/><line x1="178" y1="286.5" x2="274" y2="286.5" stroke="#4ecdc4" stroke-width="0.7" opacity="0.35"/><text x="226.0" y="342" font-size="9.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">112×112×64</text><text x="226.0" y="360" font-size="9.5" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">Conv+ReLU</text><line x1="277" y1="253.0" x2="301" y2="253.0" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="306" y="210" width="96" height="110" rx="6" fill="#12181f" stroke="#58a6ffaa" stroke-width="1.4" opacity="0.95"/><line x1="306" y1="237.5" x2="402" y2="237.5" stroke="#58a6ff" stroke-width="0.7" opacity="0.35"/><line x1="306" y1="265.0" x2="402" y2="265.0" stroke="#58a6ff" stroke-width="0.7" opacity="0.35"/><line x1="306" y1="292.5" x2="402" y2="292.5" stroke="#58a6ff" stroke-width="0.7" opacity="0.35"/><text x="354.0" y="342" font-size="9.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">56×56×64</text><text x="354.0" y="360" font-size="9.5" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">Pool</text><line x1="405" y1="265.0" x2="429" y2="265.0" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="434" y="234" width="96" height="86" rx="6" fill="#12181f" stroke="#4ecdc4aa" stroke-width="1.4" opacity="0.95"/><line x1="434" y1="255.5" x2="530" y2="255.5" stroke="#4ecdc4" stroke-width="0.7" opacity="0.35"/><line x1="434" y1="277.0" x2="530" y2="277.0" stroke="#4ecdc4" stroke-width="0.7" opacity="0.35"/><line x1="434" y1="298.5" x2="530" y2="298.5" stroke="#4ecdc4" stroke-width="0.7" opacity="0.35"/><text x="482.0" y="342" font-size="9.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">28×28×256</text><text x="482.0" y="360" font-size="9.5" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">Conv+ReLU</text><line x1="533" y1="277.0" x2="557" y2="277.0" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="562" y="258" width="96" height="62" rx="6" fill="#12181f" stroke="#58a6ffaa" stroke-width="1.4" opacity="0.95"/><line x1="562" y1="273.5" x2="658" y2="273.5" stroke="#58a6ff" stroke-width="0.7" opacity="0.35"/><line x1="562" y1="289.0" x2="658" y2="289.0" stroke="#58a6ff" stroke-width="0.7" opacity="0.35"/><line x1="562" y1="304.5" x2="658" y2="304.5" stroke="#58a6ff" stroke-width="0.7" opacity="0.35"/><text x="610.0" y="342" font-size="9.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">14×14×256</text><text x="610.0" y="360" font-size="9.5" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">Pool</text><line x1="661" y1="289.0" x2="685" y2="289.0" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="690" y="280" width="96" height="40" rx="6" fill="#12181f" stroke="#e0a458aa" stroke-width="1.4" opacity="0.95"/><line x1="690" y1="290.0" x2="786" y2="290.0" stroke="#e0a458" stroke-width="0.7" opacity="0.35"/><line x1="690" y1="300.0" x2="786" y2="300.0" stroke="#e0a458" stroke-width="0.7" opacity="0.35"/><line x1="690" y1="310.0" x2="786" y2="310.0" stroke="#e0a458" stroke-width="0.7" opacity="0.35"/><text x="738.0" y="342" font-size="9.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">25,088</text><text x="738.0" y="360" font-size="9.5" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">Flatten</text><line x1="78" y1="96" x2="640" y2="96" stroke="#4ecdc4" stroke-width="1"/><text x="359" y="88" font-size="11" fill="#4ecdc4" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">特徵抽取：Conv → ReLU → Pool（重複 N 次）</text><line x1="806" y1="96" x2="976" y2="96" stroke="#e0a458" stroke-width="1"/><text x="891" y="88" font-size="11" fill="#e0a458" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">決策</text><rect x="806" y="180" width="170" height="140" rx="10" fill="#12181f" stroke="#ff7b7299" stroke-width="1.4"/><text x="822" y="206" font-size="11" fill="#ff7b72" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">Dense → softmax</text><text x="822" y="236" font-size="10" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">貓</text><rect x="846" y="226" width="74.4" height="16" rx="3" fill="#ff7b72" stroke="none" stroke-width="1" opacity="0.8"/><text x="926.4" y="237" font-size="9.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0.93</text><text x="822" y="262" font-size="10" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">狗</text><rect x="846" y="252" width="4.0" height="16" rx="3" fill="#ff7b72" stroke="none" stroke-width="1" opacity="0.8"/><text x="856.0" y="263" font-size="9.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0.05</text><text x="822" y="288" font-size="10" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">鳥</text><rect x="846" y="278" width="3" height="16" rx="3" fill="#ff7b72" stroke="none" stroke-width="1" opacity="0.8"/><text x="855" y="289" font-size="9.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0.02</text><line x1="790" y1="250" x2="802" y2="250" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><text x="50" y="386" font-size="12" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">同一張圖，換掉最後那一段（決策頭）就是另一個任務——見下圖。</text></svg>
<figcaption style="font-size:13.5px;color:#9aa7b4;margin-top:10px;line-height:1.65;text-align:left">概念架構的常見畫法：方塊高度就是解析度，一路變小、通道一路變多，最後才接決策頭。</figcaption>
</figure>
<!-- /CNNI-FIGURE:concept-arch -->

把整個網路畫成一排方塊時，有一個更直覺的版本：**方塊的高度就是空間解析度**。
`224×224×3` 一路縮到 `14×14×256`、再攤平成 `25,088` 維，最後才由 `Dense → softmax` 給出機率。
這張圖在教科書與外部資料裡很常見（例如 [Dragon1 的 CNN 定義頁](https://www.dragon1.com/terms/convolutional-neural-networks-definition)），
本頁依照同一個概念重畫成深色版，沒有重製原圖。

<!-- CNNI-FIGURE:tasks -->
<figure style="margin:22px 0;padding:14px 12px;background:#0d1117;border:1px solid #222b36;border-radius:12px;text-align:center;-webkit-print-color-adjust:exact;print-color-adjust:exact">
<svg style="width:100%;height:auto;display:block" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 410" role="img" aria-label="同一張圖的三種任務：分類輸出一個標籤、偵測輸出框加類別、分割輸出逐像素遮罩"><defs><marker id="ar" markerWidth="8" markerHeight="8" refX="6" refY="3" orient="auto"><path d="M0,0 L6,3 L0,6 z" fill="#8b949e"/></marker></defs><rect x="0.75" y="0.75" width="998.5" height="408.5" rx="16" fill="#0d1117" stroke="#222b36" stroke-width="1.5"/><text x="40" y="44" font-size="20" fill="#e6edf3" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">同一張圖，三種答案</text><text x="40" y="72" font-size="13" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">骨幹可以完全一樣；差別在「輸出頭」與「損失函數」</text><rect x="30" y="96" width="300" height="250" rx="12" fill="#12181f" stroke="#4ecdc488" stroke-width="1.4" opacity="0.6"/><text x="46" y="122" font-size="13" fill="#4ecdc4" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">分類</text><text x="96" y="122" font-size="10.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">這張圖是什麼？</text><rect x="50" y="140" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="76" y="140" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="102" y="140" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="128" y="140" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="154" y="140" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="180" y="140" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="50" y="166" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="76" y="166" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="102" y="166" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="128" y="166" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="154" y="166" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="180" y="166" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="50" y="192" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="76" y="192" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="102" y="192" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="128" y="192" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="154" y="192" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="180" y="192" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="50" y="218" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="76" y="218" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="102" y="218" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="128" y="218" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="154" y="218" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="180" y="218" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="50" y="244" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="76" y="244" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="102" y="244" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="128" y="244" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="154" y="244" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="180" y="244" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="50" y="270" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="76" y="270" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="102" y="270" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="128" y="270" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="154" y="270" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="180" y="270" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><line x1="210" y1="218" x2="240" y2="218" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><text x="240" y="190" font-size="10.5" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">一個標籤</text><text x="240" y="212" font-size="9.5" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">輸出</text><text x="240" y="232" font-size="9" fill="#4ecdc4" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1 個類別機率</text><text x="46" y="288" font-size="10" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">標籤</text><text x="46" y="312" font-size="10.5" fill="#4ecdc4" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">只看「有沒有」</text><rect x="348" y="96" width="300" height="250" rx="12" fill="#12181f" stroke="#e0a45888" stroke-width="1.4" opacity="0.6"/><text x="364" y="122" font-size="13" fill="#e0a458" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">偵測</text><text x="414" y="122" font-size="10.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">物件在哪裡？</text><rect x="368" y="140" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="394" y="140" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="420" y="140" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="446" y="140" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="472" y="140" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="498" y="140" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="368" y="166" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="394" y="166" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="420" y="166" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="446" y="166" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="472" y="166" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="498" y="166" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="368" y="192" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="394" y="192" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="420" y="192" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="446" y="192" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="472" y="192" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="498" y="192" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="368" y="218" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="394" y="218" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="420" y="218" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="446" y="218" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="472" y="218" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="498" y="218" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="368" y="244" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="394" y="244" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="420" y="244" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="446" y="244" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="472" y="244" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="498" y="244" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="368" y="270" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="394" y="270" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="420" y="270" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="446" y="270" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="472" y="270" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="498" y="270" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="394" y="166" width="78" height="78" rx="2" fill="none" stroke="#e0a458" stroke-width="2"/><line x1="528" y1="218" x2="558" y2="218" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><text x="558" y="190" font-size="10.5" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">框 + 類別</text><text x="558" y="212" font-size="9.5" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">輸出</text><text x="558" y="232" font-size="9" fill="#e0a458" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">K × (x, y, w, h, 類別)</text><text x="364" y="288" font-size="10" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">座標 + 標籤</text><text x="364" y="312" font-size="10.5" fill="#e0a458" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">還要「在哪裡」</text><rect x="666" y="96" width="300" height="250" rx="12" fill="#12181f" stroke="#c084fc88" stroke-width="1.4" opacity="0.6"/><text x="682" y="122" font-size="13" fill="#c084fc" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">分割</text><text x="732" y="122" font-size="10.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">每個像素是什麼？</text><rect x="686" y="140" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="712" y="140" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="738" y="140" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="764" y="140" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="790" y="140" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="816" y="140" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="686" y="166" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="712" y="166" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="738" y="166" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="764" y="166" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="790" y="166" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="816" y="166" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="686" y="192" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="712" y="192" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="738" y="192" width="26" height="26" rx="2" fill="#c084fc55" stroke="#30363d" stroke-width="0.8"/><rect x="764" y="192" width="26" height="26" rx="2" fill="#c084fc55" stroke="#30363d" stroke-width="0.8"/><rect x="790" y="192" width="26" height="26" rx="2" fill="#c084fc55" stroke="#30363d" stroke-width="0.8"/><rect x="816" y="192" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="686" y="218" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="712" y="218" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="738" y="218" width="26" height="26" rx="2" fill="#c084fc55" stroke="#30363d" stroke-width="0.8"/><rect x="764" y="218" width="26" height="26" rx="2" fill="#c084fc55" stroke="#30363d" stroke-width="0.8"/><rect x="790" y="218" width="26" height="26" rx="2" fill="#c084fc55" stroke="#30363d" stroke-width="0.8"/><rect x="816" y="218" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="686" y="244" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="712" y="244" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="738" y="244" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="764" y="244" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="790" y="244" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="816" y="244" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="686" y="270" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="712" y="270" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="738" y="270" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="764" y="270" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="790" y="270" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><rect x="816" y="270" width="26" height="26" rx="2" fill="#12181f" stroke="#30363d" stroke-width="0.8"/><line x1="846" y1="218" x2="876" y2="218" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><text x="876" y="190" font-size="10.5" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">逐像素遮罩</text><text x="876" y="212" font-size="9.5" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">輸出</text><text x="876" y="232" font-size="9" fill="#c084fc" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">H × W × 類別</text><text x="682" y="288" font-size="10" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">像素級標註</text><text x="682" y="312" font-size="10.5" fill="#c084fc" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">每個位置都要</text><text x="40" y="384" font-size="12" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">這也是為什麼分類可以用 GAP 收尾，而偵測／分割必須保留空間維度。</text></svg>
<figcaption style="font-size:13.5px;color:#9aa7b4;margin-top:10px;line-height:1.65;text-align:left">同一張圖餵進同一個骨幹，換一個輸出頭就是另一個任務：分類只看有沒有、偵測要位置、分割要每個像素。</figcaption>
</figure>
<!-- /CNNI-FIGURE:tasks -->

第二張圖回答另一個常見問題：**同樣的骨幹，換一個輸出頭就是另一個任務**。

| 任務 | 輸出 | 需要的空間資訊 |
|---|---|---|
| 分類 | 1 個類別機率（`K`） | 幾乎不需要（所以可以用 GAP 收尾） |
| 偵測 | `K × (x, y, w, h, 類別)` | 要位置 |
| 分割 | `H × W × 類別` | 每個像素都要 |

這也是為什麼卷積層一路保留下來的「空間排列」很重要：一旦攤平，位置就沒了，
分類還無所謂，偵測與分割就直接失效。

## 10. 七個常見誤解

| 誤解 | 事實 |
|---|---|
| filter 越大看得越廣，所以越大越好 | 兩層 3×3 的感受野等於一層 5×5，但參數只要 `2×9 = 18` 個（5×5 要 25 個），還多了一次非線性 |
| 池化一定用最大池化 | LeNet-5 用平均池化；現代網路也常用「步幅 2 的卷積」直接取代池化 |
| CNN 一定要有池化層 | 沒有池化也能做，靠 stride 或空洞卷積降尺寸 |
| 特徵圖越多越好 | 通道加倍，下一層的參數與計算量也跟著加倍 |
| softmax 是一種激活函數 | 它是最後一層的輸出轉換，隱藏層用的是 ReLU 這類逐元素函數 |
| 卷積就是數學上的卷積 | 實作是互相關（核心不翻轉），只是名字沿用 |
| 攤平再接全連接是必須的 | 全域平均池化（GAP）可以直接把每個通道壓成一個數字，參數更少 |

## 附錄 A 速查表

| 想問的事 | 答案 |
|---|---|
| 卷積在做什麼 | 逐元素相乘再相加（核心不翻轉，實作是互相關） |
| 輸出尺寸 | `O = (N + 2P − F) / S + 1`，除不盡往下取整 |
| 這一層的深度由什麼決定 | filter 的數量（不是輸入通道數） |
| 卷積參數量 | `(F×F×C_in + 1) × C_out` |
| 全連接參數量 | `(n_in + 1) × n_out` |
| 池化有參數嗎 | 沒有（原版 LeNet-5 的池化例外：帶了 scale 與 bias） |
| ReLU 的公式 | `max(0, x)` |
| 為什麼要非線性 | 線性疊線性還是一個線性變換 |
| softmax 的作用 | 把 logits 變成加總為 1 的機率，不改排序 |
| 預測怎麼取 | `argmax`（不一定要先算 softmax） |

## 附錄 B 常見錯誤（動手時最容易撞到的）

1. **把 padding 算成「加在輸出」**：padding 是補在輸入的邊緣，`P` 要乘 2 進公式。
2. **把 stride 和池化視窗搞混**：stride 是「滑幾格」，pooling size 是「一次看多大」；兩者可以不同。
3. **以為特徵圖通道數等於輸入通道數**：輸出通道數＝這一層 filter 的數量。
4. **在 softmax 前面又接 sigmoid**：多類別單選用 softmax，多標籤才用逐類 sigmoid。
5. **把 batch 維度當成通道**：`(N, C, H, W)` 的 `C` 才是通道，`N` 是樣本數。
6. **以為池化可以「放大」特徵**：池化只會縮小面積，放大的工作屬於上採樣（轉置卷積、插值）。

## 附錄 C 延伸閱讀與離線使用

站內：

- [卷積層的運作細節](/cnn-conv-details/)——本篇的續篇：一個輸出值的手算、多通道 kernel、感受野、空洞卷積
- [CNN 學習筆記（Lesson 6 完整複習＋60 題測驗）](/cnn-notes/)——卷積的數學、LeNet-5、卷積的反向傳播
- [CNN 2 學習筆記（Lesson 7）](/cnn2-notes/)——輸出體積、stride、感受野、ReLU 的兩個經典問題、空洞卷積
- [CNN 專題：十個實驗](/cnn-lab/)——把這些層真的訓練起來的實驗紀錄
- [CNN II 實驗紀錄](/2026/10/29/cnn-2-experiments/)——33 組 run 的對照

站外：

- [Dive into Deep Learning（繁中）— 卷積神經網路](https://zh.d2l.ai/chapter_convolutional-neural-networks/index.html)
- [LeCun et al., Gradient-Based Learning Applied to Document Recognition (1998)](http://yann.lecun.com/exdb/lenet/)
- [Convolutional Neural Networks Definition（Dragon1）](https://www.dragon1.com/terms/convolutional-neural-networks-definition)——另一種白話定義與四個應用例子（貓狗、交通號誌、手寫數字、醫學影像）。<br />該頁的示意圖為 **© Dragon1（All rights reserved）**，因此本筆記只引用連結、不重製圖片；要看圖請直接開原頁。
- [Convolutional Neural Network (CNN): A Complete Guide（LearnOpenCV, Bill Kromydas, 2023-01-18）](https://learnopencv.com/understanding-convolutional-neural-networks-cnn/)——以 VGG-16 為例，從 MLP 的三個問題（不具平移不變、參數爆炸、易過擬合）一路講到卷積、池化與全連接分類頭（© BIG VISION LLC）。<br />附註：該文的參數量範例有筆誤——`224×224×3` 接三層 128 與 10 類，實算約 **1,929 萬**（150,528×128 + 128×128 + 128×10），不是文中寫的「3,000 億」。

離線：本頁是**單一 HTML 檔、零外部資源**，下載後直接雙擊就能看；原始 Markdown 也在同一層。

## 附錄 D 一頁精華（考前 10 分鐘）

| # | 記這一條 |
|---|---|
| 1 | 卷積＝逐元素相乘再相加，核心滑過整張圖 |
| 2 | 輸出深度＝filter 數量；卷積參數只跟核大小與通道數有關 |
| 3 | `O = (N + 2P − F) / S + 1` |
| 4 | ReLU 提供非線性，負值歸 0，梯度也可能歸 0 |
| 5 | 池化沒有參數，面積剩 1/4，給的是局部容忍 |
| 6 | 參數九成以上集中在攤平後的全連接層 |
| 7 | softmax 把 logits 變成加總為 1 的機率，不改排序 |
| 8 | LeNet-5 約 6.2 萬個參數，用平均池化 |

## 附錄 E 互動測驗（30 題）

**🖱️ [開始作答](mc-quiz.html)**　即時計分、作答進度存在瀏覽器（localStorage）、
可切換「只看未答／只看答錯」，按「顯示答案」才揭曉答案與解析。

題目涵蓋本頁的實測數字與常見誤解，每題都附一句解析，答錯時可以直接對回上面的章節。

## 附錄 G 實測原始輸出

`verify_numbers.py` 的完整輸出（Python 3.15.1 / NumPy 2.5.4，本機執行）：

```text
------------------------------------------------------------------------
1. conv 5x5 * 3x3 (stride 1, no padding)
   input patch top-left:
[[1 1 1]
 [0 1 1]
 [0 0 1]]
   kernel:
[[1 0 1]
 [0 1 0]
 [1 0 1]]
   feature map:
 [[4 3 4]
 [2 4 3]
 [2 3 4]]
   top-left cell: 4 = (1*1+0*1+1*1) + (0*0+1*1+0*1) + (1*0+0*0+1*1)
------------------------------------------------------------------------
2. output size  O = (N + 2P - F)/S + 1
   valid    N=5 F=3 S=1 P=0           -> 3
   same     N=5 F=3 S=1 P=1           -> 5
   stride2  N=5 F=3 S=2 P=0           -> 2
   MNIST    N=28 F=3 S=1 P=1          -> 28
   MNIST    N=28 F=2 S=2 P=0 (pool)   -> 14
------------------------------------------------------------------------
3. one small MNIST CNN, shape by shape
   輸入             28 x 28 x  1
   Conv 3x3 x8    28 x 28 x  8
   Pool 2x2       14 x 14 x  8
   Conv 3x3 x16   14 x 14 x 16
   Pool 2x2        7 x  7 x 16
   Flatten        784 維
   Dense 64       64 維 (params 50240)
   Softmax 10     10 維
------------------------------------------------------------------------
4. ReLU(x) = max(0, x)
   in : [-3.   -1.   -0.25  0.    0.5   2.  ]
   out: [0.  0.  0.  0.  0.5 2. ]
------------------------------------------------------------------------
5. pooling 4x4 -> 2x2 (window 2x2, stride 2)
   input:
 [[1 3 2 4]
 [5 6 7 8]
 [3 2 1 0]
 [1 4 9 5]]
   max: [6, 8, 4, 9]
   avg: [3.75, 5.25, 2.5, 3.75]
   params: 0
------------------------------------------------------------------------
6. softmax of logits [4.2 1.3 0.4]
   probabilities: [0.9282 0.0511 0.0208]  sum = 1.0
------------------------------------------------------------------------
7. parameter counts
   Conv 3x3x3 -> 64 : 1792
   Dense 3136 -> 10 : 31370  (x17.5)
   FC 1024->120     : 123000  vs 5x5 conv: 25  (x4920)
   LeNet-5 C1=156 S2=12 C3=2416 S4=32 C5=48120 F6=10164 out=850
   LeNet-5 conv+fc total = 61706
   LeNet-5 incl. trainable pooling = 61750
------------------------------------------------------------------------
8. three filters, same 5x5 input -> three 3x3 maps
   vertical  [[-2, 0, 2], [-3, -2, 2], [-3, -1, 2]]
   diagonal  [[0, 0, 0], [0, -1, 0], [-2, -1, 2]]
   blob      [[2, 0, 2], [-2, 1, 0], [-2, 1, 2]]
------------------------------------------------------------------------
```

---

*本頁為個人學習筆記整理；圖表為手繪內嵌 SVG，數字全部由附錄 G 的程式算出。*
