事前学習の中核反復ステップ:順伝播、損失、逆伝播と勾配クリッピング
Agent支援工学分析
前提条件
第49日のデータローダーおよび第50日のAdamWオプティマイザ。
学習目標
標準的な5ステップの訓練反復を実行し、set_to_none=True によるメモリ解放とグローバルL2ノルムクリッピングを適用する。
手順
参照資料: この活動に割り当てられた既存の読解・練習時間内で、CS336 Assignment 1: Basics (Spring 2025 tagged release) の tests/adapters.py: run_gradient_clipping; tests/test_nn_utils.py: test_gradient_clipping を参照し、ローカル課題とのつながりを一つ自分の言葉で説明する。既存の演習と合格基準は維持し、外部資料が開けない場合は本コース独自の説明を使う。各訓練反復において:(1) optimizer.zero_grad(set_to_none=True) で勾配を解放;(2) logits, loss = model(x, y) で順伝播とクロスエントロピーを算出;(3) loss.backward() で勾配を累積;(4) torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0) でL2ノルムを1.0以下に抑制;(5) optimizer.step() で重みを更新する。勾配クリッピングが外れ値バッチによる重み崩壊を防ぐ機序を考察せよ。
演習と予測記録
1. 1ステップの訓練反復を実行する完全なPythonコードを示せ。
2. クリッピング前の全パラメータ勾配ノルムが ||g||_2 = 5.0、max_norm = 1.0 の場合、各勾配に乗じられる縮小率はいくらか?
3. なぜ optimizer.zero_grad(set_to_none=True) は通常の zero_grad() よりも明確に優れているのか?
縮小率は min(1.0, max_norm / ||g||_2)。ゼロ値書き込みとポインタ解放のメモリ帯域幅の差に着目します。
1. 訓練ステップコード:
optimizer.zero_grad(set_to_none=True)
logits, loss = model(x, y)
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
optimizer.step()2. 縮小率:1.0 / 5.0 = 0.2。全パラメータ勾配テンソルに 0.2 が掛けられ、L2ノルムが厳密に1.0以下に抑えられる。
3.
set_to_none=True はバッファにゼロを上書きするのではなく、勾配テンソルのメモリ領域そのものを解放するため、GPUメモリ帯域幅を節約し、以後の自動微分割り当てを高速化する。
標準的な5ステップ訓練コードを記述し、クリッピング倍率を正確に計算し、set_to_none=True のメモリ的利点を説明できること。
公式ソースリソースと権利証跡
CS336 Assignment 1: Basics (Spring 2025 tagged release)
該当セクション・タイムスタンプ: tests/adapters.py: run_gradient_clipping; tests/test_nn_utils.py: test_gradient_clipping
検証バージョンと日付: 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)
Simpler code layout, useful complementary reference for minimal PyTorch Transformer.
High-level API focus rather than building from raw tensor fundamentals.
nanoGPT: train.py pretraining loop
該当セクション・タイムスタンプ: train.py: training loop (while True): forward under ctx, scaler.scale(loss).backward(), clip_grad_norm_(grad_clip), scaler.step(optimizer)
検証バージョンと日付: 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)
Heavy abstraction layer wrapping distributed training; nanoGPT exposes raw PyTorch loops without framework overhead.
High-performance enterprise distributed framework; unsuitable for local educational first-principles comprehension.