统计二元词元对:从词元流到计数表
Agent 辅助工程检查
前置要求
完成第 37 天。如受阻,使用第 35 天的回退链接复习概率与 Python。
学习目标
将词元序列转换为二元计数表 C(w_{i-1}, w_i),区分上下文总频次与配对频次,并将每个计数值与手动追踪对照验证(支持 Unicode 字符与空输入边界处理)。
操作指引
二元语法(bigram)指相邻的两个连续词元。必做工作:在词元流 'the cat sat the cat ran the dog sat'(模拟由三行训练数据合并成的 9 词元流,遵循 text_tools 的规范)上滑动长度为 2 的窗口,使用 Counter(zip(words, words[1:])) 统计所有相邻配对。选做工作:在验证集 val 划分上重复此过程并比较数据稀疏度。注意:当输入为空文本 '' 时配对计数为空集,Unicode 字符串如 'é猫é' 亦能完全按相同逻辑滑动计数。
自主演练与记录
在运行任何代码之前,手工写出 8 个相邻词元对以及完整计数表。然后用同样方法计算 'banana' 的字符级 bigram 并记录两张表。
词元流配对:(the,cat), (cat,sat), (sat,the), (the,cat), (cat,ran), (ran,the), (the,dog), (dog,sat)。对于 'banana' 字符流:(b,a), (a,n), (n,a), (a,n), (n,a)。
词元流计数(8 个实例,7 种不同二元组):(the,cat)=2, (cat,sat)=1, (sat,the)=1, (cat,ran)=1, (ran,the)=1, (the,dog)=1, (dog,sat)=1。上下文总量(以各词元开头的二元组总数):the=3, cat=2, sat=1, ran=1, dog=1。'banana' 字符:(a,n)=2, (n,a)=2, (b,a)=1。空输入 '' 产生的 bigram 计数为空字典 {}。
你的手写计数表与 Counter(zip(words, words[1:])) 的输出完全一致,并能清晰阐述为什么上下文行总和(而非全语料 unigram 词频)是下一步转移概率归一化的正确分母。
权威源资源与权利证明
makemore: Character-level language models on names.txt
对应章节 / 范围: makemore.py part 1 bigrams: counting consecutive character pairs into a frequency table
验证版本与日期: Verified 2026-10-30
Standard minimal implementation of a count-based bigram language model on the same names.txt corpus family.
Basic Python and Day 37 token ids
The lesson builds the full count table locally in pure Python with verified counts.
对比候选资源与选型分析 (2)
More rigorous and comprehensive but assumes PyTorch fluency; retained as the systems-rigor companion source.
Strong Transformer training reference but beyond the count-based baseline scope; deferred to later pretraining days.
Speech and Language Processing, 3rd ed. draft, Chapter 3: N-gram Language Models
对应章节 / 范围: Sections 3.1-3.2: n-gram counts, Markov assumption, and maximum-likelihood estimation
验证版本与日期: Draft of 2026-08-19 (Verified: 2026-10-30)
Standard reference text defining n-gram models, count normalization, and the chain-rule decomposition used in this lesson.
Basic probability
All definitions and worked count tables are included in the lesson itself.
对比候选资源与选型分析 (2)
Detailed smoothing survey but far beyond the add-one scope of a first n-gram day; optional further reading.
Solid academic lecture treatment but less current and less integrated with executable count tables than the SLP3 draft.