A bigram model is a table of conditional probabilities
Updated
A bigram language model predicts the next character using only the current one. We can build that model by counting pairs, before introducing a neural network. The important question is what each row of the count table means.
This note develops the counting and loss sections of my first makemore notebook. The example below is illustrative, not a reported training result.
Normalize the right thing
Let count how often character follows character . Let be the number of possible next tokens, including any boundary token. With additive smoothing ,
Each row sums to one. Dividing by the sum of the entire table would instead describe a joint frequency over pairs; it would not give the conditional distribution needed for next-character sampling.
For an illustrative row with counts and , the smoothed probabilities are . An unseen transition has a nonzero probability. Smoothing prevents infinite loss on that transition, but does not create evidence that it is common.
Turn probabilities into a loss
For observed transitions, mean negative log-likelihood is
Natural logarithms measure this loss in nats per transition. Assigning higher probability to the observed next character lowers its contribution to the loss. Multiplying many probabilities can underflow; summing log probabilities avoids that particular numerical problem.
A uniform predictor over tokens has loss . Be careful with a shortcut from scratch notes: one transition receiving more than probability does not establish that the model has learned useful structure. We need the average loss on held-out data, compared under the same tokenization and boundary conventions.
What this model cannot remember
Two prefixes ending in the same character receive the same next-character distribution. A bigram model cannot distinguish them using earlier context. Adding a longer context window motivates a multilayer perceptron language model.
Split complete names into training, validation and test sets before creating adjacent-character examples. Otherwise, fragments of the same name can appear on both sides of the split. Keep the test set for the final evaluation; do not report the illustrative row above as an experiment.
The learning source is Karpathy's makemore repository, accompanying Zero to Hero.