# GAP 是什麼？為什麼現代 CNN 用它取代全連接層

> 系列第六篇。前五篇把卷積、激活、池化、全連接層都拆過了，這一篇回答兩個連在一起的問題：<br /><strong>① GAP 到底在做什麼？② 為什麼 2015 年之後的分類頭，幾乎都從「Flatten + 大 FC」換成「GAP + 一個小 Linear」？</strong><br />GAP 本身只有一行公式、零參數；它換來的東西（參數量、尺寸自由、可解釋性）才是重點。<br />所有數字都是本機用 NumPy 算的（Python 3.15.1 / NumPy 2.5.4），原始輸出在附錄 G。

<!-- CNGH-FIGURE:heads -->
<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="兩種分類頭：7×7×512 展平後接 1000 類全連接是 25,089,000 個參數；先做 GAP 壓成 512 再接 1000 類只要 513,000 個"><defs><marker id="arg" 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">兩種分類頭，差 48.9 倍</text><text x="40" y="72" font-size="13" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">同樣從 7×7×512 的特徵圖出發，輸出 1000 類</text><rect x="30" y="96" width="460" height="250" rx="12" fill="#12181f" stroke="#ff7b7277" stroke-width="1.4" opacity="0.7"/><text x="50" y="124" font-size="12.5" fill="#ff7b72" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">舊寫法：Flatten + 大 FC</text><rect x="50" y="150" width="124" height="56" rx="8" fill="#12181f" stroke="#ff7b72aa" stroke-width="1.4"/><text x="112" y="174" 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×512</text><line x1="177" y1="178" x2="197" y2="178" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="200" y="150" width="124" height="56" rx="8" fill="#12181f" stroke="#ff7b72aa" stroke-width="1.4"/><text x="262" y="174" font-size="11.5" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">Flatten</text><text x="262" y="192" font-size="10" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">25,088 維</text><line x1="327" y1="178" x2="347" y2="178" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="350" y="150" width="124" height="56" rx="8" fill="#12181f" stroke="#ff7b72aa" stroke-width="1.4"/><text x="412" y="174" font-size="11.5" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">FC 1000</text><text x="50" y="250" font-size="11.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">參數：25,088 × 1000 + 1000</text><text x="50" y="276" font-size="16" fill="#ff7b72" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">= 25,089,000</text><text x="50" y="310" font-size="10.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">權重集中在最沒有空間結構的那一層</text><rect x="510" y="96" width="460" height="250" rx="12" fill="#12181f" stroke="#4ecdc477" stroke-width="1.4" opacity="0.7"/><text x="530" y="124" font-size="12.5" fill="#4ecdc4" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">新寫法：GAP + 小 FC</text><rect x="530" y="150" width="124" height="56" rx="8" fill="#12181f" stroke="#4ecdc4aa" stroke-width="1.4"/><text x="592" y="174" 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×512</text><line x1="657" y1="178" x2="677" y2="178" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="680" y="150" width="124" height="56" rx="8" fill="#12181f" stroke="#4ecdc4aa" stroke-width="1.4"/><text x="742" y="174" font-size="11.5" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">GAP</text><text x="742" y="192" font-size="10" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">512 維</text><line x1="807" y1="178" x2="827" y2="178" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="830" y="150" width="124" height="56" rx="8" fill="#12181f" stroke="#4ecdc4aa" stroke-width="1.4"/><text x="892" y="174" font-size="11.5" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">FC 1000</text><text x="530" y="250" font-size="11.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">參數：512 × 1000 + 1000</text><text x="530" y="276" font-size="16" fill="#4ecdc4" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">= 513,000</text><text x="530" y="310" font-size="10.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">GAP 自己 0 個參數，只做平均</text><text x="40" y="384" font-size="12" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">兩邊的輸出完全一樣（1000 個 logits），差別只在「先壓縮還是先展平」。</text></svg>
<figcaption style="font-size:13.5px;color:#9aa7b4;margin-top:10px;line-height:1.65;text-align:left">同一張 7×7×512 特徵圖：展平再接 1000 類要 2,509 萬個參數；先 GAP 壓成 512 維再分類只要 51.3 萬——差 48.9 倍。</figcaption>
</figure>
<!-- /CNGH-FIGURE:heads -->

## 1. 舊寫法的代價

```text
Conv → Pool → Flatten → FC(很大) → FC → Softmax
```

最後一層特徵圖 `7×7×512`，展平是 **25,088** 維。接 1,000 類：

```text
25,088 × 1,000 + 1,000 = 25,089,000    ← 兩千五百萬個參數，只為了一個分類頭
```

## 2. GAP 是什麼：一個通道換一個數

