Start here

Run it

The course's recorded model, the small open language model of the videos. next_word.json holds, for two beginnings, every chance it gives the next word, and for the sentence "Once upon a time, there was a little girl.", the chance it gave each word that came. Its next word has 248,320 places: its 248,077 words and pieces, and a few spare ones.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.

1Ten ratings

Run it

From Maximum likelihood

Seven thumbs-up and three thumbs-down. Run the cell: it scores every like-rate from 0 to 1 by the chance of these ten ratings, the likelihood, and prints the top.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.

The top is at 0.7, the share you saw. At a like-rate of 1, a thumbs-down can't happen, so the likelihood is 0.

2Ten thousand ratings

Your turn

From Maximum likelihood

Now 7,000 thumbs-up and 3,000 down. Their chances multiplied come out 0 at every like-rate. Add their logs instead: np.log2 counts halvings. The rates leave out 0 and 1, where a log has no end. Then minus the top, over 10,000, is the loss for one rating, in bits.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.
Hint

7,000 thumbs-up, each adding np.log2(rates), and 3,000 thumbs-down, each adding np.log2(1 - rates).

One way to do it
rates = np.linspace(0.01, 0.99, 99)
product = rates ** 7000 * (1 - rates) ** 3000
log_likelihood = 7000 * np.log2(rates) + 3000 * np.log2(1 - rates)
best = rates[log_likelihood.argmax()]
loss = -log_likelihood.max() / 10_000
print(product.max(), round(best, 2), round(loss, 2))

3How unsure the model is

Your turn

From Entropy

The model gives every place a chance, but many places get the very same chance. So the file keeps each chance once, in chances, with how many places got it, in words. Work out the entropy by hand: each chance times its surprise, times its words, added up. Then SciPy, given every place's chance.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.
Hint

The surprise of a chance is -np.log2(chances). Multiply it by chances and by words, then .sum().

One way to do it
bits = {}
for beginning in record["beginnings"]:
    chances = np.array(beginning["chances"])
    words = np.array(beginning["words"])
    bits[beginning["text"]] = (words * chances * -np.log2(chances)).sum()
    by_scipy = entropy(np.repeat(chances, words), base=2)
    print(beginning["text"], round(bits[beginning["text"]], 2), round(by_scipy, 2))

4The entropy, drawn

Your turn

From Entropy

Entropy is the average surprise, so draw it and count. Let the model write the next word after "The cat sat on the" 100,000 times, each word as often as its chance says, and average the surprise of the words it drew. The file's chances are rounded, so dividing by their sum makes them add up to exactly 1, as rng.choice needs.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.
Hint

every_word[drawn] gives the chance of each word drawn. Its surprise is minus its log in halvings.

One way to do it
cat = record["beginnings"][1]
every_word = np.repeat(cat["chances"], cat["words"])
rng = np.random.default_rng(0)
drawn = rng.choice(len(every_word), size=100_000, p=every_word / every_word.sum())
drawn_surprise = -np.log2(every_word[drawn])
print(round(drawn_surprise.mean(), 2), round(bits["The cat sat on the"], 2))

5The model, scored on a sentence

Your turn

From Cross-entropy and KL divergence and Maximum likelihood

Score the model on each word of the sentence: its surprise at the chance it gave the word that came. Their average is the cross-entropy. Then minus the log of all ten chances multiplied, over ten: the negative log-likelihood per word. Use np.log2: np.log counts in nats, as PyTorch does, and gives 1.52.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.

A word that came with a chance of 0 would cost without end: np.log2(0) is minus infinity. So a model never gives a word exactly 0.

Hint

Each surprise is -np.log2 of its chance. For the second, multiply all ten with np.prod first.

One way to do it
pieces = record["sentence"]["pieces"]
came = np.array([piece["p"] for piece in pieces])
surprise = -np.log2(came)
cross_entropy = surprise.mean()
per_word = -np.log2(np.prod(came)) / len(came)
plt.bar(range(10), surprise)
plt.xticks(range(10), [piece["word"] for piece in pieces])
print(surprise.round(2), round(cross_entropy, 2), round(per_word, 2))

6A coin and a model

Your turn

From Cross-entropy and KL divergence and Entropy

A fair coin, and a model that says heads 90 times in 100. Work out what the model pays a toss, the coin's own entropy, and the KL between them, by hand. Then SciPy's entropy with two sets of chances gives the KL: the truth first, the model second. Swap them, and you get the other KL.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.
Hint

Weigh each surprise by how often the side really comes: the coin's chances. For cross, the surprise is the model's.

One way to do it
coin = np.array([0.5, 0.5])
model = np.array([0.9, 0.1])
cross = (coin * -np.log2(model)).sum()
own = (coin * -np.log2(coin)).sum()
kl = cross - own
print(round(cross, 2), round(own, 2), round(kl, 2))
print(round(entropy(coin, model, base=2), 2), round(entropy(model, coin, base=2), 2))

7A model that knows nothing

On your own

From Cross-entropy and KL divergence

On your own now. A model that gives each of its 248,077 words and pieces the same chance, whatever came before. Score it on the same sentence: keep its cross-entropy in flat_cross_entropy, in bits, and how many times the real model's 2.19 it is in times.

Tab indents. Control or Command with Enter runs the cell. Escape, then Tab, leaves the box.
Hint

Each word's chance is 1 over the vocabulary. Its surprise is the same for every word of the sentence.

One way to do it
vocabulary = record["vocabulary"]
flat = np.full(10, 1 / vocabulary)
flat_cross_entropy = (-np.log2(flat)).mean()
times = flat_cross_entropy / cross_entropy
print(vocabulary, round(flat_cross_entropy, 2), round(times, 1))

Run on Pyodide 314, Python 3.14, NumPy 2.4, matplotlib 3.10 and SciPy 1.18. Checked on .