新內容:自迴歸 (x, y) 批次與 get_batch
Agent 輔助工程分析
先修要求
第 1-3 天的分詞、第 36 天的交叉熵、第 48 天的前向傳播。
學習目標
從一維 token 陣列建構因果輸入-目標對,用 get_batch 取樣批次,計算批次張量的大小,並用 ln(V) 核對第一個損失。
實驗指南
這是今天唯一的新內容,其餘時間複習第 7 週。解碼器用位置 t 及之前的 token 預測 token t+1。上下文長度為 L 時,切出 L + 1 個 token 的視窗 chunk = tokens[i : i + L + 1],再令 x = chunk[:-1]、y = chunk[1:],兩者形狀都是 (L,)。一個批次把 B 個隨機視窗堆疊成形狀為 (B, T) 的 x 和 y。模型輸出 logits 的形狀是 (B, T, V),而 F.cross_entropy 需要 (N, V) 和 (N,),因此用 logits.view(-1, V) 和 y.view(-1) 攤平。訓練開始前所有 token 等機率,第一個損失應接近 ln(V)。
實戰與預測記錄
1. tokens = [502, 1204, 33, 991, 88, 412],L = 4,從索引 0 開始,寫出 x 和 y。為什麼視窗需要 L + 1 個 token?
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]。每個張量含 64 * 1024 = 65,536 個 token。第一個損失的期望值是 ln(50257)。
1. x = [502, 1204, 33, 991],y = [1204, 33, 991, 88]。L 個監督對 (x_t, y_t) 需要 L 個輸入,再加 1 個 token 作為最後一個目標。
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. 每個張量 65,536 個 token * 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,卻完全學不到下一個 token 的預測。
能推導位移後的 (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.