4. Count what comes next
First, two things from lesson 3.
The program that always says “the” made 4 mistakes. That number is its…
Who labeled the next-word examples?
A program that reads first
“Always the” ignores the text. A smarter program reads some text first:
counts = {} # for each word: what came right after it, and how many times
def read(text):
for word, next_word in pairs(text):
counts[word][next_word] += 1Move the slider to let read go through these three sentences, one pair of words at a time.
| word | what came next |
|---|
Then, to guess the next word, the program looks only at the last word:
def guess(text):
last_word = text.split()[-1]
return biggest_count(counts[last_word])guess gets a text that ends with “the”. What does it say?
Is it better?
The sentence from lesson 3. In every row guess takes the last word (underlined) and picks the biggest count for it. The gray box below shows how it picked for the newest row. Hover over any row to see that row.
Move the slider to the end. How many mistakes does it make?
Why did it say “cat” in the last row?
What just happened
- The counts are the program’s parameters. Nobody wrote them by hand: the program got them by reading text. That’s training, and the text is the training data.
- On the lesson 3 sentence, the loss went from 4 to 1.
- A program that guesses from the last word only is a bigram model (bigram: a pair of words). An LLM looks at all the text before the gap, not just the last word.