How a Language Model Is Trained

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

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

  1. This brings in Python's built-in math module, for functions like math.exp and math.sqrt.
  2. Three words that could follow "i".
  3. The untrained model scores them all 0: no preference at all.
  4. The training text says "i like", so the right answer is option 0, "like".
  5. Learning rate: how big each nudge is.
  6. step is created with the value 0.
  7. Softmax, as before: powers…
  8. …divided by the total. Every word has chance 0.333.
  9. 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.
  10. 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.
  11. It prints step 0 chance of like: 0.333 loss: 1.1.
  12. step changes from 0 to 1.
  13. powered now points to a different list: [1.94838, 0.71677, 0.71677].
  14. Now "like" has chance 0.576.
  15. Loss 0.552: the model is less surprised.
  16. scores now points to a different list: [1.091, -0.545, -0.545].
  17. It prints step 1 chance of like: 0.576 loss: 0.552.
  18. step changes from 1 to 2.
  19. powered now points to a different list: [2.97725, 0.579842, 0.579842].
  20. "like" is at 0.72.
  21. Loss 0.329.
  22. scores now points to a different list: [1.371, -0.685, -0.685].
  23. It prints step 2 chance of like: 0.72 loss: 0.329.
  24. 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

Best practices

Key takeaways

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.

·

More in Training a Language Model