<!-- CNGH-FIGURE:gap-mechanics -->
<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 400" role="img" aria-label="GAP 的機制：4×4 的通道 1 平均成 8.5，其餘通道各自平均，輸出 1×1×C；逐通道互不相干"><defs><marker id="arg" 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="398.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">GAP 就是每個通道各自取平均</text><text x="40" y="72" font-size="13" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">H×W×C 的每一張特徵圖 → 一個數；通道數不變、寬高都變 1</text><text x="60" y="112" font-size="11.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">通道 1（4×4）</text><rect x="60" y="124" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="80.0" y="147.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="100" y="124" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="120.0" y="147.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">2</text><rect x="140" y="124" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="160.0" y="147.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">3</text><rect x="180" y="124" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="200.0" y="147.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">4</text><rect x="60" y="164" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="80.0" y="187.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">5</text><rect x="100" y="164" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="120.0" y="187.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">6</text><rect x="140" y="164" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="160.0" y="187.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">7</text><rect x="180" y="164" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="200.0" y="187.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">8</text><rect x="60" y="204" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="80.0" y="227.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">9</text><rect x="100" y="204" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="120.0" y="227.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">10</text><rect x="140" y="204" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="160.0" y="227.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">11</text><rect x="180" y="204" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="200.0" y="227.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">12</text><rect x="60" y="244" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="80.0" y="267.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">13</text><rect x="100" y="244" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="120.0" y="267.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">14</text><rect x="140" y="244" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="160.0" y="267.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">15</text><rect x="180" y="244" width="40" height="40" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="200.0" y="267.96" font-size="11" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">16</text><text x="60" y="306" font-size="12" fill="#4ecdc4" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">平均 = 136 ÷ 16 = 8.5</text><text x="60" y="330" font-size="11" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">其餘 511 個通道各做一次</text><line x1="250" y1="210" x2="320" y2="210" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><text x="285" y="190" font-size="11.5" fill="#4ecdc4" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">GAP</text><text x="285" y="232" font-size="10" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0 參數</text><rect x="330" y="120" width="210" height="170" rx="10" fill="#12181f" stroke="#4ecdc477" stroke-width="1.4"/><text x="435" y="148" font-size="12" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1 × 1 × C</text><rect x="370" y="166" width="130" height="30" rx="5" fill="#12181f" stroke="#4ecdc4aa" stroke-width="1.2"/><text x="435" y="187" font-size="12" fill="#4ecdc4" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">8.50</text><rect x="370" y="202" width="130" height="30" rx="5" fill="#12181f" stroke="#58a6ffaa" stroke-width="1.2"/><text x="435" y="223" font-size="12" fill="#58a6ff" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0.42</text><rect x="370" y="238" width="130" height="30" rx="5" fill="#12181f" stroke="#c084fcaa" stroke-width="1.2"/><text x="435" y="259" font-size="12" fill="#c084fc" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">0.11</text><text x="435" y="282" font-size="10" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">⋮（共 512 個）</text><rect x="580" y="120" width="380" height="170" rx="12" fill="#1c2230" stroke="#58a6ff55" stroke-width="1.4"/><text x="600" y="148" font-size="12" fill="#e6edf3" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">逐通道、互不相干</text><text x="600" y="176" font-size="11.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">第 c 個通道只看第 c 張圖：</text><text x="600" y="200" font-size="11.5" fill="#e6edf3" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">GAP_c = (1 / H·W) ΣΣ x[i, j, c]</text><text x="600" y="230" font-size="11" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">實測：逐通道迴圈 = mean(dim=(1,2))</text><text x="600" y="252" font-size="11" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">若對整個張量取平均 → 只剩一個數，</text><text x="600" y="272" font-size="11" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">通道資訊就沒了</text><text x="40" y="372" font-size="12" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">這就是「H×W×C 壓成 C 維」的全部內容：C 次平均，沒有權重、沒有 bias。</text></svg>
<figcaption style="font-size:13.5px;color:#9aa7b4;margin-top:10px;line-height:1.65;text-align:left">4×4 的通道平均成 8.5，512 個通道各做一次就是 512 維；GAP 沒有權重也沒有 bias。</figcaption>
</figure>
<!-- /CNGH-FIGURE:gap-mechanics -->

```text
GAP(X_c) = (1 / H·W) · Σ_i Σ_j x[i, j, c]
輸入 H × W × C  →  輸出 1 × 1 × C
```

三個要點：

- **逐通道獨立**：第 `c` 個通道只看第 `c` 張特徵圖，通道之間不混合（實測：逐通道迴圈等於 `mean(dim=(1,2))`）。
- **通道數不變**：`C` 進、`C` 出；變的只有 `H`、`W`（都變成 1）。
- **沒有參數**：沒有權重、沒有 bias，只有一次平均。

手算一個 4×4 的通道（值 1 到 16）：

```text
(1 + 2 + … + 16) / 16 = 136 / 16 = 8.5
```

所以 `4×4×1 → 1×1×1 = 8.5`；`7×7×512 → 512`。

> 一個容易搞錯的地方：對**整個張量**取平均也會得到「一個數字」，但那會把「哪個通道貢獻的」混掉。
> GAP 的輸出是長度 `C` 的向量，不是純量。

## 3. GAP 與一般 Average Pooling 的差別

| 項目 | 一般 Average Pooling | GAP |
|---|---|---|
| 視窗大小 | 固定小窗（如 2×2） | 整張特徵圖（`H×W`） |
| 是否要 kernel size／stride | 要 | 不需要 |
| 輸出尺寸 | `7×7 → 3×3`（2×2 窗、步幅 2） | `7×7 → 1×1` |
| 可學習參數 | 0 | 0 |
| 常見位置 | CNN 中段 | 分類頭之前 |
| 覆蓋範圍 | 可能漏掉邊緣 | 一定蓋滿整張圖 |

實測（7×7 的值 1–49）：

| 做法 | 輸出 | 平均 |
|---|---|---|
| GAP | 1×1 | **25.00**（49 格全用到） |
| AvgPool2d(2,2) | 3×3 | 21.00（只用 36 格，最後一列一行被丟掉） |
| AvgPool2d(3,3) | 2×2 | 21.00（同樣只用到 36 格） |
| AvgPool2d(7,7) | 1×1 | **25.00** ← 這時它就**是** GAP |

換句話說：**GAP 就是「視窗等於整張圖」的平均池化**，所以它不需要 kernel size 與 stride；而一般池化會因為不整除而漏掉邊緣——這是很多人以為「兩者結果一樣」卻對不上的原因。

## 4. 換成 GAP 之後，參數差多少

| 分類頭 | 參數 |
|---|---|
| Flatten(25,088) → Linear(1000) | 25,089,000 |
| GAP(512) → Linear(1000) | **513,000** |

**差 48.9 倍**，而兩者的輸出完全一樣（1,000 個 logits）。GAP 自己貢獻 0 個參數——它只是把 512 張 `7×7` 各自平均掉。

## 5. 這在真實模型上有多大？

<!-- CNGH-FIGURE:share -->
<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 400" role="img" aria-label="分類頭占整個模型參數的比例：VGG16 89.4%、MobileNetV2 36.7%、ResNet-50 8.0%"><defs><marker id="arg" 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="398.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><text x="280" y="136" font-size="12" fill="#e6edf3" text-anchor="end" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">VGG16</text><rect x="300" y="116" width="464.88" height="28" rx="4" fill="#ff7b72" stroke="none" stroke-width="1" opacity="0.85"/><text x="774.88" y="136" font-size="12.5" fill="#ff7b72" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">89.4%</text><text x="280" y="158" font-size="10" fill="#8b949e" text-anchor="end" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">(123,642,856 / 138,357,544)</text><text x="280" y="210" font-size="12" fill="#e6edf3" text-anchor="end" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">MobileNetV2</text><rect x="300" y="190" width="190.84000000000003" height="28" rx="4" fill="#e0a458" stroke="none" stroke-width="1" opacity="0.85"/><text x="500.84000000000003" y="210" font-size="12.5" fill="#e0a458" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">36.7%</text><text x="280" y="232" font-size="10" fill="#8b949e" text-anchor="end" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">(1,281,000 / 3,487,816)</text><text x="280" y="284" font-size="12" fill="#e6edf3" text-anchor="end" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">ResNet-50</text><rect x="300" y="264" width="41.6" height="28" rx="4" fill="#4ecdc4" stroke="none" stroke-width="1" opacity="0.85"/><text x="351.6" y="284" font-size="12.5" fill="#4ecdc4" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">8.0%</text><text x="280" y="306" font-size="10" fill="#8b949e" text-anchor="end" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">(2,049,000 / 25,557,032)</text><text x="40" y="366" font-size="12" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">VGG16 的 89.4% 幾乎都卡在 4096×4096 那兩層；換成 GAP 頭之後，同一個骨幹的分類頭會掉到 1% 等級。</text></svg>
<figcaption style="font-size:13.5px;color:#9aa7b4;margin-top:10px;line-height:1.65;text-align:left">分類頭占模型總參數的比例。VGG16 有 89.4% 的參數在最後三層全連接；MobileNetV2 是 36.7%；ResNet-50 只剩 8.0%。</figcaption>
</figure>
<!-- /CNGH-FIGURE:share -->

