52 / 90

第52日:自己回帰テキスト生成:温度調節、Top-kと核サンプリング(Top-p)

貪欲デコードの無限ループ退行を分析し、温度スケーリング z / T の数式を導き、Top-p 核サンプリングの累積確率切断を実装し、乱数シードの再現性を検証する。

モデル・事前学習とLoRA 480分の集中学習(8時間)
01 120 分

自己回帰サンプリング機構:貪欲法と温度スケーリング(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 に渡すと、どのような実行時エラーが発生するか?

独立習得基準

温度スケーリングを手計算し、多様性とループのトレードオフを解説できること。

公式ソースリソースと権利証跡

linking arXiv non-exclusive license / Open Access

The Curious Case of Neural Text Degeneration

著者・提供機関: Ari Holtzman, Jan Buys, Li Du, Maxwell Forbes, Yejin Choi | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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.

Hugging Face Generation Strategies Guide ↗ compared_alternative [Apache 2.0]

Practical library guide; Holtzman provides the foundational mathematical reasoning.

linking MIT

nanoGPT: sample.py autoregressive generation script

著者・提供機関: Andrej Karpathy | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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)
llama.cpp sampling implementation ↗ compared_alternative [MIT]

C++ implementation with high performance; nanoGPT is readable Python matched to our curriculum.

PyTorch text generation tutorial ↗ compared_alternative [Modified BSD]

Encoder-decoder model; nanoGPT decoder-only autoregressive loop is the direct fit for modern LLMs.

02 120 分

語彙切り捨て戦略:Top-kと核サンプリング(Top-p / Nucleus)

Agent支援工学分析

前提条件

活動01。

学習目標

Top-kとTop-p切り捨てを実装し、Top-pが分布の急峻さに動的に適応する仕組みを論じる。

手順

トークン A、B、C、D、E に対する降順ソート済みの確率 p = [0.40, 0.30, 0.15, 0.10, 0.05] が与えられたとする。(1) k=2 のとき {A, B} に切り詰め、再正規化して p = [0.4/0.7, 0.3/0.7] = [0.571, 0.429] とせよ。(2) 核サンプリングの閾値 p_cutoff=0.80 のとき、累積和は A: 0.40、B: 0.70、C: 0.85(>0.80)となるため、{A, B, C} を保持し、sum=0.85 で再正規化して [0.471, 0.353, 0.176] とせよ。

演習と予測記録

1. Top-p で p=0.60 とした場合、保持されるトークンはどれか?
2. 分布が急峻な場合(例:p = [0.98, 0.01, 0.01])、Top-p が解決する Top-k の欠点は何か?
3. 核サンプリングのアルゴリズムの手順を3行で述べよ。

独立習得基準

核サンプリングの手計算を行い、動的語彙適応の利点を説明できること。

公式ソースリソースと権利証跡

linking arXiv non-exclusive license / Open Access

The Curious Case of Neural Text Degeneration

著者・提供機関: Ari Holtzman, Jan Buys, Li Du, Maxwell Forbes, Yejin Choi | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Section 3.1 Nucleus Sampling (with Section 2.1 Text Generation Decoding Strategies)

検証バージョンと日付: 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.

Hugging Face Generation Strategies Guide ↗ compared_alternative [Apache 2.0]

Practical library guide; Holtzman provides the foundational mathematical reasoning.

linking MIT

nanoGPT: sample.py autoregressive generation script

著者・提供機関: Andrej Karpathy | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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)
llama.cpp sampling implementation ↗ compared_alternative [MIT]

C++ implementation with high performance; nanoGPT is readable Python matched to our curriculum.

PyTorch text generation tutorial ↗ compared_alternative [Modified BSD]

Encoder-decoder model; nanoGPT decoder-only autoregressive loop is the direct fit for modern LLMs.

03 120 分

繰り返しペナルティ(Repetition Penalty)と乱数シードの再現性

Agent支援工学分析

前提条件

活動 52-01 および 52-02。

学習目標

既出トークンにペナルティ係数 theta を適用し、乱数シードの固定により生成の決定性を保証する。

手順

文脈中に既に現れているトークン i について、logit z_i > 0 なら z_i = z_i / theta、z_i < 0 なら z_i = z_i * theta とする(theta > 1.0、例:1.2)。torch.manual_seed(42) によって、実行をまたいで生成されるトークン列が同一になることを検証せよ。

演習と予測記録

1. トークン 0 と 1 が既に出現しているとき、logits z = [3.0, -2.0, 1.0] にペナルティ theta=1.2 を適用せよ。
2. なぜ正の logit には除算を、負の logit には乗算を用いるのか?
3. seed=42 のとき生成されるトークンがビット単位で同一になることを示す2行のテストを書け。

独立習得基準

ペナルティ計算を正しく適用し、シード固定による決定性を証明できること。

公式ソースリソースと権利証跡

linking arXiv non-exclusive license / Open Access

The Curious Case of Neural Text Degeneration

著者・提供機関: Ari Holtzman, Jan Buys, Li Du, Maxwell Forbes, Yejin Choi | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Section 5.3 Repetition and Appendix B Example Generations

検証バージョンと日付: 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.

Hugging Face Generation Strategies Guide ↗ compared_alternative [Apache 2.0]

Practical library guide; Holtzman provides the foundational mathematical reasoning.

linking MIT

nanoGPT: sample.py autoregressive generation script

著者・提供機関: Andrej Karpathy | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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)
llama.cpp sampling implementation ↗ compared_alternative [MIT]

C++ implementation with high performance; nanoGPT is readable Python matched to our curriculum.

PyTorch text generation tutorial ↗ compared_alternative [Modified BSD]

Encoder-decoder model; nanoGPT decoder-only autoregressive loop is the direct fit for modern LLMs.

04 120 分

独立サンプリング関数の実装と完全性検証

完全自立閉本テスト

前提条件

活動01-03の完了。

学習目標

温度、Top-k、Top-p、自己回帰ループを備えた完全な generate() 関数を自力実装する。

手順

def generate(model, idx, max_new_tokens, temperature=1.0, top_k=None, top_p=None) を実装せよ。生成された出力が期待される形状 (B, T + max_new_tokens) と一致することを assert で検証せよ。

演習と予測記録

1. 完全な generate コードを示せ。
2. top_k=1 が貪欲法と等価であることを検証せよ。
3. 文脈長がモデルの block_size を超える場合の必須ガードは何か?

独立習得基準

完全な generate 関数を自作し、貪欲法等価性テストにパスすること。

公式ソースリソースと権利証跡

linking MIT

nanoGPT: sample.py autoregressive generation script

著者・提供機関: Andrej Karpathy | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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)
llama.cpp sampling implementation ↗ compared_alternative [MIT]

C++ implementation with high performance; nanoGPT is readable Python matched to our curriculum.

PyTorch text generation tutorial ↗ compared_alternative [Modified BSD]

Encoder-decoder model; nanoGPT decoder-only autoregressive loop is the direct fit for modern LLMs.

linking arXiv non-exclusive license / Open Access

The Curious Case of Neural Text Degeneration

著者・提供機関: Ari Holtzman, Jan Buys, Li Du, Maxwell Forbes, Yejin Choi | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Section 3.1 Nucleus Sampling (with Section 2.1 Text Generation Decoding Strategies)

検証バージョンと日付: 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.

Hugging Face Generation Strategies Guide ↗ compared_alternative [Apache 2.0]

Practical library guide; Holtzman provides the foundational mathematical reasoning.