50 / 90

第50日:最適化メカニクス:AdamWの分離型重み減衰と学習率スケジューリング

AdamWのパラメータ更新式を導出し、分離型重み減衰とL2正則化の本質的相違を対比し、線形ウォームアップとコサイン減衰を実装する。

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

AdamW 分離型重み減衰の数学的導出

Agent支援工学分析

前提条件

第31-33日の勾配降下法とモメンタム;L2正則化の概念。

学習目標

AdamWの更新式 w_{t+1} = w_t (1 - eta * lambda) - eta * m_hat_t / (sqrt(v_hat_t) + eps) を導出し、従来のAdamにおけるL2正則化の歪み原因を解明する。

手順

標準的なAdamにL2正則化を適用すると、勾配に lambda * w が加算される。適応的な2次モーメント sqrt(v_t) による除算により、過去の勾配が大きい重みは減衰が抑制され、勾配が小さい重みは過剰に減衰される。Loshchilov & Hutter (2019) は重み減衰を勾配スケーリングから分離するAdamWを提案した。w_0 = 1.0, lambda = 0.1, eta = 0.01, g_1 = 0.5, beta_1 = 0.9, beta_2 = 0.999, eps = 1e-8 における第1ステップの更新を手計算せよ。

演習と予測記録

1. L2正則化付きAdamとAdamWの更新式の差異を示せ。
2. 第1ステップにおける m_hat_1, v_hat_1 および更新後の重み w_1 を手計算せよ。
3. 分離型重み減衰がTransformerの汎化性能を著しく向上させる理由は何か?

独立習得基準

両更新式を導出し、第1ステップの数値計算を手計算で検証できること。

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

linking arXiv non-exclusive license / Open Access

Decoupled Weight Decay Regularization

著者・提供機関: Ilya Loshchilov, Frank Hutter | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Section 2 Decoupling the Weight Decay from the Gradient-Based Update (Algorithm 2, AdamW)

検証バージョンと日付: ICLR 2019 / arXiv:1711.05101v3 (Verified: 2026-10-30)

選定理由:

Foundational primary literature defining AdamW, demonstrating that L2 regularization does not equal decoupled weight decay in adaptive gradient algorithms.

ソース前提知識:

Stochastic gradient descent, L2 regularization, and momentum concepts from Days 31-33.

安全フォールバック(オフライン回復性):

All update equations and mathematical proofs are self-contained in the curriculum lessons.

比較対象候補リソースと選定評価 (2)

Original Adam algorithm implementing L2 regularization via gradient addition, leading to sub-optimal generalization compared to decoupled weight decay.

PyTorch torch.optim.AdamW documentation ↗ compared_alternative [Modified BSD]

Standard library reference; Loshchilov & Hutter provides theoretical mathematical derivations and empirical justification.

linking MIT

nanoGPT: train.py pretraining loop

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

該当セクション・タイムスタンプ: train.py: optimizer = model.configure_optimizers(weight_decay, learning_rate, (beta1, beta2), device_type)

