統計二元詞元對:從詞元流到計數表
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.