Lesson 2 of 5 · 2 min
One job: predict the next token
A language model gives a probability to every possible next token. Writing a paragraph is that single step repeated.
import random
text = "the cat sat on the mat the cat ran to the door the cat sat down".split()
table = {}
for a, b in zip(text, text[1:]):
table.setdefault(a, []).append(b)
random.seed(3)
word, out = "the", ["the"]
for _ in range(8):
word = random.choice(table.get(word, ["the"]))
out.append(word)
print(" ".join(out))
print("after 'cat':", table["cat"])
This toy looks only one word back. A transformer looks at everything in its context and weighs what matters, which is what attention does.
