Training Embeddings
Starting from random vectors and letting prediction error reshape them.
By the end of this chapter, you'll be able to:
- Describe the predict → loss → update loop that trains any learned parameter
- Explain what 'training an embedding' actually changes
- Connect this loop to the more general concept of gradient descent
The Embeddings chapter left off with a matrix full of random noise. Nothing about it is meaningful yet. This chapter is about the loop that turns that noise into something useful — the same loop that trains every learned parameter in this handbook, embeddings included.
The loop, in four steps
- Predict. Run the current parameters forward to produce an output.
- Calculate loss. Compare that output to what it should have been — the bigger the gap, the bigger the loss.
- Compute the error's direction. Figure out which way each parameter needs to move to make the loss smaller next time.
- Update. Nudge every parameter a small step in that direction, and repeat.
Here's exactly that loop, on the simplest possible target — a single trainable number — so the mechanics stay visible without an embedding matrix's worth of noise:
class Loss:
def calculate(self, prediction, actual):
return (prediction - actual) ** 2from neuron import Neuron
from loss import Loss
def train():
neuron = Neuron()
loss_function = Loss()
input_value = 2
expected = 4
learning_rate = 0.01
for step in range(100):
prediction = neuron.predict(input_value)
loss = loss_function.calculate(
prediction,
expected
)
error = expected - prediction
neuron.weight += (
learning_rate *
error *
input_value
)
if step % 10 == 0:
print(
"Step:",
step,
"Weight:",
neuron.weight,
"Loss:",
loss
)
if __name__ == "__main__":
train()Step: 0 Weight: 0.56 Loss: 9.0
Step: 10 Weight: 1.04 Loss: 3.98
Step: 20 Weight: 1.36 Loss: 1.76
...
Step: 90 Weight: 1.96 Loss: 0.006Predict
prediction = 2 × 0.5 = 1.0
Calculate loss
loss = (1.0 − 4)² = 9.0
Compute error
error = 4 − 1.0 = 3.0
Update
weight = 0.5 + 0.01 × 3.0 × 2 = 0.56
The Gradient Descent chapter covers this
same trajectory in full — the weight keeps closing the distance to 2.0, the value
that makes 2 × weight = 4, without ever fully reaching it.
Watch the Loss column: it shrinks every ten steps. That's the entire point — the weight isn't being set to the "right" value directly, it's being nudged, over and over, in whatever direction reduces the error.
This is gradient descent
error * input_value is a simplified stand-in for a gradient — the direction and
steepness of the loss with respect to a parameter. Scaling it by learning_rate before
applying it is exactly the update rule behind gradient descent, covered on its own in
the Gradient Descent chapter.
From one number to a whole embedding matrix
An embedding vector is just many trainable numbers stacked together instead of one. The loop doesn't change conceptually — predict, measure loss, compute each number's share of the error, nudge it — it just runs across every dimension of every token's vector simultaneously, using the real loss function of whatever the model is being trained to do (usually: predict the next token in a sequence).
The effect, after enough steps over enough text: tokens that tend to appear in
similar contexts get nudged toward similar vectors, because the same prediction
errors keep pushing them in the same direction. Nobody hand-labels "dog" and
"puppy" as related — the loop discovers it, purely from the statistics of which
words tend to surround them.
Check your understanding
1. In the training loop above, what triggers each update to neuron.weight?
2. What does it mean, mechanically, to 'train an embedding'?