Predict with a tiny bigram table
Watch the spaces around tokens and probability values. · 177 words
Language modeling assigns probabilities to sequences of tokens. A token may be a word, part of a word, or punctuation. A bigram model uses only the previous token, so it predicts the next token with P(next | previous). To train it, count how often each token follows each other token in the training text. Suppose three captions are "the cat sleeps", "the cat naps", and "the dog sleeps". The count from "the" to "cat" is 2, and the count from "cat" to "sleeps" is 1. Divide each count by the total number of times the previous token appears.
Now start with "the" and decode one token at a time. From the row for "the", "cat" has probability 2/3 and "dog" has probability 1/3, so a sampler may pick "cat". From "cat", "sleeps" and "naps" each have probability 1/2. If the sampler picks "sleeps", the text so far is "the cat sleeps". The table supplies local choices, while the decoder combines those choices into a sequence. A bigram cannot use information from tokens before the immediately previous token.
Check your recall
In the tiny bigram table, what is P(cat | the), and why?
Check your answer
It is 2/3 because two of the three training captions have "cat" after "the".
Sources and credits
Original KeyTouch lesson. Background reading · Hugging Face · LLM Course.