我把三個架構的參數量逐層加起來（MobileNetV2 依官方 block 規格重算）：

<!-- CNGH-FIGURE:vgg16 -->
<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="VGG16 逐階段參數：五個卷積階段共 14,714,688（10.6%），三層全連接共 123,642,856（89.4%），總計 138,357,544"><defs><marker id="arg" 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">VGG16：89.4% 的參數都在最後三層</text><text x="40" y="72" font-size="13" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">同一張 224×224×3 的圖；每塊下面的數字是那一階段的參數量</text><rect x="22" y="118" width="108" height="88" rx="8" fill="#12181f" stroke="#4ecdc4aa" stroke-width="1.5" opacity="0.95"/><text x="76.0" y="142" font-size="10.5" fill="#4ecdc4" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">conv1 ×2</text><text x="76.0" y="166" 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="76.0" y="188" font-size="9" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">38,720</text><line x1="131" y1="162.0" x2="141" y2="162.0" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="144" y="118" width="108" height="88" rx="8" fill="#12181f" stroke="#4ecdc4aa" stroke-width="1.5" opacity="0.95"/><text x="198.0" y="142" font-size="10.5" fill="#4ecdc4" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">conv2 ×2</text><text x="198.0" y="166" font-size="9.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">56×56×128</text><text x="198.0" y="188" font-size="9" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">221,440</text><line x1="253" y1="162.0" x2="263" y2="162.0" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="266" y="118" width="108" height="88" rx="8" fill="#12181f" stroke="#4ecdc4aa" stroke-width="1.5" opacity="0.95"/><text x="320.0" y="142" font-size="10.5" fill="#4ecdc4" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">conv3 ×3</text><text x="320.0" y="166" 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="320.0" y="188" font-size="9" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1,475,328</text><line x1="375" y1="162.0" x2="385" y2="162.0" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="388" y="118" width="108" height="88" rx="8" fill="#12181f" stroke="#4ecdc4aa" stroke-width="1.5" opacity="0.95"/><text x="442.0" y="142" font-size="10.5" fill="#4ecdc4" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">conv4 ×3</text><text x="442.0" y="166" font-size="9.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">14×14×512</text><text x="442.0" y="188" font-size="9" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">5,899,776</text><line x1="497" y1="162.0" x2="507" y2="162.0" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="510" y="118" width="108" height="88" rx="8" fill="#12181f" stroke="#4ecdc4aa" stroke-width="1.5" opacity="0.95"/><text x="564.0" y="142" font-size="10.5" fill="#4ecdc4" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">conv5 ×3</text><text x="564.0" y="166" font-size="9.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">7×7×512</text><text x="564.0" y="188" font-size="9" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">7,079,424</text><line x1="619" y1="162.0" x2="629" y2="162.0" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="632" y="118" width="108" height="88" rx="8" fill="#12181f" stroke="#ff7b72aa" stroke-width="1.5" opacity="0.95"/><text x="686.0" y="142" font-size="10.5" fill="#ff7b72" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">FC 4096</text><text x="686.0" y="166" font-size="9.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">25,088→4,096</text><text x="686.0" y="188" font-size="9" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">102,764,544</text><line x1="741" y1="162.0" x2="751" y2="162.0" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="754" y="118" width="108" height="88" rx="8" fill="#12181f" stroke="#ff7b72aa" stroke-width="1.5" opacity="0.95"/><text x="808.0" y="142" font-size="10.5" fill="#ff7b72" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">FC 4096</text><text x="808.0" y="166" font-size="9.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">4,096→4,096</text><text x="808.0" y="188" font-size="9" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">16,781,312</text><line x1="863" y1="162.0" x2="873" y2="162.0" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><rect x="876" y="118" width="108" height="88" rx="8" fill="#12181f" stroke="#ff7b72aa" stroke-width="1.5" opacity="0.95"/><text x="930.0" y="142" font-size="10.5" fill="#ff7b72" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">FC 1000</text><text x="930.0" y="166" font-size="9.5" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">4,096→1,000</text><text x="930.0" y="188" font-size="9" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">4,097,000</text><line x1="22" y1="104" x2="618" y2="104" stroke="#4ecdc4" stroke-width="1"/><text x="320" y="96" font-size="11" fill="#4ecdc4" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">卷積骨幹 14,714,688（10.6%）</text><line x1="646" y1="104" x2="976" y2="104" stroke="#ff7b72" stroke-width="1"/><text x="811" y="96" font-size="11" fill="#ff7b72" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">分類頭 123,642,856（89.4%）</text><rect x="22" y="254" width="960" height="30" rx="4" fill="#161b22" stroke="#30363d" stroke-width="1"/><rect x="22" y="254" width="102" height="30" rx="4" fill="#4ecdc4" stroke="none" stroke-width="1" opacity="0.85"/><rect x="124" y="254" width="858" height="30" rx="4" fill="#ff7b72" stroke="none" stroke-width="1" opacity="0.85"/><text x="73" y="274" font-size="11" fill="#08141a" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">10.6%</text><text x="553" y="274" font-size="11" fill="#08141a" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">89.4%（123,642,856）</text><rect x="22" y="306" width="960" height="78" rx="10" fill="#1c2230" stroke="#58a6ff55" stroke-width="1.4"/><text x="42" y="334" font-size="12" fill="#e6edf3" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">光是 25,088 → 4,096 那一層就 102,764,544 個參數</text><text x="42" y="360" font-size="11.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">—— 比整個卷積骨幹（14,714,688）的 6.9 倍還多，而且它丢掉所有空間結構。</text><text x="22" y="416" font-size="12" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">把這三層換成 GAP + Linear(512→1000)，分類頭會從 1.24 億降到 51.3 萬。</text></svg>
<figcaption style="font-size:13.5px;color:#9aa7b4;margin-top:10px;line-height:1.65;text-align:left">VGG16 的參數分佈：卷積骨幹只佔 10.6%，其餘 89.4% 都在最後三層全連接；光是 25,088 到 4,096 那層就 1.03 億。</figcaption>
</figure>
<!-- /CNGH-FIGURE:vgg16 -->

