自己回帰サンプリング機構:貪欲法と温度スケーリング(Temperature)
Agent支援工学分析
前提条件
第48日のDecoder-Only順伝播と第41日のSoftmax。
学習目標
次トークン予測分布 p_i = softmax(z_i / T) を定式化し、温度 T がエントロピーを制御する仕組みを説明する。
手順
logits z = [2.0, 1.0, 0.0] について:(1) 貪欲法(greedy)による argmax を求めよ(インデックス 0)。(2) T=1.0 での確率を計算せよ:exp([2,1,0]) = [7.389, 2.718, 1.0]、sum=11.107 -> p = [0.665, 0.245, 0.090]。(3) T=0.5 で計算せよ:z/0.5 = [4.0, 2.0, 0.0]、exp([4,2,0]) = [54.598, 7.389, 1.0]、sum=62.987 -> 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]、sum=5.367。
T=2.0 の確率:[0.506, 0.307, 0.186]。低温は最大Logitに確率を過度に集中させ多様性を奪い、局所ループに陥らせる。T=0.0 は ZeroDivisionError を引き起こすため、コード側で 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.