Show the model the real next word, measure how surprised it was (the loss), and nudge its scores. Repeat this billions of times and you have a language model.
AI · beginner · 10 min · 24 steps · free
What you'll learn
Measure surprise with the loss −log(chance of the right word)
Update scores with the softmax gradient
Watch the chance of the right word rise as the loss falls
train_next_word.py
import math
# Training: show the model the real next word, measure how surprised it was, and nudge its scores.
options = ["like", "want", "tea"]
scores = [0.0, 0.0, 0.0] # after "i", the untrained model has no preference
target = 0 # in the training text, "i" was followed by "like"
lr = 1.0
for step in range(3):
powered = [math.exp(s) for s in scores]
chances = [round(p / sum(powered), 3) for p in powered]
loss = round(-math.log(chances[target]), 3)
# The gradient of this loss: (chance - 1) for the right word, (chance - 0) for the others.
scores = [round(s - lr * (c - (1 if i == target else 0)), 3) for i, (s, c) in enumerate(zip(scores, chances))]
print("step", step, "chance of like:", chances[target], "loss:", loss)
Step by step
This brings in Python's built-in math module, for functions like math.exp and math.sqrt.
Three words that could follow "i".
The untrained model scores them all 0: no preference at all.
The training text says "i like", so the right answer is option 0, "like".
Learning rate: how big each nudge is.
step is created with the value 0.
Softmax, as before: powers…
…divided by the total. Every word has chance 0.333.
The loss is −log(chance of the right word) = −log(0.333) = 1.1. It is big when the right word was thought unlikely, and 0 when it was certain.
Nudge the scores. For the right word the gradient is chance − 1 (negative), so its score goes up by 0.667. Each other word goes down by its chance, 0.333.
It prints step 0 chance of like: 0.333 loss: 1.1.
step changes from 0 to 1.
powered now points to a different list: [1.94838, 0.71677, 0.71677].
Now "like" has chance 0.576.
Loss 0.552: the model is less surprised.
scores now points to a different list: [1.091, -0.545, -0.545].
It prints step 1 chance of like: 0.576 loss: 0.552.
step changes from 1 to 2.
powered now points to a different list: [2.97725, 0.579842, 0.579842].
"like" is at 0.72.
Loss 0.329.
scores now points to a different list: [1.371, -0.685, -0.685].
It prints step 2 chance of like: 0.72 loss: 0.329.
Three nudges took "like" from 0.33 to 0.72. A real model does the same with billions of words of text, adjusting billions of weights inside the transformer instead of three scores.
Common mistakes
Thinking the model stores sentences: it only adjusts numbers that make the right next word more likely
Best practices
Read a falling loss as "less surprised by the real text"
Key takeaways
Pre-training = predict the next token of real text, again and again.
The loss is −log(chance given to the right token): high when surprised.
The gradient raises the right token's score and lowers the others.
As training goes on, the loss falls and the right words become likely.
Check yourself
The model gave the right word a chance of 1.0. What is the loss?
Answer: 0. −log(1) = 0: no surprise at all.
Where do the "right answers" for pre-training come from?
Answer: The next word in ordinary text. The text itself supplies the answer: whatever word actually came next.