liveBook · Manning

Numerical stability

As explained in Build a Reasoning Model (From Scratch)

The property of a computation remaining accurate and avoiding underflow or rounding errors when values are very small or very large.

Numerical stability keeps probability scoring accurate when values become extremely small. Here, token probabilities are converted to log probabilities with logarithmic scaling to avoid underflow and rounding issues during sequence scoring. section 5.5

The treatment builds the concept through these steps:

  • Starts from token probabilities used to rank responses, where joint probabilities can become tiny. section 5.5
  • Applies torch.log or torch.log_softmax to move into log space while preserving ordering. section 5.5

Token scoring with log probabilities

A simple logit-to-probability example computes softmax probabilities, then converts them with torch.log and torch.log_softmax. The plotted values keep the same ranking, and a next-token example scores "Berlin" higher than "Bridge".

How Build a Reasoning Model (From Scratch) covers it

How deep it goes
Explains the scaling step, demonstrates torch.log_softmax, and derives joint sequence scores by summing token log probabilities.
Examples
Uses a softmax/log-softmax toy array, plotted logits and probabilities, and Berlin-versus-Bridge next-token scoring.
What it assumes
Assumes token probabilities and logits are already available, then introduces log probabilities from zero.
What you can do after
Apply log scaling to score tokens and compare sequences without losing signal from very small probabilities.
How it connects
Connects token probabilities, logits, softmax, log probabilities, and sequence likelihood.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Reasoning Model (From Scratch). The section links point to the parts of the book it draws on.