| 模型 | 分類頭參數 | 全部參數 | 佔比 |
|---|---|---|---|
| VGG16 | 123,642,856 | 138,357,544 | **89.4%** |
| MobileNetV2（1.0、1000 類） | 1,281,000 | 3,487,816 | **36.7%** |
| ResNet-50 | 2,049,000 | 25,557,032 | **8.0%** |

VGG16 有將近九成參數卡在最後三層全連接（其中兩層是 `4096×4096`）。MobileNetV2 的 36.7% 對應「全連接分類頭約佔 37%」這個常被引用的數字；差額來自是否把 BatchNorm 的 `γ/β` 算進去（+17,056）。

## 6. 好處二：輸入尺寸不再綁死

<!-- CNGH-FIGURE:input-size -->
<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 420" role="img" aria-label="輸入尺寸自由：7×7、8×8、14×14 三種特徵圖經過 GAP 都是 512 維，展平則變成 25088、32768、100352 維"><defs><marker id="arg" 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="418.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">GAP 讓分類頭與輸入尺寸脫鉤</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="110" width="140" height="52" rx="8" fill="#12181f" stroke="#4ecdc4aa" stroke-width="1.4"/><text x="120" y="142" 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×512</text><line x1="194" y1="136" x2="236" y2="136" stroke="#4ecdc4" stroke-width="1.5" marker-end="url(#ar)"/><rect x="240" y="110" width="90" height="52" rx="7" fill="#12181f" stroke="#4ecdc4" stroke-width="1.4"/><text x="285" y="142" font-size="11.5" fill="#4ecdc4" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">GAP</text><line x1="333" y1="136" x2="392" y2="136" stroke="#30363d" stroke-width="1.2"/><rect x="50" y="182" width="140" height="52" rx="8" fill="#12181f" stroke="#4ecdc4aa" stroke-width="1.4"/><text x="120" y="214" font-size="11.5" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">8×8×512</text><line x1="194" y1="208" x2="236" y2="208" stroke="#4ecdc4" stroke-width="1.5" marker-end="url(#ar)"/><rect x="240" y="182" width="90" height="52" rx="7" fill="#12181f" stroke="#4ecdc4" stroke-width="1.4"/><text x="285" y="214" font-size="11.5" fill="#4ecdc4" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">GAP</text><line x1="333" y1="208" x2="392" y2="208" stroke="#30363d" stroke-width="1.2"/><rect x="50" y="254" width="140" height="52" rx="8" fill="#12181f" stroke="#4ecdc4aa" stroke-width="1.4"/><text x="120" y="286" 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×512</text><line x1="194" y1="280" x2="236" y2="280" stroke="#4ecdc4" stroke-width="1.5" marker-end="url(#ar)"/><rect x="240" y="254" width="90" height="52" rx="7" fill="#12181f" stroke="#4ecdc4" stroke-width="1.4"/><text x="285" y="286" font-size="11.5" fill="#4ecdc4" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">GAP</text><line x1="333" y1="280" x2="392" y2="280" stroke="#30363d" stroke-width="1.2"/><line x1="392" y1="136" x2="392" y2="280" stroke="#30363d" stroke-width="1.2"/><line x1="392" y1="208" x2="432" y2="208" stroke="#4ecdc4" stroke-width="1.5" marker-end="url(#ar)"/><rect x="436" y="148" width="150" height="120" rx="9" fill="#12181f" stroke="#4ecdc4" stroke-width="1.6"/><text x="511" y="190" font-size="14" fill="#4ecdc4" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">512 維</text><text x="511" y="214" font-size="10.5" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">三種尺寸</text><text x="511" y="236" font-size="10.5" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">輸出完全相同</text><line x1="590" y1="208" x2="632" y2="208" stroke="#4ecdc4" stroke-width="1.5" marker-end="url(#ar)"/><rect x="636" y="168" width="210" height="80" rx="9" fill="#12181f" stroke="#4ecdc4" stroke-width="1.6"/><text x="741" y="198" font-size="12" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">Linear(512 → 1000)</text><text x="741" y="222" font-size="10" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">一個分類頭吃三種尺寸</text><rect x="50" y="296" width="900" height="84" rx="10" fill="#12181f" stroke="#ff7b7277" stroke-width="1.4"/><text x="70" y="322" font-size="12" fill="#ff7b72" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">改成展平：25,088 / 32,768 / 100,352 —— 長度跟著尺寸變</text><text x="70" y="348" font-size="11.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">同一個 Linear(25,088 → 1000) 只能接第一種；換解析度就得換分類頭，權重全部重來。</text><text x="40" y="404" font-size="12" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">GAP 的輸出長度只取決於通道數 C；展平的長度是 H×W×C。</text></svg>
<figcaption style="font-size:13.5px;color:#9aa7b4;margin-top:10px;line-height:1.65;text-align:left">GAP 的輸出長度只取決於通道數，所以同一個分類頭可以吃不同大小的特徵圖；展平則會直接對不上。</figcaption>
</figure>
<!-- /CNGH-FIGURE:input-size -->

| 特徵圖 | GAP 之後 | 展平之後 |
|---|---|---|
| 7×7×512 | 512 | 25,088 |
| 8×8×512 | 512 | 32,768 |
| 14×14×512 | 512 | 100,352 |

GAP 的輸出長度只取決於通道數 `C`，所以**同一個分類頭可以吃不同解析度的特徵圖**（`7×7`、`14×14`、`28×28` 都通）。展平的長度是 `H×W×C`，換尺寸就得換分類頭、權重全部重來。這也是全卷積網路（FCN）能在任意尺寸影像上跑的原因。

## 7. 好處三：對位移更穩健

