バイグラムのカウント:トークンストリームから集計表へ
Agent 支援エンジニアリング確認
前提条件
第 37 日を完了していること。行き詰まった場合は第 35 日の回復リンクで確率と Python を復習してください。
学習目標
トークンストリームをバイグラムカウント表 C(w_{i-1}, w_i) に変換し、文脈合計とペアカウントを分離し、すべての数値を手計算トレースと照合検証する(Unicode 文字や空入力の境界処理を含む)。
手順
バイグラムとは連続する 2 つのトークンのペアです。必須の作業:トークンストリーム 'the cat sat the cat ran the dog sat'(text_tools の動作と同様に、3 行の学習データを結合した 9 トークンのストリーム)上で長さ 2 のウィンドウをスライドさせ、Counter(zip(words, words[1:])) で隣接ペアを集計します。任意の作業:val 分割で繰り返し、データのスパース性を比較します。空文字列 '' が入力された場合は空のカウントとなり、Unicode 文字列 'é猫é' でも同様の規則でカウントされます。
演習と予測記録
コードを実行する前に、8 つの隣接ペアと完全なカウント表を手書きしてください。次に 'banana' の文字レベルバイグラムも同様に計算し、両方の表を記録してください。
ストリームのペア:(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。空文字列 '' の場合は {} となります。
手書きの集計表が 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.