liveBook · Manning

Log probability

As explained in Build a Reasoning Model (From Scratch)

The logarithm of a model-assigned probability, used here to score how likely a generated token or whole sequence is under the current parameters.

Log probability is the natural logarithm of a model-assigned token probability. Applying log scaling preserves likelihood ordering while replacing tiny products with more manageable sums: higher probabilities become values closer to zero, and unlikely tokens become more negative. section 5.5 section 5.4

For next-token scoring, logits are converted with torch.log_softmax, then indexed at the observed next tokens. Summing those selected values gives a sequence log probability, equivalent to taking the logarithm of the product of conditional token probabilities. section 5.5 section 6.8

Choose the aggregation that matches the scoring or training objective:

  • Average only the answer-token log probabilities to obtain a length-normalized score for comparing responses of different lengths. section 5.6
  • Sum the answer-token log probabilities to represent the likelihood of an entire generated sequence. section 6.8
  • Multiply sequence log probabilities by detached advantages and negate the mean when constructing the GRPO policy-gradient loss for a minimizing optimizer. section 6.9

Scoring Berlin versus Bridge

Given the prompt “What is the capital of Germany?”, score “The capital of Germany is Berlin.” and “The capital of Germany is Bridge.” by excluding prompt tokens and averaging answer-token log probabilities. The model assigns the nonsensical ending a much more negative score, illustrating confidence-based ranking without guaranteeing factual correctness. section 5.6 section 5.4

Figure 5.13 Overview of moving from token probabilities to token log probabilities, which provide a numerically more stable basis for the log-probability scoring used later in self-refinement.

Trace the conversion from token probabilities to numerically stable token log probabilities before using them in a scorer. section 5.5

How Build a Reasoning Model (From Scratch) covers it

How deep it goes
Derives log-softmax indexing, sequence summation, answer-only averaging, and policy-loss integration, but does not develop broader probability theory.
Examples
Uses Berlin-versus-Bridge answers, seven-token answer averaging, rollout comparisons, and a seven-token synthetic vocabulary.
What it assumes
Assumes tokenization, model logits, tensor indexing, and softmax; teaches log scaling and token-to-sequence aggregation explicitly.
What you can do after
Apply it via PyTorch to compute token, average answer, and sequence log probabilities for scoring responses and building GRPO losses.
How it connects
Connects logits, softmax, token probability, sequence likelihood, self-refinement, reinforcement learning, GRPO, policy gradient loss, and entropy.

Also explained in

Compare how 8 books explain log probability →

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.