<!-- CNGH-FIGURE:shift -->
<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="平移穩健性：同一張特徵圖橫移一格，GAP 的輸出完全不變（25.0），展平後 49 個位置全部改變"><defs><marker id="arg" 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><text x="70" y="108" font-size="11.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">原始</text><rect x="60" y="120" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="73.0" y="136.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="86" y="120" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="99.0" y="136.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">2</text><rect x="112" y="120" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="125.0" y="136.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">3</text><rect x="138" y="120" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="151.0" y="136.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">4</text><rect x="164" y="120" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="177.0" y="136.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">5</text><rect x="190" y="120" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="203.0" y="136.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">6</text><rect x="216" y="120" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="229.0" y="136.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">7</text><rect x="60" y="146" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="73.0" y="162.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">8</text><rect x="86" y="146" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="99.0" y="162.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">9</text><rect x="112" y="146" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="125.0" y="162.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">10</text><rect x="138" y="146" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="151.0" y="162.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">11</text><rect x="164" y="146" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="177.0" y="162.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">12</text><rect x="190" y="146" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="203.0" y="162.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">13</text><rect x="216" y="146" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="229.0" y="162.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">14</text><rect x="60" y="172" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="73.0" y="188.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">15</text><rect x="86" y="172" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="99.0" y="188.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">16</text><rect x="112" y="172" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="125.0" y="188.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">17</text><rect x="138" y="172" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="151.0" y="188.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">18</text><rect x="164" y="172" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="177.0" y="188.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">19</text><rect x="190" y="172" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="203.0" y="188.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">20</text><rect x="216" y="172" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="229.0" y="188.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">21</text><rect x="60" y="198" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="73.0" y="214.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">22</text><rect x="86" y="198" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="99.0" y="214.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">23</text><rect x="112" y="198" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="125.0" y="214.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">24</text><rect x="138" y="198" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="151.0" y="214.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">25</text><rect x="164" y="198" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="177.0" y="214.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">26</text><rect x="190" y="198" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="203.0" y="214.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">27</text><rect x="216" y="198" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="229.0" y="214.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">28</text><rect x="60" y="224" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="73.0" y="240.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">29</text><rect x="86" y="224" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="99.0" y="240.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">30</text><rect x="112" y="224" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="125.0" y="240.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">31</text><rect x="138" y="224" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="151.0" y="240.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">32</text><rect x="164" y="224" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="177.0" y="240.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">33</text><rect x="190" y="224" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="203.0" y="240.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">34</text><rect x="216" y="224" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="229.0" y="240.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">35</text><rect x="60" y="250" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="73.0" y="266.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">36</text><rect x="86" y="250" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="99.0" y="266.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">37</text><rect x="112" y="250" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="125.0" y="266.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">38</text><rect x="138" y="250" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="151.0" y="266.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">39</text><rect x="164" y="250" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="177.0" y="266.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">40</text><rect x="190" y="250" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="203.0" y="266.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">41</text><rect x="216" y="250" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="229.0" y="266.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">42</text><rect x="60" y="276" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="73.0" y="292.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">43</text><rect x="86" y="276" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="99.0" y="292.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">44</text><rect x="112" y="276" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="125.0" y="292.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">45</text><rect x="138" y="276" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="151.0" y="292.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">46</text><rect x="164" y="276" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="177.0" y="292.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">47</text><rect x="190" y="276" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="203.0" y="292.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">48</text><rect x="216" y="276" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="229.0" y="292.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">49</text><text x="330" y="108" font-size="11.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">右移一格</text><rect x="320" y="120" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="333.0" y="136.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">7</text><rect x="346" y="120" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="359.0" y="136.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">1</text><rect x="372" y="120" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="385.0" y="136.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">2</text><rect x="398" y="120" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="411.0" y="136.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">3</text><rect x="424" y="120" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="437.0" y="136.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">4</text><rect x="450" y="120" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="463.0" y="136.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">5</text><rect x="476" y="120" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="489.0" y="136.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">6</text><rect x="320" y="146" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="333.0" y="162.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">14</text><rect x="346" y="146" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="359.0" y="162.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">8</text><rect x="372" y="146" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="385.0" y="162.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">9</text><rect x="398" y="146" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="411.0" y="162.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">10</text><rect x="424" y="146" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="437.0" y="162.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">11</text><rect x="450" y="146" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="463.0" y="162.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">12</text><rect x="476" y="146" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="489.0" y="162.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">13</text><rect x="320" y="172" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="333.0" y="188.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">21</text><rect x="346" y="172" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="359.0" y="188.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">15</text><rect x="372" y="172" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="385.0" y="188.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">16</text><rect x="398" y="172" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="411.0" y="188.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">17</text><rect x="424" y="172" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="437.0" y="188.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">18</text><rect x="450" y="172" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="463.0" y="188.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">19</text><rect x="476" y="172" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="489.0" y="188.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">20</text><rect x="320" y="198" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="333.0" y="214.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">28</text><rect x="346" y="198" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="359.0" y="214.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">22</text><rect x="372" y="198" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="385.0" y="214.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">23</text><rect x="398" y="198" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="411.0" y="214.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">24</text><rect x="424" y="198" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="437.0" y="214.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">25</text><rect x="450" y="198" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="463.0" y="214.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">26</text><rect x="476" y="198" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="489.0" y="214.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">27</text><rect x="320" y="224" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="333.0" y="240.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">35</text><rect x="346" y="224" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="359.0" y="240.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">29</text><rect x="372" y="224" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="385.0" y="240.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">30</text><rect x="398" y="224" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="411.0" y="240.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">31</text><rect x="424" y="224" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="437.0" y="240.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">32</text><rect x="450" y="224" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="463.0" y="240.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">33</text><rect x="476" y="224" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="489.0" y="240.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">34</text><rect x="320" y="250" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="333.0" y="266.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">42</text><rect x="346" y="250" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="359.0" y="266.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">36</text><rect x="372" y="250" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="385.0" y="266.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">37</text><rect x="398" y="250" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="411.0" y="266.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">38</text><rect x="424" y="250" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="437.0" y="266.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">39</text><rect x="450" y="250" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="463.0" y="266.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">40</text><rect x="476" y="250" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="489.0" y="266.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">41</text><rect x="320" y="276" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="333.0" y="292.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">49</text><rect x="346" y="276" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="359.0" y="292.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">43</text><rect x="372" y="276" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="385.0" y="292.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">44</text><rect x="398" y="276" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="411.0" y="292.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">45</text><rect x="424" y="276" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="437.0" y="292.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">46</text><rect x="450" y="276" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="463.0" y="292.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">47</text><rect x="476" y="276" width="26" height="26" rx="4" fill="#12181f" stroke="#30363d" stroke-width="0.9" opacity="0.85"/><text x="489.0" y="292.24" font-size="9" fill="#e6edf3" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">48</text><rect x="560" y="118" width="400" height="96" rx="9" fill="#12181f" stroke="#4ecdc4" stroke-width="1.4"/><text x="580" y="146" font-size="12.5" fill="#4ecdc4" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">GAP：25.0 → 25.0</text><text x="580" y="172" font-size="11" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">差 0.0（平均與位置無關）</text><text x="580" y="196" font-size="10.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">唯一不變的摘要</text><rect x="560" y="236" width="400" height="96" rx="9" fill="#12181f" stroke="#ff7b72" stroke-width="1.4"/><text x="580" y="264" font-size="12.5" fill="#ff7b72" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">Flatten：49 個位置全部改變</text><text x="580" y="290" font-size="11" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">L2 距離 17.15；FC 必須重新學</text><text x="580" y="314" font-size="10.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">位移越多，權重越難學</text><text x="60" y="330" font-size="12" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">這就是「GAP 讓模型不必死記位置」的意思——</text><text x="60" y="352" font-size="12" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">它只問「這個通道整體有多強」。</text><text x="60" y="386" 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">同一張特徵圖往右移一格：GAP 的輸出一個小數點都沒變，展平後的向量 49 個位置全部換了。</figcaption>
</figure>
<!-- /CNGH-FIGURE:shift -->

