6

Training: nudging the weights

Measure how wrong the guess was, find which way each number should move, nudge, repeat.

Hover or tap a token to follow it. In step 5, drag the slider or tap − / +.

blue frame: learned weights (training changes them)green frame: numbers worked out for this sentence
1The training data: our corpus
Module 6, step 1: The training data: our corpus

Training uses nothing but the four sentences cleaned in module 1: the same tokens and the same IDs. They hold 22 “guess the next token” questions, each with a known right answer.

Each question is a (context, next token) pair. This is self-supervised: the text supplies its own answers.

2The training loop
Module 6, step 2: The training loop

Training is four moves, repeated. Only the last move changes the model.

forward pass → loss → gradients (backpropagation) → gradient descent step. This next-token training is the only kind of training in this course; other methods are out of scope.

3How wrong was the guess?
Module 6, step 3: How wrong was the guess?

One number scores each guess: small when the right token got a big chance, big when it got a small one.

loss = −log p(right token). Called cross-entropy loss.

4Which way should each score move?
Module 6, step 4: Which way should each score move?

Subtract the right answer (1 for “sat”, 0 for the rest) from the chances. Negative → raise that score. Positive → lower it.

∂loss/∂scores = p − y, where y is the one-hot right answer.

5One nudge, worked by hand
Module 6, step 5: One nudge, worked by hand

The push travels back to the table U: each number moves by (cat's row × push), times a small step size.

∂loss/∂U = hᵀ(p − y); U ← U − η · ∂loss/∂U. The same idea reaches E, P and the W tables, one step further back each time (chain rule).

6Now repeat, for every question
Module 6, step 6: Now repeat, for every question

Drag the slider. Each step nudges every table using all 22 questions at once. The loss falls and “sat” wins.

150 steps of plain gradient descent, η = 0.3. All 26 frames are computed in Python, not in your browser.

After training, the same forward pass gives “sat” 99% (it started at 8.3%). The tables now hold what our four-sentence corpus taught them.