AI ENGINEERING: DEEP DIVE · MODULE 03 — TOKENIZATION & TRAINING MECHANICS · 08 A hands-on series · math + code

Module 3.2

Predict, measure wrong, nudge

Same output head as Module 2.3, same vocabulary, same query — but this time the model hasn't learned "quota" yet. Watch cross-entropy measure exactly how wrong it is, then watch one gradient step nudge it toward the right answer.

TRUE NEXT WORD: "quota" — MODEL'S CURRENT GUESS
Cross-entropy loss: 1.98 P(quota): 0.14

The math

Three formulas, one training step

Cross-entropy loss
L = −log P(true token)
= −log(0.14)
= 1.98
Gradient (a clean shortcut)
∂L/∂logits = probs − one_hot(true)
softmax + cross-entropy combine into exactly this
grad(quota) = −0.86
Gradient descent
logits ← logits − lr · gradient
lr exaggerated here to make one step visible
lr = 1.0 (real training: ~0.0001–0.01)

Every weight in the entire network gets a version of this same nudge — the chain rule is just this gradient, propagated backward through every matrix that helped compute the logits.

The code

One training step, in full

one_training_step.py
def softmax(x):
    e = np.exp(x - x.max())
    return e / e.sum()

def cross_entropy(logits, true_idx):
    probs = softmax(logits)
    return -np.log(probs[true_idx]), probs

loss, probs = cross_entropy(logits, true_idx)

one_hot = np.zeros_like(probs); one_hot[true_idx] = 1.0
grad = probs - one_hot                    # ∂loss/∂logits, exactly

lr = 0.01                                # realistic learning rate
logits = logits - lr * grad               # gradient descent step

Real training runs this on batches of thousands of (context, next-token) pairs at once, and the gradient reaches all the way back through every attention and feed-forward matrix in every block — but it is, at its core, this loop.

Go deeper (optional)

Gradient descent, slowly and clearly

A shorter, focused clip on the general mechanism (not specific to language models) — useful if the "logits minus one-hot" shortcut above felt like it skipped a step.

StatQuest with Josh Starmer — "Gradient Descent, Step-by-Step." Supplementary and optional.

The Nimbus thread

This is how Nimbus-bot would learn "quota" at all

Nothing about "workspace quota" is built into a base model. If Nimbus's real support transcripts show "export fail" followed by "quota" often enough, this exact update — repeated across millions of examples — is what shifts the weights until the model predicts it confidently, the way Module 2.3 assumed it already did. Module 3.3 covers what changes when this happens at each stage: pretraining, fine-tuning, and instruction-tuning.

So what

Training is predict, measure how wrong, nudge every weight a little — repeated billions of times, never anything more mysterious than that.