liveBook · Manning

Torch.prod

As explained in Build a Reasoning Model (From Scratch)

PyTorch's product operation used here to multiply per-token probabilities into a sequence score.

torch.prod multiplies the per-token probabilities for a prompt into one joint sequence likelihood. Here it closes the scoring loop after softmax turns logits into next-token probabilities. section 5.4

The calculation indexes each position’s true next token, gathers its probability, then takes the product across the sequence. The result is used to compare candidate completions such as Berlin versus Bridge. section 5.4

Used to support token-level sequence scoring in this workflow:

  • Aggregates those probabilities into a single sequence score. section 5.4
  • Highlights why raw likelihoods can underflow on longer sequences. section 5.4

Prompt likelihood scoring

For "The capital of Germany is Berlin", the gathered next-token probabilities multiply to 5.9372e-08. For "The capital of Germany is Bridge", the product is 1.0481e-13, so the former scores higher under the model’s own distribution.

How Build a Reasoning Model (From Scratch) covers it

How deep it goes
Defines the product step and shows it in code, but defers numerical stabilization to log probabilities.
Examples
Uses the Berlin and Bridge prompts, plus printed next-token probabilities and joint probabilities.
What it assumes
Assumes token IDs, logits, softmax, and next-token prediction; teaches the product aggregation from that setup.
What you can do after
Apply it to score a sequence by multiplying per-token probabilities into a joint likelihood.
How it connects
Connects softmax, logits, next-token prediction, sequence likelihood, and log probabilities.

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.