自回歸採樣機制:貪心解碼與溫度縮放 (Temperature)
Agent 輔助工程分析
前置要求
第 48 日 Decoder-Only 前向傳播與第 41 日 Softmax 基礎。
學習目標
建立下一詞預測分佈公式 p_i = softmax(z_i / T),闡明溫度 T 如何精確調控輸出分佈的信息熵。
操作指引
給定 logits z = [2.0, 1.0, 0.0]:(1) 貪心解碼取 argmax 為索引 0;(2) T=1.0 時概率為 [0.665, 0.245, 0.090];(3) 計算 T=0.5 下的概率 p = [0.867, 0.117, 0.016];(4) 證明 T->0 退化為貪心 argmax,T->inf 趨於均勻分佈。
自主實作與記錄
1. 計算 z = [2.0, 1.0, 0.0] 在 T=2.0 時的 Softmax 概率分佈。
2. 為什麼過低的溫度(如 T=0.2)在減少幻覺的同時更容易導致複讀機式死循環?
3. 若在代碼中直接傳入 T=0.0 進行 z / T 計算,會拋出什麼運行時異常?
z / 2.0 = [1.0, 0.5, 0.0]。exp([1, 0.5, 0]) = [2.718, 1.649, 1.0],總和為 5.367。
T=2.0 時概率為 [0.506, 0.307, 0.186]。低溫將概率質量過度壓縮在單一最高 Logit 上,極大降低了探索多樣性,遇到局部極大值時陷入重複循環。T=0.0 會直接引發 ZeroDivisionError,工程實現中必須特殊判斷並將 T=0 路由至 torch.argmax。
能準確手算溫度縮放分佈,深入解釋創造性與死循環之間的博弈平衡。
權威來源資源與權利證明
The Curious Case of Neural Text Degeneration
對應章節 / 時間戳: Section 2.1 Text Generation Decoding Strategies and Section 3.3 Sampling with Temperature
驗證版本與日期: ICLR 2020 / arXiv:1904.09751v2 (Verified: 2026-10-30)
Primary source introducing Nucleus (Top-p) sampling and analyzing the failure of greedy decoding (loops, repetition).
Softmax probabilities, cumulative probability distributions from Days 25-27 and 41-42.
Top-p truncation algorithms and worked probability cutoffs are completely explained in the lesson.
對比候選資源與選型分析 (2)
Introduces top-k sampling; Holtzman et al. dynamic nucleus threshold is compared directly against top-k.
Practical library guide; Holtzman provides the foundational mathematical reasoning.
nanoGPT: sample.py autoregressive generation script
對應章節 / 時間戳: sample.py: temperature and top_k settings passed to model.generate (model.py: GPT.generate)
驗證版本與日期: Git master (Verified: 2026-10-30)
Compact historical code reference for the named implementation functions; CS336 spring2025 provides the lead component exercises. Do not infer top-p, atomic persistence or cloud budget enforcement from nanoGPT.
PyTorch inference, torch.multinomial, and Day 48 decoder-only forward pass.
The generation loop is implemented in runnable local scripts.
對比候選資源與選型分析 (2)
C++ implementation with high performance; nanoGPT is readable Python matched to our curriculum.
Encoder-decoder model; nanoGPT decoder-only autoregressive loop is the direct fit for modern LLMs.