liveBook · Manning

Chapter 5: Inference-time scaling via self-refinement

What you will learn in chapter 5 of Build a Reasoning Model (From Scratch)

Introduces self-refinement as an inference-time scaling technique and contrasts it with self-consistency.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces self-refinement as an inference-time scaling technique and contrasts it with self-consistency.

Key terms: inference-time scaling, self-consistency, self-refinement

5.1 Scoring and iteratively improving model responses

The section contrasts earlier inference-time methods with self-refinement and introduces scoring as a way to compare answers and track improvement.

Key terms: chain-of-thought prompting, base model, reasoning models, self-consistency, most frequent, latency, self-refinement, scoring functions, logprob scoring

5.2 Loading a pretrained model

The section checks a loaded model with sampled math responses, explains why the examples start on CPU, and weighs response quality against generation cost.

Key terms: qualitative value, tokens, intermediate steps, CPU, MPS, CUDA, temperature scaling, top-p sampling, random seed, MATH-500

5.3 Scoring LLM responses with a rule-based score

The section builds a rule-based scorer that ranks LLM responses by extractable answer format and brevity, then shows how to use and adjust it.

Key terms: rule-based scoring function, scorer, verifier, heuristic_score, boxed_bonus, extract_bonus, brevity_bonus, fulltext_bonus, exponentially decaying term, characters

5.4 Understanding token probability scores

This section shows how to score a completed answer using the model’s next-token probabilities, combine them into a sequence score, and interpret that score without mistaking it for correctness.

Key terms: logits, torch.softmax, next-token probabilities, calc_next_token_probas, t_idx, next_ids, all_probas[t_idx, next_ids], conditional probabilities, joint probability, sequence likelihood

5.5 From token probability scores to log probabilities

This section turns token probabilities into log probabilities so responses can be scored and compared without multiplying many tiny numbers.

Key terms: log probabilities, natural logarithm, logits, joint probability, sequence log likelihood, log space, underflow, torch.log_softmax, torch.softmax, torch.sum

5.6 Scoring model confidence with log probabilities

This section turns token log probabilities into an answer-only, length-normalized score for comparing responses, while explaining why that score is not a guarantee of correctness.

Key terms: avg_logprob_answer, answer tokens, token log probabilities, logprob scoring, confident mistakes, scoring signal

5.7 Self-refinement through iterative feedback

The section walks through self-refinement: an LLM generates an answer, critiques it, and uses a refinement prompt to revise it.

Key terms: self-refinement, critique prompt, refinement prompt, draft, refinement loop, critique

5.8 Coding the self-refinement loop

This section turns self-refinement into an iterative function with optional scoring, then uses benchmark results to show when refinement and its scorers help or hurt.

Key terms: self_refinement_loop, iterations, score_fn, revised_score, current_score, avg_logprob_answer, functools.partial, final_extracted, final_full, MATH-500

5.9 Summary

The section recaps how self-refinement revises an answer and how scoring can evaluate responses and guard against worse revisions.

Where this is used later

Concepts explained in this chapter

← Chapter 4: Improving reasoning with inference-time scaling Chapter 6: Training reasoning models with reinforcement learning →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.