新内容:自回归 (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.