検証バージョンと日付: Git master commit 71c3905 (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.

ソース前提知識:

Python, PyTorch, and Day 46-48 Transformer model architecture.

安全フォールバック(オフライン回復性):

Runnable pretraining scripts and step-by-step tensor mechanics are fully self-contained in the curriculum lessons.

比較対象候補リソースと選定評価 (2)
Hugging Face Accelerate / Trainer ↗ compared_alternative [Apache-2.0]

Heavy abstraction layer wrapping distributed training; nanoGPT exposes raw PyTorch loops without framework overhead.

Megatron-LM (NVIDIA) ↗ compared_alternative [BSD-3-Clause]

High-performance enterprise distributed framework; unsuitable for local educational first-principles comprehension.

02 120 分

パラメータグループ分離:2Dテンソルと1Dテンソルの減衰規程

コア導出の自立実施

前提条件

活動01および第46-48日のTransformer実装知識。

学習目標

モデルパラメータを重み減衰対象(2次元行列)と非対象(1次元バイアス・正規化係数)に分離し、最適化インスタンスを構成する。

手順

参照資料: この活動に割り当てられた既存の読解・練習時間内で、CS336 Assignment 1: Basics (Spring 2025 tagged release) の tests/adapters.py: get_adamw_cls; tests/test_optimizer.py: test_adamw を参照し、ローカル課題とのつながりを一つ自分の言葉で説明する。既存の演習と合格基準は維持し、外部資料が開けない場合は本コース独自の説明を使う。LLMの事前学習(nanoGPT、LLaMA等)において、重み減衰は2次元以上の重み行列(線形層、注意機構射影、FFN層)のみに適用する。1次元バイアスおよび正規化スケールベクトル gamma(LayerNorm / RMSNorm)は減衰させてはならない。gamma を減衰させると0に近づき、活性化の分散が不自然に増大して訓練が発散する。

演習と予測記録

1. configure_optimizers(model, weight_decay, learning_rate, betas, device) のPythonコードを示せ。
2. Linear層(重み (768, 768)、バイアス (768))とRMSNorm(スケール (768))を持つモデルにおいて、減衰されるパラメータを指定せよ。
3. RMSNormのスケールベクトル gamma に誤って重み減衰を適用した場合の障害を述べよ。

独立習得基準

テンソル次元に基づいてパラメータ分離を実装し、1Dパラメータを除外する論理的根拠を提示できること。

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

linking MIT

CS336 Assignment 1: Basics (Spring 2025 tagged release)

著者・提供機関: Stanford University CS336 Staff | オリジナル出所URL ↗

該当セクション・タイムスタンプ: tests/adapters.py: get_adamw_cls; tests/test_optimizer.py: test_adamw

検証バージョンと日付: spring2025 tag; README, tests and MIT LICENSE checked 2026-11-01

選定理由:

Lead assignment-driven source for pretraining component practice and tests. nanoGPT remains a compact historical code-reading companion, not the current recommended training curriculum.

ソース前提知識:

Fluent Python, PyTorch, and linear algebra

安全フォールバック(オフライン回復性):

Local self-contained pretraining scripts run on standard local CPUs or budget GPUs without Stanford cluster dependency.

比較対象候補リソースと選定評価 (2)
Karpathy nanoGPT ↗ compared_alternative [MIT]

Simpler code layout, useful complementary reference for minimal PyTorch Transformer.

Hugging Face Transformers Course ↗ compared_alternative [Apache-2.0]

High-level API focus rather than building from raw tensor fundamentals.

linking MIT

nanoGPT: train.py pretraining loop

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

該当セクション・タイムスタンプ: train.py: optimizer = model.configure_optimizers(weight_decay, learning_rate, (beta1, beta2), device_type); the 2D-decay / 1D-no-decay split is in model.py GPT.configure_optimizers

検証バージョンと日付: Git master commit 71c3905 (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.

ソース前提知識:

Python, PyTorch, and Day 46-48 Transformer model architecture.

安全フォールバック(オフライン回復性):

Runnable pretraining scripts and step-by-step tensor mechanics are fully self-contained in the curriculum lessons.

比較対象候補リソースと選定評価 (2)
Hugging Face Accelerate / Trainer ↗ compared_alternative [Apache-2.0]

Heavy abstraction layer wrapping distributed training; nanoGPT exposes raw PyTorch loops without framework overhead.

Megatron-LM (NVIDIA) ↗ compared_alternative [BSD-3-Clause]

High-performance enterprise distributed framework; unsuitable for local educational first-principles comprehension.

03 120 分

学習率スケジューリング:線形ウォームアップとコサイン減衰

Agent支援工学分析

前提条件

活動01および02。

学習目標

線形ウォームアップとコサインアニーリングを組み合わせた学習率関数 get_lr(it) を実装し、各マイルストーンでの数値を検証する。

手順

参照資料: この活動に割り当てられた既存の読解・練習時間内で、CS336 Assignment 1: Basics (Spring 2025 tagged release) の tests/adapters.py: run_get_lr_cosine_schedule; tests/test_optimizer.py: test_get_lr_cosine_schedule を参照し、ローカル課題とのつながりを一つ自分の言葉で説明する。既存の演習と合格基準は維持し、外部資料が開けない場合は本コース独自の説明を使う。事前学習では初期の不安定性を防ぐために最初の T_warmup ステップで学習率を0から eta_max へ線形に増加させる。その後 T_max ステップまでコサイン曲線に従って eta_min = 0.1 * eta_max まで滑らかに減衰させ、以降は eta_min を維持する。

演習と予測記録

1. eta(t) の区分的数学関数式を示せ。
2. eta_max = 6e-4, eta_min = 6e-5, T_warmup = 1000, T_max = 5000 において、t=0, 1000, 3000, 5000 の正確な学習率を求めよ。
3. なぜLLM事前学習において滑らかなコサイン減衰が段階的ステップ減衰より優れているのか?

独立習得基準

各マイルストーンの学習率を手計算し、アサーションを通過するコサインスケジューラを実装できること。

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

linking MIT

CS336 Assignment 1: Basics (Spring 2025 tagged release)

著者・提供機関: Stanford University CS336 Staff | オリジナル出所URL ↗

該当セクション・タイムスタンプ: tests/adapters.py: run_get_lr_cosine_schedule; tests/test_optimizer.py: test_get_lr_cosine_schedule

検証バージョンと日付: spring2025 tag; README, tests and MIT LICENSE checked 2026-11-01

選定理由:

Lead assignment-driven source for pretraining component practice and tests. nanoGPT remains a compact historical code-reading companion, not the current recommended training curriculum.

ソース前提知識:

Fluent Python, PyTorch, and linear algebra

安全フォールバック(オフライン回復性):

Local self-contained pretraining scripts run on standard local CPUs or budget GPUs without Stanford cluster dependency.

比較対象候補リソースと選定評価 (2)
Karpathy nanoGPT ↗ compared_alternative [MIT]

Simpler code layout, useful complementary reference for minimal PyTorch Transformer.

Hugging Face Transformers Course ↗ compared_alternative [Apache-2.0]

High-level API focus rather than building from raw tensor fundamentals.

linking arXiv non-exclusive license / Open Access

Decoupled Weight Decay Regularization

著者・提供機関: Ilya Loshchilov, Frank Hutter | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Appendix B.2 Adam with Cosine Annealing and Warm Restarts

検証バージョンと日付: ICLR 2019 / arXiv:1711.05101v3 (Verified: 2026-10-30)

選定理由:

Foundational primary literature defining AdamW, demonstrating that L2 regularization does not equal decoupled weight decay in adaptive gradient algorithms.

ソース前提知識:

Stochastic gradient descent, L2 regularization, and momentum concepts from Days 31-33.

安全フォールバック(オフライン回復性):

All update equations and mathematical proofs are self-contained in the curriculum lessons.

比較対象候補リソースと選定評価 (2)

Original Adam algorithm implementing L2 regularization via gradient addition, leading to sub-optimal generalization compared to decoupled weight decay.

PyTorch torch.optim.AdamW documentation ↗ compared_alternative [Modified BSD]

Standard library reference; Loshchilov & Hutter provides theoretical mathematical derivations and empirical justification.

linking MIT

nanoGPT: train.py pretraining loop

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

該当セクション・タイムスタンプ: train.py: get_lr(it): linear warmup over warmup_iters, cosine decay to min_lr by lr_decay_iters

検証バージョンと日付: Git master commit 71c3905 (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.

ソース前提知識:

Python, PyTorch, and Day 46-48 Transformer model architecture.

安全フォールバック(オフライン回復性):

Runnable pretraining scripts and step-by-step tensor mechanics are fully self-contained in the curriculum lessons.

比較対象候補リソースと選定評価 (2)
Hugging Face Accelerate / Trainer ↗ compared_alternative [Apache-2.0]

Heavy abstraction layer wrapping distributed training; nanoGPT exposes raw PyTorch loops without framework overhead.

Megatron-LM (NVIDIA) ↗ compared_alternative [BSD-3-Clause]

High-performance enterprise distributed framework; unsuitable for local educational first-principles comprehension.

04 120 分

独立オプティマイザ構築と数値検証テスト

完全自立閉本テスト

前提条件

活動01〜03の完了。

学習目標

パラメータグループ分離と学習率スケジューラを含む完全なオプティマイザ設定コードを自立作成し、減衰状態を検証する。

手順

Transformerモデルに対するAdamW初期化スクリプトを作成し、betas=(0.9, 0.95)、eps=1e-8、weight_decay=0.1 を適用し、パラメータグループの健全性をアサーションで検証せよ。

演習と予測記録

1. 完全なパラメータグループ分離コードを示せ。
2. ndim < 2 のパラメータに weight_decay > 0 が適用されていないことを検証するassert文を示せ。
3. なぜ言語モデルの事前学習において beta_2 を従来の 0.999 ではなく 0.95 に設定することが多いのか?

独立習得基準

パラメータグループのアサーションをパスする最適化コードを提示し、beta_2=0.95 の工学的意義を論理的に解説できること。

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

linking arXiv non-exclusive license / Open Access

Decoupled Weight Decay Regularization

著者・提供機関: Ilya Loshchilov, Frank Hutter | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Section 2 Decoupling the Weight Decay from the Gradient-Based Update (Algorithm 2, AdamW)

検証バージョンと日付: ICLR 2019 / arXiv:1711.05101v3 (Verified: 2026-10-30)

選定理由:

Foundational primary literature defining AdamW, demonstrating that L2 regularization does not equal decoupled weight decay in adaptive gradient algorithms.

ソース前提知識:

Stochastic gradient descent, L2 regularization, and momentum concepts from Days 31-33.

安全フォールバック(オフライン回復性):

All update equations and mathematical proofs are self-contained in the curriculum lessons.

比較対象候補リソースと選定評価 (2)

Original Adam algorithm implementing L2 regularization via gradient addition, leading to sub-optimal generalization compared to decoupled weight decay.

PyTorch torch.optim.AdamW documentation ↗ compared_alternative [Modified BSD]

Standard library reference; Loshchilov & Hutter provides theoretical mathematical derivations and empirical justification.

linking MIT

CS336 Assignment 1: Basics (Spring 2025 tagged release)

著者・提供機関: Stanford University CS336 Staff | オリジナル出所URL ↗

該当セクション・タイムスタンプ: README setup; tests/adapters.py; tests/test_optimizer.py; tests/test_nn_utils.py; tests/test_serialization.py

検証バージョンと日付: spring2025 tag; README, tests and MIT LICENSE checked 2026-11-01

選定理由:

Lead assignment-driven source for pretraining component practice and tests. nanoGPT remains a compact historical code-reading companion, not the current recommended training curriculum.

ソース前提知識:

Fluent Python, PyTorch, and linear algebra

安全フォールバック(オフライン回復性):

Local self-contained pretraining scripts run on standard local CPUs or budget GPUs without Stanford cluster dependency.

比較対象候補リソースと選定評価 (2)
Karpathy nanoGPT ↗ compared_alternative [MIT]

Simpler code layout, useful complementary reference for minimal PyTorch Transformer.

Hugging Face Transformers Course ↗ compared_alternative [Apache-2.0]

High-level API focus rather than building from raw tensor fundamentals.