把同一張 7×7 特徵圖往右移一格（循環位移），實測：

| 做法 | 結果 |
|---|---|
| GAP | 25.0 → **25.0**（差 0.0） |
| Flatten | 49 個位置**全部**改變，L2 距離 17.15 |

GAP 的輸出與位置無關，所以「同一個特徵往旁邊挪幾格」對它沒有任何影響。這對分類特別合理：**你只需要知道「有沒有貓耳朵」，不需要知道它在第幾行第幾列**。
代價也就在這裡：需要精確位置的任務（物件偵測、語意分割、關鍵點估計）不能只用 GAP。

## 8. 好處四：可解釋性——GAP 的權重就是 CAM 的權重

<!-- CNGH-FIGURE:cam -->
<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 390" role="img" aria-label="GAP 的權重就是 CAM 的權重：三個通道各乘上類別權重後相加成 5×5 熱圖，而它的空間平均剛好等於該類別的 logit"><defs><marker id="arg" 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="388.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">GAP 的權重，就是 CAM 的權重</text><text x="40" y="72" font-size="13" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">分類頭的每一列 w[k] ，就是「類別 k 對每個通道的重視程度」</text><text x="60" y="124" font-size="10.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">通道 1</text><rect x="60" y="132" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="78" y="132" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="96" y="132" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="114" y="132" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="132" y="132" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="60" y="150" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="78" y="150" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="96" y="150" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="114" y="150" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="132" y="150" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="60" y="168" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="78" y="168" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="96" y="168" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="114" y="168" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="132" y="168" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="60" y="186" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="78" y="186" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="96" y="186" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="114" y="186" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="132" y="186" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="60" y="204" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="78" y="204" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="96" y="204" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="114" y="204" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="132" y="204" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><text x="200" y="176" font-size="11.5" fill="#4ecdc4" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">w = 0.9</text><text x="60" y="198" font-size="10.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">通道 2</text><rect x="60" y="206" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="78" y="206" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="96" y="206" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="114" y="206" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="132" y="206" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="60" y="224" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="78" y="224" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="96" y="224" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="114" y="224" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="132" y="224" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="60" y="242" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="78" y="242" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="96" y="242" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="114" y="242" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="132" y="242" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="60" y="260" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="78" y="260" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="96" y="260" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="114" y="260" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="132" y="260" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="60" y="278" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="78" y="278" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="96" y="278" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="114" y="278" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="132" y="278" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><text x="200" y="250" font-size="11.5" fill="#58a6ff" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">w = 0.2</text><text x="60" y="272" font-size="10.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">通道 3</text><rect x="60" y="280" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="78" y="280" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="96" y="280" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="114" y="280" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="132" y="280" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="60" y="298" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="78" y="298" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="96" y="298" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="114" y="298" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="132" y="298" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="60" y="316" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="78" y="316" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="96" y="316" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="114" y="316" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="132" y="316" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="60" y="334" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="78" y="334" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="96" y="334" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="114" y="334" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="132" y="334" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="60" y="352" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="78" y="352" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="96" y="352" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="114" y="352" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><rect x="132" y="352" width="18" height="18" rx="3" fill="none" stroke="#30363d" stroke-width="0.7" opacity="0.45"/><text x="200" y="324" font-size="11.5" fill="#c084fc" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">w = −0.6</text><line x1="270" y1="210" x2="330" y2="210" stroke="#8b949e" stroke-width="1.5" marker-end="url(#ar)"/><text x="300" y="190" font-size="10.5" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">加權相加</text><rect x="336" y="120" width="190" height="168" rx="10" fill="#12181f" stroke="#e0a45888" stroke-width="1.4"/><text x="431" y="148" font-size="12" fill="#e6edf3" font-weight="700" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">CAM（貓）</text><rect x="371" y="162" width="20" height="20" rx="3" fill="none" stroke="#e0a458" stroke-width="0.8" opacity="0.9"/><rect x="391" y="162" width="20" height="20" rx="3" fill="none" stroke="#e0a458" stroke-width="0.8" opacity="0.9"/><rect x="411" y="162" width="20" height="20" rx="3" fill="none" stroke="#e0a458" stroke-width="0.8" opacity="0.9"/><rect x="431" y="162" width="20" height="20" rx="3" fill="none" stroke="#e0a458" stroke-width="0.8" opacity="0.9"/><rect x="451" y="162" width="20" height="20" rx="3" fill="none" stroke="#e0a458" stroke-width="0.8" opacity="0.9"/><rect x="371" y="182" width="20" height="20" rx="3" fill="none" stroke="#e0a458" stroke-width="0.8" opacity="0.9"/><rect x="391" y="182" width="20" height="20" rx="3" fill="none" stroke="#e0a458" stroke-width="0.8" opacity="0.9"/><rect x="411" y="182" width="20" height="20" rx="3" fill="none" stroke="#e0a458" stroke-width="0.8" opacity="0.9"/><rect x="431" y="182" width="20" height="20" rx="3" fill="none" stroke="#e0a458" stroke-width="0.8" opacity="0.9"/><rect x="451" y="182" width="20" height="20" rx="3" fill="none" stroke="#e0a458" stroke-width="0.8" opacity="0.9"/><rect x="371" y="202" width="20" height="20" rx="3" fill="none" stroke="#e0a458" stroke-width="0.8" opacity="0.9"/><rect x="391" y="202" width="20" height="20" rx="3" fill="none" stroke="#e0a458" stroke-width="0.8" opacity="0.9"/><rect x="411" y="202" width="20" height="20" rx="3" fill="none" stroke="#e0a458" stroke-width="0.8" opacity="0.9"/><rect x="431" y="202" width="20" height="20" rx="3" fill="none" stroke="#e0a458" stroke-width="0.8" opacity="0.9"/><rect x="451" y="202" width="20" height="20" rx="3" fill="none" stroke="#e0a458" stroke-width="0.8" opacity="0.9"/><rect x="371" y="222" width="20" height="20" rx="3" fill="none" stroke="#e0a458" stroke-width="0.8" opacity="0.9"/><rect x="391" y="222" width="20" height="20" rx="3" fill="none" stroke="#e0a458" stroke-width="0.8" opacity="0.9"/><rect x="411" y="222" width="20" height="20" rx="3" fill="none" stroke="#e0a458" stroke-width="0.8" opacity="0.9"/><rect x="431" y="222" width="20" height="20" rx="3" fill="none" stroke="#e0a458" stroke-width="0.8" opacity="0.9"/><rect x="451" y="222" width="20" height="20" rx="3" fill="none" stroke="#e0a458" stroke-width="0.8" opacity="0.9"/><rect x="371" y="242" width="20" height="20" rx="3" fill="none" stroke="#e0a458" stroke-width="0.8" opacity="0.9"/><rect x="391" y="242" width="20" height="20" rx="3" fill="none" stroke="#e0a458" stroke-width="0.8" opacity="0.9"/><rect x="411" y="242" width="20" height="20" rx="3" fill="none" stroke="#e0a458" stroke-width="0.8" opacity="0.9"/><rect x="431" y="242" width="20" height="20" rx="3" fill="none" stroke="#e0a458" stroke-width="0.8" opacity="0.9"/><rect x="451" y="242" width="20" height="20" rx="3" fill="none" stroke="#e0a458" stroke-width="0.8" opacity="0.9"/><text x="431" y="274" font-size="10" fill="#8b949e" text-anchor="middle" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">5×5 的類別熱圖</text><rect x="560" y="120" width="400" height="96" rx="10" fill="#12181f" stroke="#4ecdc4" stroke-width="1.4"/><text x="580" y="148" font-size="12" fill="#e6edf3" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">logit(貓) = Σ_c w_c × GAP(A_c)</text><text x="580" y="176" font-size="12" fill="#4ecdc4" font-weight="700" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">= CAM(貓) 的空間平均</text><text x="580" y="200" font-size="10.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">實測最大差 2.22e-16（浮點誤差）</text><rect x="560" y="232" width="400" height="84" rx="10" fill="#12181f" stroke="#ff7b7277" stroke-width="1.4"/><text x="580" y="260" font-size="11.5" fill="#e6edf3" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">反過來：把 CAM 放大回原圖尺寸，</text><text x="580" y="284" font-size="11.5" fill="#8b949e" font-family="'Segoe UI','Microsoft JhengHei','Noto Sans TC',sans-serif">就是「模型在看哪裡」的熱圖</text><text x="40" y="356" 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">通道權重乘上特徵圖相加就是 CAM；而它的空間平均正好等於 GAP + Linear 算出的 logit。</figcaption>
</figure>
<!-- /CNGH-FIGURE:cam -->

