Docsity

Reinforcement learning, Lecture notes of Computer Science

Toggle sidebar
bg1
Chapter 18
Reasoning in LLMs
18.1 Chain of thoughts
Motivation. Consider the grade-school arithmetic example where Roger
starts with 5 tennis balls and buys 2 cans with 3 balls each. To solve it, we
naturally do it in steps, compute the number of new balls first, and only then
add them to the original 5 balls:
z: 2 ×3 = 6,5 + 6 = 11,
so the final answer is a= 11.
Typical prompts to LLMs will just ask the LLMs to solve the problem.
Chain-of-thought (CoT) prompting instead asks the model to include the
intermediate computation before giving the answer.
The motivation is that many reasoning tasks are easier to express as a
short sequence of intermediate steps followed by an answer than as a single
direct prediction. The reasoning trace gives the model more time to think
(more compute), and more memory to log intermediate results. The extra
tokens can be useful because the autoregressive model can condition later
predictions on earlier partial computations, turning one hard prediction into
a sequence of smaller predictions.
Few-shot CoT. In few-shot CoT, each in-context demonstration contains
not only an input and answer, but also an intermediate natural-language
solution. A prompt has the form
(x(1), z (1), a(1)),...,(x(m), z(m), a(m)), x,
220
pf3
pf4
pf5

Partial preview of the text

Download Reinforcement learning and more Lecture notes Computer Science in PDF only on Docsity!

Chapter 18

Reasoning in LLMs

18.1 Chain of thoughts

Motivation. Consider the grade-school arithmetic example where Roger starts with 5 tennis balls and buys 2 cans with 3 balls each. To solve it, we naturally do it in steps, compute the number of new balls first, and only then add them to the original 5 balls:

z : 2 × 3 = 6, 5 + 6 = 11,

so the final answer is a = 11. Typical prompts to LLMs will just ask the LLMs to solve the problem. Chain-of-thought (CoT) prompting instead asks the model to include the intermediate computation before giving the answer. The motivation is that many reasoning tasks are easier to express as a short sequence of intermediate steps followed by an answer than as a single direct prediction. The reasoning trace gives the model more time to think (more compute), and more memory to log intermediate results. The extra tokens can be useful because the autoregressive model can condition later predictions on earlier partial computations, turning one hard prediction into a sequence of smaller predictions.

Few-shot CoT. In few-shot CoT, each in-context demonstration contains not only an input and answer, but also an intermediate natural-language solution. A prompt has the form

(x(1), z(1), a(1)),... , (x(m), z(m), a(m)), x,

and the model is expected to continue with a new trace z and answer a for the query x. This is the prompting version of asking the model to “show its work.” Wei et al. [2022] showed that, for sufficiently large pretrained language models, few-shot CoT substantially improves arithmetic, symbolic, and commonsense reasoning compared with standard few-shot prompts that contain only final answers.

Zero-shot CoT. Zero-shot CoT removes the hand-written demonstrations and instead uses a short instruction that encourages an intermediate trace. The canonical example is to append a phrase such as “Let’s think step by step” before asking for the answer [Kojima et al., 2022]. This is useful when writing high-quality few-shot rationales is expensive or when the same prompting template should transfer across many task types.

18.2 RLVR with long chain-of-thought rea-

soning

The previous section described chain-of-thought as an inference-time format: we prompt the model so that it writes intermediate steps before giving the answer. If this format makes prediction easier, it is natural to ask whether we can train models to produce useful reasoning traces, rather than only elicit them through prompting. The challenge is that high-quality step-by- step supervision is expensive. In many domains, however, the final answer can be checked automatically: a math problem may have a known answer, a programming problem may have unit tests, a theorem-proving task may have a proof checker, and a structured-output task may have a deterministic validator. Reinforcement learning with verifiable rewards (RLVR) trains exactly in this setting. The model samples a completion containing a chain of thought and a final answer, while an automatic verifier rewards the completion ac- cording to whether the final answer passes the check. This makes RLVR especially natural for long chain-of-thought reasoning, where it is difficult to supervise the whole trace but comparatively easy to verify the final result. OpenAI’s o1 report showed that reasoning accuracy improves with both reinforcement-learning train-time compute and test-time thinking com- pute [OpenAI, 2024]. DeepSeek-R1 then gave a more detailed public recipe in which DeepSeek-R1-Zero improves during RLVR-style training and produces longer reasoning traces as training progresses [DeepSeek-AI, 2025]. This em-

The episode ends when the model emits an end-of-sequence token or reaches a length limit. The verifier provides the terminal reward R(x, y). Thus the only nonzero task reward is at the end of the generated completion. The reward objective is

JR(θ) = Ex∼D, y∼πθ (·|x) [R(x, y)]. (18.1)

A common regularized objective subtracts a KL penalty from this reward to encourage the policy to not drift away from a reference policy by too far:

Jβ (θ) = JR(θ) − β Ex∼D [DKL(πθ(· | x) ‖ πref (· | x))] , (18.2)

and the training problem is to maximize Jβ (θ). Here πref is usually the initial supervised-finetuned or instruction-tuned model. The KL term discourages the policy from moving too far from a capable language model while the verifier reward pushes it toward completions that solve the task.

Rewards and verifiers. The simplest verifier extracts a final answer and compares it to a ground-truth answer. For code, the verifier may run unit tests; for symbolic tasks, it may call a parser, theorem checker, or algebra system. In practice, the reward usually also checks that the answer appears in the requested format.

Policy gradient. With the MDP formulation above, applying the policy- gradient identity from Chapter 21 to the reward term (JR in (18.1)) gives

∇θJR(θ) = Ex∼D, y∼πθ (·|x)

[ T

t=

(R(x, y) − bt) ∇θ log πθ(yt | x, y empirical average over sampled completions and token positions:

Ei,t[·] =

G

∑^ G

i=

Ti

∑Ti

t=

[·].

When the minibatch contains several prompts, this notation also includes the empirical average over prompts. Define the token-level likelihood ratio

r t( i)(θ) =

πθ(y( t i)| x, y(