Example concept
How a model chooses its next token
01 Intuition
If a phrase is often followed by the same word, a simple language model can learn that pattern by counting examples. More matching examples make a completion more likely.
02 Define it
A next-word distribution lists the probability of each possible completion. Divide each word's count by the total count so the probabilities add to 100%.
03 Work it out
Suppose ten matching examples end with mat six times, chair three times, and floor once.
04 Explore it
Add or remove examples ending in mat. The other counts stay fixed while the model recalculates all three probabilities.
05 Code it
The same calculation is short enough to inspect. Python loads only when you run it, and executes locally in your browser.
counts = {
"mat": 6,
"chair": 3,
"floor": 1,
}
total = sum(counts.values())
for word, count in counts.items():
probability = count / total
print(f"{word:>5}: {probability:.1%}")Ready to run.
Run the program to compare its output with the hand calculation.