新規内容: 自己回帰 (x, y) バッチと get_batch
Agent支援工学分析
前提条件
第1〜3日のトークン化、第36日の交差エントロピー、第48日の順伝播。
学習目標
平坦なトークン配列から因果的な入力・ターゲットの組を作り、get_batch でバッチをサンプリングし、バッチテンソルのサイズを求め、最初の損失を ln(V) と照合する。
手順
今日の新しい内容はこれだけで、残りは第7週の復習です。デコーダは t までのトークンからトークン t+1 を予測します。コンテキスト長 L では L + 1 トークンの窓 chunk = tokens[i : i + L + 1] を切り出し、x = chunk[:-1]、y = chunk[1:] とします。どちらも形状は (L,) です。バッチは B 個のランダムな窓を積み重ね、形状 (B, T) の x と y にします。モデルの出力ロジットは (B, T, V) ですが、F.cross_entropy は (N, V) と (N,) を受け取るので、logits.view(-1, V) と y.view(-1) に平坦化します。学習前はすべてのトークンが等確率なので、最初の損失は ln(V) に近いはずです。
演習と予測記録
1. tokens = [502, 1204, 33, 991, 88, 412]、L = 4、開始位置 0 のとき x と y を書け。窓に L + 1 トークン必要なのはなぜか。
2. get_batch(data, batch_size, block_size, device) を書け。
3. B = 64、T = 1024 のとき、x と y の合計は int64 で何バイトか。int32 ではどうか。
4. 正しく平坦化した cross_entropy の呼び出しを書き、V = 50,257 での最初の損失の期待値を答えよ。
5. y = x(シフトなし)だと何が起きるか。
x は tokens[0:4]、y は tokens[1:5]。1 テンソルあたり 64 * 1024 = 65,536 トークン。最初の損失の期待値は ln(50257)。
1. x = [502, 1204, 33, 991]、y = [1204, 33, 991, 88]。L 個の教師ペア (x_t, y_t) には L 個の入力と、最後のターゲット用にもう 1 トークンが必要。
2. get_batch:
def get_batch(data, batch_size, block_size, device):
ix = torch.randint(len(data) - block_size, (batch_size,))
x = torch.stack([torch.from_numpy(data[i:i + block_size].astype(np.int64)) for i in ix])
y = torch.stack([torch.from_numpy(data[i + 1:i + 1 + block_size].astype(np.int64)) for i in ix])
return x.to(device), y.to(device)3. 1 テンソルあたり 65,536 トークン * 8 バイト = 524,288 バイト。x と y の合計は int64 で 1,048,576 バイト (1.0 MiB)、int32 で 524,288 バイト (512 KiB)。
4. loss = F.cross_entropy(logits.view(-1, logits.size(-1)), y.view(-1))。最初の損失の期待値は ln(50257) ≈ 10.8249。これより大きく外れる、または NaN なら、初期化か形状の不具合を疑う。
5. y = x ではモデルは入力をコピーするだけで損失がほぼ 0 に下がり、次トークン予測を何も学ばない。
シフトした (x, y) の組を導き、動く get_batch を書き、バッチテンソルのサイズを正確に求め、ln(V) で最初の損失を検証できる。
公式ソースリソースと権利証跡
nanoGPT: train.py pretraining loop
該当セクション・タイムスタンプ: train.py: get_batch(split): x = data[i:i+block_size], y = data[i+1:i+1+block_size]
検証バージョンと日付: 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.
CS336 Assignment 1: Basics (Spring 2025 tagged release)
該当セクション・タイムスタンプ: 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)
Simpler code layout, useful complementary reference for minimal PyTorch Transformer.
High-level API focus rather than building from raw tensor fundamentals.