Appendix B: Exercise solutions
local graph
Appendix B: Exercise solutions
Section titled “Appendix B: Exercise solutions”appendix B Exercise solutions
The complete code examples for the exercise solutions can be found in the supplementary GitHub repository at https://github.com/rasbt/reasoning-from-scratch.
B.1 Chapter 2
Section titled “B.1 Chapter 2”EXERCISE 2.1: ENCODING UNKNOWN WORDS
We can use a prompt similar to “Hello, Ardwarklethyrx. Haus und Garten.”, which contains a made-up word (“Ardwarklethyrx”) and three words in a non-English language (German):
prompt = "Hello, Ardwarklethyrx. Haus und Garten."input_token_ids_list = tokenizer.encode(prompt)for i in input_token_ids_list: print(f"{[i]} --> {tokenizer.decode([[i]])}")The output is:
[9707] —> Hello [11] —> , [1644] —> Ar [29406] —> dw [838] —> ark [273] —> le [339] —> th [10920] —> yr
[87] —> x [13] —> . [47375] —> Haus [2030] —> und [93912] —> Garten [13] —> .
As you can see, unknown words are broken into smaller subwords or even single tokens; this allows the tokenizer and LLM to handle any input.
German words are not broken down into characters or even subwords here, suggesting that the tokenizer has seen German texts during training. This also suggests that the LLM was likely trained on German texts too and should be able to handle at least some non-English languages well.
EXERCISE 2.2: RERUN CODE ON NON-CPU DEVICES
We can simply delete the line device = torch.device(“cpu”) in listing 2.1 and then rerun the rest of the code in chapter 2 as is. Reference numbers for the hardware I used to run the code are provided in table 2.1 at the end of chapter 2.
B.2 Chapter 3
Section titled “B.2 Chapter 3”EXERCISE 3.1: ADDING MORE TEST CASES
There are endlessly many test cases we can add. This is a selection of some interesting ones:
from reasoning_from_scratch.ch03 import ( run_demos_table)
more_tests = [ ("check_17", "[1, 2]", "(1, 2)", True), ("check_18", "1e-3", "0.001", True), ("check_19", "(-3)^2", "9", True), ("check_20", "-1", "-1", True),]
run_demos_table(more_tests)The output follows:
Test | Expect | Got | Status check_17 | True | True | PASS check_18 | True | True | PASS check_19 | True | True | PASS check_20 | True | False | FAIL
As you can see, the tests pass in all cases except for check_20, which swaps the regular minus sign with a Unicode version of a minus sign that looks indistinguishable to the
human eye (depending on the font or editor you are using). We could fix this test case by adding one of the following lines anywhere in the normalize_text function:
text = text.replace("-", "-")or
text = text.replace("\u2212", "-")Here is another interesting test:
extra_tests_1 = [ ("check_21", "Text around answer 3.", "3", True) ]You can run it with the following code:
run_demos_table(extra_tests_1)But it fails the test:
Test | Expect | Got | Status check_21 | True | False | FAIL
Passed 0/1
While it may seem that the code cannot handle cases that contain text, this is actually a poorly designed test. In practice, the run_demos_table function is intended specifically to test the grade_answer function; nothing more, nothing less.
The grade_answer function would never receive the entire answer in this text form because the answer would have been extracted from the text before being passed to it. If we want to test text answers, we need to call the test as follows:
from reasoning_from_scratch.ch03 import ( extract_final_candidate ) extra_tests_2 = [ ("check_21", extract_final_candidate("Text around answer 3."), "3", True) ] run_demos_table(extra_tests_2)As you can see from the output, it now passes the test:
Test | Expect | Got | Status check_21 | True | True | PASS
Passed 1/1
EXERCISE 3.2: CALCULATING THE AVERAGE RESPONSE LENGTH
There are two options for calculating the average response length. The first is to modify the evaluate_math500_stream function (listing 3.13) by adding the following lines:
# ...# below `num_correct = 0`total_len = 0
# ...# inside for i, row in enumerate(math_data, start=1):# anywhere below `gen_text = ...`total_len += len(tokenizer.encode(gen_text))
# ...# anywhere at the bottom before the return statementavg_len = total_len / num_examplesprint(f"Average length: {avg_len:.2f} tokens")Alternatively, the second option is to calculate the response lengths from the .json files created when we ran the evaluate_math500_stream function in the main chapter. This way, we avoid having to rerun the evaluation.
First, we load the .jsonl file as follows:
import jsonfrom pathlib import Path
WHICH_MODEL = "base"dev_name = "mps"
local_path = Path(f"math500-{dev_name}.jsonl") # You may need to adjust this path.if not local_path.exists(): raise FileNotFoundError( f"{local_path} not found. Run ch03_main.ipynb to create it." )
results = []with open(local_path, "r") as f: for line in f: if line.strip(): results.append(json.loads(line))
print("Number of entries:", len(results))Let’s print the dictionary keys to get a better idea of how the results dataset is structured:
print(results[0].keys())This prints the following:
dict_keys(['index', 'problem', 'gtruth_answer', 'generated_text', 'extracted', 'correct'])Each entry has multiple keys, but we are interested only in the generated_text key, which contains the model’s full answer.
Next, we need to load the tokenizer so we can tokenize the answer text before calculating the number of tokens. This is similar to the code we used in listing 3.1:
from reasoning_from_scratch.qwen3 import ( download_qwen3_small, Qwen3Tokenizer)
if WHICH_MODEL == "base":
download_qwen3_small( kind="base", tokenizer_only=True, out_dir="qwen3" ) tokenizer_path = Path("qwen3") / "tokenizer-base.json" tokenizer = Qwen3Tokenizer(tokenizer_file_path=tokenizer_path)
elif WHICH_MODEL == "reasoning":
download_qwen3_small( kind="reasoning", tokenizer_only=True, out_dir="qwen3" ) tokenizer_path = Path("qwen3") / "tokenizer-reasoning.json" tokenizer = Qwen3Tokenizer( tokenizer_file_path=tokenizer_path, apply_chat_template=True, add_generation_prompt=True, add_thinking=True, )Now we can calculate the average length as follows, which is similar to how we could have modified the evaluate_math500_stream function:
total_len = 0
for item in results: num_tokens = len(tokenizer.encode(item["generated_text"])) total_len += num_tokens
avg_len = total_len / len(results)print(f"Average length: {avg_len:.2f} tokens")The resulting average length is as follows:
Average length: 98.00 tokens
Table B.1 lists the average lengths for the different models and subsets.
Table B.1 Average number of tokens on MATH-500
| Model | Device | Average length | MATH-500 size |
|---|---|---|---|
| Base | CPU | 97.30 | 10 |
| Base | CUDA | 96.74 | 500 |
| Reasoning | CPU | 891.80 | 10 |
| Reasoning | CUDA | 1361.21 | 500 |
As you can see from the results in table B.1, and as expected, the reasoning model generates much longer responses (in this case, approximately 10 times longer).
EXERCISE 3.3: EXTENDING OR CHANGING THE EVALUATION DATASET
To evaluate the model on a larger dataset, you can simply change math_data[:10] to a different slice or larger number (up to 500) in the following function call:
num_correct, num_examples, acc = evaluate_math500_stream( model, tokenizer, device, math_data=math_data[:10], max_new_tokens=2048, verbose=False)Table B.2 shows the accuracy values for different dataset sizes. (Since the MATH-500 test set is already shuffled, no additional shuffling was applied.) As you can see, the first 10 examples are not very representative of MATH-500 performance on the full set of 500 examples.
Table B.2 Accuracies for different MATH-500 dataset sizes
| Model | Device | Accuracy | MATH-500 size |
|---|---|---|---|
| Base | CUDA | 30.0% | 10 |
| Base | CUDA | 34.0% | 50 |
| Base | CUDA | 27.0% | 100 |
| Base | CUDA | 15.3% | 500 |
| Reasoning | CUDA | 90.0% | 10 |
| Reasoning | CUDA | 58.0% | 50 |
| Reasoning | CUDA | 56.0% | 100 |
| Reasoning | CUDA | 48.2% | 500 |
We can also create an entirely new dataset in a style similar to MATH-500. For example, this book’s repository includes a dataset in MATH-500 style; we can use it by changing the filename from math500_test.json to math_new50_exercise.json in the chapter’s code (this dataset is included in the book’s GitHub repository at https://github.com/rasbt/reasoning-from-scratch/tree/main/ch03/01_main-chapter-code).
The performance of the models is as follows:
- Base: 36.0% (18/50)
- Reasoning: 80.0% (40/50)
On the new 50-example dataset, the base model reaches 36.0% accuracy, which is close to its 34.0% accuracy on the 50-example MATH-500 subset shown in table B.2. The reasoning model reaches 80.0% accuracy on the new dataset, which is higher than its 58.0% accuracy on the same MATH-500 subset. This suggests that the reasoning model’s strong performance is not limited to the original MATH-500 examples. While possible overlap with Qwen3’s training data cannot be ruled out, these results do not show signs of extensive overfitting to MATH-500.
EXERCISE 3.4: EXPERIMENTING WITH DIFFERENT PROMPT TEMPLATES
We could use an alternative prompt, similar to the one suggested in the chapter, that uses the word “problem” instead of “question”:
def render_prompt(prompt): template = ( "You are a helpful math assistant.\n" "Solve the problem and write the final " "result on a new line as:\n" "\\boxed{ANSWER}\n\n" f"Problem:\n{prompt}\n\nAnswer:" ) return templateOn the 500 examples, using this prompt improves the base model’s performance from 15.3% to 31.2%. It also improves the reasoning model’s performance from 48.2% to 50.0%.
From these observations, we may conclude that the base model is much more sensitive to the prompt format (likely due to memorizing some prompt-formatted MATH-500 examples from the training set) than the reasoning model. The latter seems largely unaffected.
B.3 Chapter 4
Section titled “B.3 Chapter 4”EXERCISE 4.1: USING CHAIN-OF-THOUGHT PROMPTING ON MATH-500
The modification only requires adding a prompt suffix such as “\n\nExplain step by step.” after applying the prompt template. Only a very small portion of code in the MATH-500 evaluation function from chapter 3 needs to be updated, as follows:
def evaluate_math500_stream(...): # ... for i, row in enumerate(math_data, start=1): prompt = render_prompt(row["problem"]) prompt += "\n\nExplain step by step." # NEW gen_text = generate_text_stream_concat( model, tokenizer, prompt, device,The improvements are shown in row 3 of table 4.1.
EXERCISE 4.2: USING TEMPERATURE SCALING AND TOP-P FILTERING ON MATH-500
Here, we replace the generate_text_stream_concat function with generate_text_stream_concat_flex and pass in generate_text_top_p_stream_cache as its generation function. The updated MATH-500 evaluation function from chapter 3 follows, and the changes are marked with comments labeled # NEW:
def evaluate_math500_stream( model, tokenizer, device, math_data, out_path=None, max_new_tokens=512, verbose=False, temperature=1.0, # NEW top_p=1.0, # NEW): # ...
with open(out_path, "w", encoding="utf-8") as f: for i, row in enumerate(math_data, start=1): prompt = render_prompt(row["problem"]) gen_text = generate_text_stream_concat_flex( # NEW model, tokenizer, prompt, device, max_new_tokens=max_new_tokens, verbose=verbose, generate_func=generate_text_top_p_stream_cache, # NEW temperature=temperature, # NEW top_p=top_p # NEW ) # ...The difference between this modified function and the baseline from chapter 3 can be seen in rows 1 and 4 of table 4.1.
EXERCISE 4.3: USING SELF-CONSISTENCY SAMPLING ON MATH-500
Starting from the evaluate_math500_stream function in listing 3.15, the first modification is to replace the line gen_text = generate_text_stream_concat(…) with a call to results = self_consistency_vote(…) from section 4.6.
The second modification adds a simple tiebreaking rule that selects the first occurrence of the most frequent answer. For instance, if the sampled results are 1, 3, 5, 3, 5, the function would return 3 because it is the first member of the most frequent group.
Since the most frequent answers are stored in results[“majority_winners”], one straightforward way to break ties is to take the first element of this list, that is, results[“majority_winners”][0].
Those changes are illustrated in the following code excerpts:
def evaluate_math500_stream( model, tokenizer, device, math_data, out_path=None, max_new_tokens=2048, verbose=False, prompt_suffix="", # NEW temperature=1.0, # NEW top_p=1.0, # NEW seed=None, # NEW num_samples=10, # NEW): if out_path is None: dev_name = str(device).replace(":","-") out_path = Path(f"math500-{dev_name}.jsonl")
num_examples = len(math_data) num_correct = 0 start_time = time.time()
with open(out_path, "w", encoding="utf-8") as f: for i, row in enumerate(math_data, start=1): prompt = render_prompt(row["problem"])
############## # NEW prompt += prompt_suffix results = self_consistency_vote( model=model, tokenizer=tokenizer, prompt=prompt, device=device, num_samples=num_samples, temperature=temperature, top_p=top_p, max_new_tokens=max_new_tokens, show_progress=False, show_long_answer=False, seed=seed, )
# resolve ties if results["final_answer"] is None: extracted = results["majority_winners"][0] else: extracted = results["final_answer"]extracted = extract_final_candidate(
Section titled “extracted = extract_final_candidate(”gen_text
Section titled “gen_text”)
Optionally, get long answer
Section titled “Optionally, get long answer”if extracted is not None: for idx, s in enumerate(results["short_answers"]): if s == extracted: long_answer = results["full_answers"][idx] breakgen_text = long_answer####################################################################################################################
is_correct = grade_answer( extracted, row["answer"])num_correct += int(is_correct)The performance improvements from self-consistency sampling are summarized and discussed in table 4.1 (rows 5–7 and 9–12).
EXERCISE 4.4: EARLY STOPPING IN SELF-CONSISTENCY SAMPLING
The early stopping check can be implemented by adding a few lines of code to see whether the given answer has already been counted multiple times, or, more specifically, whether the given answer count is greater than num_samples / 2:
if early_stop and counts[short] > num_samples / 2: majority_winners = [short] final_answer = short breakThe following excerpt from the modified self_consistency_vote function illustrates where to insert this code:
def self_consistency_vote( # ... early_stop=True, # NEW): # ...
if show_progress: print(f"[Sample {i+1}/{num_samples}] → {short!r}")
####################################################################################################################Early stop if one answer already meets >= 50% majority
Section titled “Early stop if one answer already meets >= 50% majority”if early_stop and counts[short] > num_samples / 2: majority_winners = [short] final_answer = short break####################################################################################################################
<!-- page: 336 (printed 324) -->if final_answer is None: mc = counts.most_common() if mc: top_freq = mc[0][1] majority_winners = [s for s, f in mc if f == top_freq] final_answer = mc[0][0] if len(majority_winners) == 1 else None
return { "full_answers": full_answers, "short_answers": short_answers, "counts": dict(counts), "groups": groups, "majority_winners": majority_winners, "final_answer": final_answer, }B.4 Chapter 5
Section titled “B.4 Chapter 5”EXERCISE 5.1: USING THE HEURISTIC SCORER AS A TIEBREAKER IN SELF-CONSISTENCY
There are many ways to implement this. Perhaps the easiest approach is to handle it outside the self-consistency function and work directly with the returned dictionary, similar to what we did in exercise 4.4, where we implemented the tiebreaking logic directly inside the evaluate_math500_stream function. The relevant lines are shown here:
from reasoning_from_scratch.ch05 import heuristic_score
def evaluate_math500_stream( # ... results = self_consistency_vote(...) # Majority vote winner available if results["final_answer"] is not None: extracted = results["final_answer"]
### NEW: Break tie with heuristic_score else: best = None best_score = float("-inf") for cand in results["majority_winners"]: scores = [ heuristic_score(results["full_answers"][idx], prompt=prompt) for idx in results["groups"][cand] ] score = max(scores) if score > best_score: best_score = score best = cand extracted = best
# ... return num_correct, num_examples, accThe results are shown in table B.3. The accuracy values and runtimes were computed on all 500 samples in the MATH-500 test set using a “cuda” GPU (DGX Spark).
Table B.3 MATH-500 self-consistency score with different tiebreaking
| Method | Model | Accuracy | Time | |
|---|---|---|---|---|
| 1 | Baseline with chain-of-thought prompting | Base | 33.4% | 129.2 min |
| 2 | Self-consistency (n=3) | Base | 43.2% | 328.2 min |
| 3 | Self-consistency (n=3) + heuristic | Base | 43.4% | 326.5 min |
| 4 | Self-consistency (n=3) + avg. logprob | Base | 44.8% | 327.7 min |
Row 1 in table B.3 is the baseline from chapter 4 without self-consistency. Row 2 doesn’t use a scorer for tiebreaking, so if there is a tie among the answers, it chooses the one that appears first. Using a heuristic scorer (row 3) as a tiebreaker results in a slight improvement. The best, though still minimal, improvement is achieved with the logprob scorer as a tiebreaker (row 4).
EXERCISE 5.2: USING THE HEURISTIC SCORER IN A BEST-OF-N SETUP
Best-of-N is similar to self-consistency in that we generate multiple answers. But instead of selecting the final answer by majority vote, we score all generated answers using a scoring function, such as heuristic_score, and return the highest-scoring one. There are several ways to implement this behavior, but the simplest approach is to use the existing self-consistency function from chapter 4 as a template and swap in heuristic_ score, as follows:
# ...from reasoning_from_scratch.ch05 import (heuristic_score)
def self_consistency_vote( #...): full_answers, short_answers = [], [] counts = Counter() groups = {} majority_winners, final_answer = [], None best_score, best_idx = float("-inf"), None
for i in range(num_samples): if seed is not None: torch.manual_seed(seed + i + 1)
answer = generate_text_stream_concat_flex( model=model, tokenizer=tokenizer, prompt=prompt, device=device, max_new_tokens=max_new_tokens, verbose=show_long_answer,generate_func=generate_text_top_p_stream_cache, temperature=temperature, top_p=top_p,<!-- ELIDED-IN-PRINT: the book prints this solution abbreviated (e.g. `def f(...):`, `# ...`); it is transcribed faithfully and is not expected to parse as standalone Python. Verified against the page scan. -->)
short = extract_final_candidate(answer, fallback="number_then_full")full_answers.append(answer)short_answers.append(short)counts[short] += 1
if short in groups: groups[short].append(i)else: groups[short] = [i]
score = heuristic_score(answer, prompt=prompt)
if score > best_score: best_score, best_idx = score, iTable B.4 compares the baseline with two best-of-N variants, one using the heuristic scorer and one using average log probability to select among three sampled answers.
Table B.4 MATH-500 best-of-N scores with heuristic and average logprob scores
| Method | Model | Accuracy | Time | |
|---|---|---|---|---|
| 1 | Baseline with chain-of-thought prompting | Base | 33.4% | 129.2 min |
| 2 | Best-of-N (n=3) + heuristic | Base | 40.6% | 327.7 min |
| 3 | Best-of-N (n=3) + avg. logprob | Base | 43.2% | 330.2 min |
The accuracy values and runtimes shown in the table were computed on all 500 samples in the MATH-500 test set using a “cuda” GPU (DGX Spark).
EXERCISE 5.3: USING THE LOGPROB SCORER AS A TIEBREAKER IN SELF-CONSISTENCY The solution is similar to exercise 5.1, except that we swap heuristic_score with avg_logprob_answer, as follows:
from reasoning_from_scratch.ch05 import heuristic_score
Section titled “from reasoning_from_scratch.ch05 import heuristic_score”from reasoning_from_scratch.ch05 import avg_logprob_answer
def evaluate_math500_stream(# ...) # ...
# score = heuristic_score( # candidate_full, prompt=prompt # ) score = avg_logprob_answer( model=model,The results were included in table B.3 (exercise 5.1), in row 4.
Exercise 5.4: USING THE LOGPROB SCORER IN A BEST-OF-N SETUP
To implement best-of-N with a logprob scorer, we can use the code from exercise 5.2 and swap the heuristic_score with avg_logprob_answer, as follows:
from reasoning_from_scratch.ch05 import ( avg_logprob_answer)# ... score = avg_logprob_answer( model=model, tokenizer=tokenizer, prompt=prompt, answer=answer, device=device ) if score > best_score: best_score, best_idx = score, i# ...The resulting MATH-500 score is shown in table B.4 (exercise 5.2).
Exercise 5.5: USING THE HEURISTIC SCORE FOR SELF-REFINEMENT
Using the heuristic_score is even simpler than using the logprob score; all we need to do is change the following code:
from functools import partial
avg_logprob_score = partial( avg_logprob_answer, model=model, tokenizer=tokenizer, device=device)torch.manual_seed(0)
results_logprob = self_refinement_loop( model=model, tokenizer=tokenizer, raw_prompt=raw_prompt, device=device, iterations=2, max_response_tokens=2048, max_critique_tokens=256, score_fn=avg_logprob_score,
verbose=True, temperature=0.7, top_p=0.9, )The updated code is
torch.manual_seed(0)results_logprob = self_refinement_loop( model=model, tokenizer=tokenizer, raw_prompt=raw_prompt, device=device, iterations=2, max_response_tokens=2048, max_critique_tokens=256, score_fn=heuristic_score, # NEW verbose=True, temperature=0.7, top_p=0.9,)The improvements over the baseline in chapter 3 and the self-consistency results from chapter 4 are shown in table 5.1 (rows 4, 5, and 10).
B.5 Chapter 6
Section titled “B.5 Chapter 6”EXERCISE 6.1: ADDING FORMAT-AWARE REWARD SHAPING
We can assign a partial reward (score 0.5) if no “\boxed{” answer is found, using the fallback=“number_then_full” fallback we coded in chapter 3, as follows:
from reasoning_from_scratch.ch03 import ( extract_final_candidate, grade_answer)
def reward_rlvr(answer_text, ground_truth):
# 1) Try to extract a boxed answer boxed = extract_final_candidate( answer_text, fallback=None ) if boxed: correct = grade_answer(boxed, ground_truth) return 1.0 if correct else 0.0
# 2) If no boxed answer is found, look for number unboxed = extract_final_candidate( answer_text, fallback="number_then_full" ) if unboxed: correct = grade_answer(unboxed, ground_truth) return 0.5 if correct else 0.0
return 0.0When plugged into the chapter 6 code and trained under the same settings, the partial-reward variant achieves lower accuracy (37.8%) than the standard GRPO setup (47.4%), despite using a similar number of tokens on average, as shown in table B.5.
Table B.5 MATH-500 accuracies for strict and partial rewards
| Method | Step | Max tokens | Num rollouts | Accuracy | Average tokens | |
|---|---|---|---|---|---|---|
| 1 | GRPO (chapter 6) | 50 | 512 | 8 | 47.4% | 586.11 |
| 2 | GRPO partial rewards (exercise 6.1) | 50 | 512 | 8 | 37.8% | 550.33 |
EXERCISE 6.2: ZERO-ADVANTAGE CASES
If the rewards are all equal (for instance, they are all 0 or all 1), the advantages will be 0 because subtracting the mean removes the shared reward value and leaves or zeros, as you can see here:
import torch
rollout_rewards = [0., 0., 0., 0.]rewards = torch.tensor(rollout_rewards)advantages = (rewards - rewards.mean()) / (rewards.std() + 1e-4)print(advantages)This returns tensor([0., 0., 0., 0.]).
Similarly, if we change the rollout rewards to rollout_rewards = [0., 0., 0., 0.], we get the same all-zero tensor, tensor([0., 0., 0., 0.]).
In short, if all rewards in a group are identical, for example, all rewards are 0 or all rewards are 1, then for all rollouts. As a result, the policy gradient is zero, and the model parameters are not updated for that prompt.
This behavior is intentional. If all rollouts are equally bad or equally good, there is no relative signal to tell the model which behavior to reinforce or suppress. Intuitively, if the model answers all the questions correctly, there is no need to update it. Conversely, if the model answers all the questions incorrectly, we don’t want to update the model to reinforce that behavior.
B.6 Chapter 7
Section titled “B.6 Chapter 7”EXERCISE 7.1: TESTING THE
The following code checks that the format reward is zero if the <think> tokens are used incorrectly:
from pathlib import Pathimport torchfrom reasoning_from_scratch.qwen3 import Qwen3Tokenizerfrom reasoning_from_scratch.qwen3 import download_qwen3_smallfrom reasoning_from_scratch.ch07 import reward_format
<!-- page: 342 (printed 330) -->download_qwen3_small( kind="reasoning", tokenizer_only=True, out_dir="qwen3")tokenizer_path = Path("qwen3") / "tokenizer-reasoning.json"tokenizer = Qwen3Tokenizer(tokenizer_file_path=tokenizer_path)
prompt = "Calculate ..."
def check_case(name, rollout): token_ids = tokenizer.encode(prompt + rollout) prompt_len = len(tokenizer.encode(prompt)) reward = reward_format( token_ids=torch.tensor(token_ids), prompt_len=prompt_len, ) print(f"{name}: {reward}")1) Correct case
Section titled “1) Correct case”check_case( "Correct order", "Let's ... <think> ... </think> ...")2) Typo in tag
Section titled “2) Typo in tag”check_case( "Typo in <think>", "Let's ... <thnik> ... </think> ...")3) Reversed order
Section titled “3) Reversed order”check_case( "Reversed order", "Let's ... </think> ... <think> ...")4) Missing one tag
Section titled “4) Missing one tag”check_case( "Missing </think>", "Let's ... <think> ...")The output is as follows, indicating that the function requires correct <think>… </think> tag usage to award a reward of 1.0:
Correct order: 1.0 Typo in <think>: 0.0 Reversed order: 0.0 Missing </think>: 0.0
EXERCISE 7.2: MAKING THE FORMAT REWARD CONDITIONAL The implementation of the conditional reward is very simple; in the chapter, we discussed implementing the overall reward as follows:
reward = rlvr_reward + format_reward_weight * format_rewardThis is one way to disable the reward when the correctness reward (rlvr_reward) is 0.0:
if conditional_reward: format_reward *= rlvr_rewardreward = rlvr_reward + format_reward_weight * format_rewardTo use this in practice, you can run the 7_6_plus_format_reward.py script, which we used in section 7.6, with the —conditional_reward flag enabled.
You can download the log file of this run (using settings similar to those in section 7.6) and plot it as follows:
from reasoning_from_scratch.ch07 import download_from_githubfrom reasoning_from_scratch.ch07 import plot_grpo_metricsdownload_from_github( "ch07/02_logs/7_6_plus_format_reward_conditional_metrics.csv")plot_grpo_metrics( "7_6_plus_format_reward_conditional_metrics.csv", columns=["loss", "reward_avg", "avg_response_len", "eval_acc"],) loss reward_avg 0.050 | 2.0 | 0.025 | 1.5 | ~~--__ __--~~ 0.000 |~~~~~~~~~~~~~~~~~~~~~~ 1.0 | ~~--~~-0.025 | 0.5 |-0.050 +---------------------- 0.0 +---------------------- 0 200 400 0 200 400 Step Step
avg_response_len eval_acc 1000 |~~--__ 40 |# # # # # # # 750 | ~~--___ 30 |# # # # # # # # 500 | ~~--___ 20 |# # # # # # # # # 250 +---------------------- 10 |# # # # # # # # # 0 200 400 0 +---------------------- Step 0 200 400 Step[!visual-conversion] Figure B.1 — Basic metrics from a GRPO training run with a conditional format reward (printed p. 331, scan page-343.png). Print annotations: “Average reward drops but recovers”, “Accuracy drops but recovers”, and “The average response length is steadily decreasing.” The body text states what the figure shows: evaluation accuracy and reward average take a big hit but seem to recover, and despite the performance crash the run looks more stable than before. Per-step values are not recoverable at scan resolution.
The plots in figure B.1 show that the evaluation accuracy and reward average take a big hit but seem to recover. Overall, despite the performance crash, this looks more stable than before, and the trend indicates that performance would improve further with longer training.
The previous plot focused on the main training metrics. To inspect the effect of the conditional format reward more directly, we can plot additional metrics from the same run:
plot_grpo_metrics( "7_6_plus_format_reward_conditional_metrics.csv", columns=["reward_avg", "format_reward_avg", "adv_std", "entropy_avg"], ) reward_avg format_reward_avg 2.0 | 1.00 | 1.5 | ~~--__ __--~~ 0.75 | ~~--__ __--~~ 1.0 | ~~--~~ 0.50 | ~~--~~ 0.5 | 0.25 | 0.0 +---------------------- 0.00 +---------------------- 0 200 400 0 200 400 Step Step
adv_std entropy_avg 1.00 | 10.0 | ______ 0.75 | __--~~~~--__ 7.5 | _/ \_ 0.50 | _- ~~--_ 5.0 | _/ 0.25 | - 2.5 | ____/ 0.00 +---------------------- 0.0 +---------------------- 0 200 400 0 200 400 Step Step[!visual-conversion] Figure B.2 — Additional metrics from a GRPO training run with a conditional format reward (printed p. 332, scan page-344.png). Print annotations: “The average format reward curve mimics average reward curve” and “The average entropy (randomness) increases after the first 200 steps.” The body text draws the conclusion: the average format reward mimics the average reward graph almost perfectly, which is a good sanity check that the conditional logic is working — the format reward is essentially always awarded once the model is correct, since the trained reasoning model already uses
<think>...</think>tags correctly and does not unlearn that ability. Per-step values are not recoverable at scan resolution.
In figure B.2, we see the average format reward mimicking the average reward graph almost perfectly, which is a good sanity check that the conditional logic is working. The average format reward also shows how much of the total reward is coming from the format term on the subset of correct answers.
As we can see, though, since the average format reward graph echoes the average reward one, it’s mainly a bonus. It looks like it’s always awarded if the model is correct; this makes sense because the trained reasoning model already knows how to use <think>…</think> tags correctly, and we can see that it doesn’t unlearn this ability.
B.7 Chapter 8
Section titled “B.7 Chapter 8”EXERCISE 8.1: TRAINING AND VALIDATION SET LENGTHS
To calculate the training and validation answer length statistics, you can add the following commands at the end of listing 8.7 in section 8.4.3, following the partitioning:
compute_length(train_examples)# Prints# Average: 1180 tokens# Shortest: 236 tokens (index 5730)# Longest: 2048 tokens (index 1319)As you can see, the average token length (1180 versus 1106) is fairly similar, and datasets should be relatively balanced.
As a bonus, we can plot histograms to visualize the distributions:
import matplotlib.pyplot as plt
train_lengths = [len(ex["token_ids"]) for ex in train_examples]val_lengths = [len(ex["token_ids"]) for ex in val_examples]Normalize counts because the validation split is much smaller
Section titled “Normalize counts because the validation split is much smaller”bins = range(0, max(train_lengths + val_lengths) + 64, 64)
fig, ax = plt.subplots(figsize=(7, 4))
ax.hist(train_lengths, bins=bins, density=True, alpha=0.6, label="Train")ax.hist(val_lengths, bins=bins, density=True, alpha=0.6, label="Validation")ax.set_xlabel("Token length")ax.set_ylabel("Density")ax.legend()plt.tight_layout()plt.show()The resulting plot is shown in figure B.3.
There are far fewer validation samples, which is why the validation histogram seems a bit jagged, but as you can see, it still provides good coverage of the distribution.
EXERCISE 8.2: DISTILLING WITHOUT <think> TOKENS
We can replicate the runs without <think>``</think> tokens in the script execution command:
uv run distill.py
—data_path deepseek-r1-math-train.json
—validation_size 25
—epochs 3
—lr 1e-5 \
—max_seq_len 2048
—grad_clip 1.0
Density 0.00175 | ## 0.00150 | ## ## ## 0.00125 | ## ## ## ## ## ## 0.00100 | ## ## ## ## ## ## ## ## 0.00075 | ## ## ## ## ## ## ## ## ## ## 0.00050 | ## ## ## ## ## ## ## ## ## ## ## ## ## 0.00025 | ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## 0.00000 +-------------------------------------------------- 0 500 1000 1500 2000 Token length
[dark] Train [light] Validation[!visual-conversion] Figure B.3 — Distribution of training and validation set lengths (printed p. 334, scan page-346.png). A two-series overlaid histogram of density against token length (0–2000), with the training and validation distributions closely tracking each other — the point of the exercise, which asks whether the two partitions are balanced by sample length. Individual bin heights are not recoverable at scan resolution; the sketch reproduces the axes, ranges and the shape of the distribution (a peak below 1000 tokens with a long right tail).
For the evaluation, we can use the base instead of reasoning model:
uv run evaluate_math500.py
—dataset_size 500
—which_model base
—max_new_tokens 4096
—checkpoint_path
run_11/checkpoints/distill/qwen3-0.6B-distill-step05746-epoch1.pth
The results are shown in table B.6, where the new results (without <think> tokens) are shown first, with the corresponding think-token results (from the chapter) in parentheses.
Table B.6 MATH-500 task accuracy with and without <think> tokens
| Method | Epoch | Final validation loss | MATH-500 accuracy | |
|---|---|---|---|---|
| 1 | Base Qwen3 0.6B (chapter 3) | - | - | 15.2% |
| 2 | Reasoning Qwen3 0.6B (chapter 3) | - | - | 48.2% |
| 3 | DeepSeek-R1 | 1 | 0.5436 (0.5404) | 31.8% (30.6%) |
| 4 | DeepSeek-R1 | 2 | 0.5349 (0.5339) | 31.8% (32.4%) |
Table B.6 MATH-500 task accuracy with and without <think> tokens (continued)
| Method | Epoch | Final validation loss | MATH-500 accuracy | |
|---|---|---|---|---|
| 5 | DeepSeek-R1 | 3 | 0.5343 (0.5306) | 30.2% (33.6%) |
| 6 | Qwen3 235B-A22B | 1 | 0.4043 (0.3130) | 44.8% (45.0%) |
| 7 | Qwen3 235B-A22B | 2 | 0.3963 (0.3087) | 39.4% (43.8%) |
| 8 | Qwen3 235B-A22B | 3 | 0.3948 (0.3078) | 39.8% (44.2%) |
Interestingly, the Qwen3 model has a lower validation loss when <think>``</think> tokens are omitted, but this doesn’t translate into better modeling performance. The omission of <think> makes the results slightly worse in almost all cases.