分類頭是 `Linear(C → K)`，每一列 `w[k]` 就是「類別 `k` 對每個通道的重視程度」。把它乘回每個通道的特徵圖再相加，就是 CAM（class activation map）：

```text
logit(k) = Σ_c w[k, c] · GAP(A_c)
         = (1 / H·W) Σ_i Σ_j Σ_c w[k, c] · A_c[i, j]
         = CAM_k 的空間平均
```

兩邊在數學上完全相等，實測最大差 **2.22e-16**（浮點誤差）。所以：

- **不需要改模型**就能產生「模型在看哪裡」的熱圖：把 `CAM_k` 放大回原圖尺寸即可（Grad-CAM 是同一條思路的推廣）。
- GAP 不是「把資訊丟掉」而已，它讓**通道權重變成可視化的空間權重**。

> 完整推導、2×2 手算例子、以及 Grad-CAM 的關係，見 [CAM：用 GAP 算出模型在看哪裡](/cnn-cam/)。


## 9. 典型現代設計

```text
Conv blocks（可訓練、保留空間）
→ GAP（0 參數，把 H×W 收成 1）
→ Linear(C → num_classes)
→ Softmax
```

實例：

```text
7×7×512 → GlobalAvgPool → 512 → Linear(512 → 1000) → Softmax → 1000 類機率
```

GoogLeNet（2014）是較早用 GAP 取代末端大 FC 的代表；ResNet、MobileNet、EfficientNet 都沿用這個頭。差別只在骨幹怎麼設計。

## 10. 但全連接層沒有消失

| 情境 | 還需要 FC 嗎 |
|---|---|
| 分類（`C → K`） | 需要，但只是一層小 `Linear` |
| 物件偵測／分割／關鍵點 | 不能只靠 GAP（位置資訊不能丟） |
| 度量學習、檢索、多標籤 | 常接輕量 FC 當 embedding／任務頭 |
| 表格資料 | 本來就用 FC |

換句話說：**被淘汰的是「大型」全連接層，不是全連接層本身**。

## 11. 變體與實作注意

| 變體 | 做法 | 什麼時候用 |
|---|---|---|
| GAP | 每通道取平均 | 分類頭的預設 |
| GMP（global max） | 每通道取最大 | 想保留「最強響應」 |
| GAP + GMP 串接 | 兩者拼接成 `2C` | 細粒度分類 |
| GeM（generalized mean） | 介於平均與最大之間 | 檢索、需要可調池化強度 |

實作上三個常踩的點：

1. **維度要對**：`x.mean(dim=(2, 3))`（PyTorch）或 `x.mean(axis=(1, 2))`（NumPy，無 batch 維），不是 `dim=1`。
2. **形狀是 `(N, C, 1, 1)`**：很多實作會 `flatten(1)` 或 `squeeze()` 才接 `Linear`。
3. **`AdaptiveAvgPool2d(1)`** 可以取代固定視窗的 `AvgPool2d`，輸入尺寸隨便換都不報錯。

兩個實測數字（4×4 特徵圖、值 1–16）：GAP = **8.50**、GMP = **16.00**——同樣 0 個參數，留下的是完全不同的資訊。

## 12. 六個常見誤解

| 誤解 | 事實 |
|---|---|
| GAP 有參數 | 沒有；參數在後面的 `Linear` |
| 用了 GAP 就沒有全連接層 | 通常還有一層 `Linear(C → K)`；GAP 取代的是「大 FC」 |
| GAP 與 2×2 平均池化的結果一樣 | 不一樣：池化窗口不整除時會漏掉邊緣（7×7 → 3×3 只用到 36 格） |
| GAP 一定比 Flatten 準 | 不一定；它換來參數與尺寸自由，代價是空間資訊 |
| GAP 可以放網路中間 | 可以（例如 SE 模組的通道注意力），但會把空間壓成 1×1 |
| GAP 讓模型「完全平移不變」 | 對整體平均不變；對局部結構仍敏感（這是好事） |

## 附錄 A 速查表

| 想問的事 | 答案 |
|---|---|
| GAP 公式 | `(1 / H·W) ΣΣ x[i, j, c]` |
| 輸出形狀 | `1 × 1 × C`（= `C` 維向量） |
| 參數 | 0 |
| 4×4 值 1–16 | 8.5 |
| 7×7×512 | → 512 |
| 舊頭參數（25,088→1000） | 25,089,000 |
| GAP 頭參數（512→1000） | 513,000（差 48.9 倍） |
| VGG16 分類頭佔比 | 89.4% |
| MobileNetV2 分類頭佔比 | 36.7% |
| GAP 與 2×2 平均池化 | 不同：視窗覆蓋率不同 |
| GAP 與 CAM | `logit(k)` 就是 `CAM_k` 的空間平均 |
| 什麼時候不能用 | 需要精確位置（偵測／分割／關鍵點） |

## 附錄 B 常見錯誤

1. **對整個張量取平均**：那是 1 個數，不是 `C` 個數。
2. **在 GAP 後面又接 Flatten**：`(N, C, 1, 1)` 攤平就是 `(N, C)`，多此一舉。
3. **維度搞錯**：`mean(dim=1)` 會把通道平均掉，正好相反。
4. **以為 2×2 平均池化可以代替 GAP**：覆蓋率與輸出形狀都不同。
5. **拿 VGG16 的參數量當「CNN 都很大」的例子**：它的問題正是那 89.4% 的分類頭。
6. **以為 GAP 只能用於分類**：通道注意力（SE）、音訊、點雲都用得到。

## 附錄 C 延伸閱讀與離線使用

站內：

- [卷積層 vs 全連接層](/cnn-conv-vs-fc/)——系列總整理：參數、算力與平移特性的對照
- [全連接層的運作](/cnn-fully-connected/)——`Wx + b`、softmax、梯度
- [激活函數與池化層](/cnn-activation-pooling/)——Max／Average Pooling 與 GAP 的取捨
- [卷積層的運作細節](/cnn-conv-details/)——kernel 深度、stride／padding、感受野、空洞卷積
- [圖解 CNN](/cnn-illustrated/)——整體資料流

站外：

- [Learning Deep Features for Discriminative Localization（CAM, CVPR 2016）](https://arxiv.org/abs/1512.04150)
- [PyTorch `AdaptiveAvgPool2d` 文件](https://pytorch.org/docs/stable/generated/torch.nn.AdaptiveAvgPool2d.html)

離線：本頁是**單一 HTML 檔、零外部資源**；原始 Markdown 在同一層。

## 附錄 D 一頁精華（考前 10 分鐘）

| # | 記這一條 |
|---|---|
| 1 | GAP：每個通道對 `H×W` 取平均，輸出 `1×1×C` |
| 2 | 逐通道獨立、通道數不變、0 參數 |
| 3 | 4×4（值 1–16）→ 8.5；7×7×512 → 512 |
| 4 | GAP = 視窗等於整張圖的平均池化；一般池化會漏邊緣 |
| 5 | 舊頭 25,089,000 vs GAP 頭 513,000（48.9 倍） |
| 6 | 分類頭佔比：VGG16 89.4%／MobileNetV2 36.7%／ResNet-50 8.0% |
| 7 | 位移：GAP 差 0.0；Flatten 49 格全變 |
| 8 | `logit(k)` = `CAM_k` 的空間平均（實測差 2.22e-16） |

## 附錄 G 實測原始輸出

`verify_cnn_gap_head.py` 的完整輸出（Python 3.15.1 / NumPy 2.5.4，本機執行）：

```text
------------------------------------------------------------------------
1. the old head: flatten 7x7x512 then one big linear layer
   7*7*512 = 25088
   flatten -> Linear(25,088 -> 1000): 25088000 weights + 1,000 bias = 25089000
------------------------------------------------------------------------
2. the GAP head
   GAP -> 512 -> Linear(512 -> 1000): 512000 weights + 1,000 bias = 513000
   ratio old / gap = 48.9x
   gap head is 0.51M, old head is 25.09M
------------------------------------------------------------------------
3. how much of a real network is the classifier
   MobileNetV2 (1.0, 1000 classes):
     total params            : 3487816
     classifier (1280->1000) : 1281000  = 36.7% of the model
     VGG16 total             : 138357544
     VGG16 classifier        : 123642856  = 89.4%
     ResNet-50 classifier    : 2049000  = 8.0% of 25557032
------------------------------------------------------------------------
4. input size freedom
    7x 7x512 -> GAP 512 維  |  flatten 25088 維
    8x 8x512 -> GAP 512 維  |  flatten 32768 維
   14x14x512 -> GAP 512 維  |  flatten 100352 維
------------------------------------------------------------------------
5. shift the feature map by one pixel
   GAP original : 25.0000
   GAP shifted  : 25.0000  (difference 0.0e+00)
   flatten L2 distance after the same shift: 17.15
   positions that changed in the flattened vector: 49 / 49
------------------------------------------------------------------------
7. GAP vs ordinary average pooling on the same 7x7 map
   7x7 map, GAP                -> 25.00  (shape 1x1, covers all 49 cells)
   AvgPool2d(2,2)  -> shape (3, 3): [[5.0, 7.0, 9.0], [19.0, 21.0, 23.0], [33.0, 35.0, 37.0]]
   mean of that 3x3 map        -> 21.00  (only 36 of 49 cells are covered)
   AvgPool2d(3,3)  -> shape (2, 2): [[9.0, 12.0], [30.0, 33.0]]
   mean of that 2x2 map        -> 21.00  (again 36 of 49)
   AvgPool2d(7,7)  -> shape (1, 1): [[25.00]]  == GAP
   7x7 with k=7 covers every cell, so it *is* GAP
------------------------------------------------------------------------
8. per-channel independence
   A shape (3, 4, 5) -> GAP [ 0.1975 -0.3611 -0.0126]
   per-channel loop matches mean(dim=(1,2)): True
   mean of the whole tensor: -0.0588 (same number, but it loses which channel it came from)
------------------------------------------------------------------------
9. GAP + Linear  ==  spatial mean of the CAM
   logits (GAP then Linear) : [0.972  1.1531 0.4366]
   mean of CAM per class    : [0.972  1.1531 0.4366]
   max abs difference       : 2.22e-16
   -> the classifier weight IS the CAM weight; GAP only averages it
------------------------------------------------------------------------
11. VGG16, stage by stage (the figure on the page)
   conv1  2 conv  -> 112x112x64  params 38720
   conv2  2 conv  -> 56x56x128   params 221440
   conv3  3 conv  -> 28x28x256   params 1475328
   conv4  3 conv  -> 14x14x512   params 5899776
   conv5  3 conv  -> 7x7x512     params 7079424
   flatten -> FC4096  params 102764544
   FC4096  -> FC4096 params 16781312
   FC4096  -> FC1000 params 4097000
   conv total 14714688 | classifier 123642856 (89.4%) | all 138357544
------------------------------------------------------------------------
```

---

*本頁為個人學習筆記整理；圖表為手繪內嵌 SVG，數字全部由附錄 G 的程式算出。*
