Chapter 12

Reinforcement learning

Reinforcement learning is a way of training where a model takes actions, a reward scores each action, and the training raises the probability of actions with high rewards. In this chapter, the model is a language model, and an action is an answer to a question. For example, a model can solve 1,000 math problems, receive the reward 1 for each correct final answer and 0 for each wrong one, and become better at math from these rewards alone.

Fine-tuning (see Chapter 11) needs a correct answer for each example. Reinforcement learning only needs a way to score answers. For example, a program can check the final number of a solution, but it cannot write the steps that lead to it. The model can then learn steps that no example contains, and Chapter 13 shows models that are trained in this way.

12.1Policy gradient

The model that is trained is the policy. Let p(a)p(a) denote the probability that the policy gives the answer aa. Our task is to change the policy so that its answers get higher rewards on average.

The policy gradient method1 trains the policy in rounds. In each round, the policy gives several answers to the same question, and a reward scores each answer. The advantage A(a)A(a) of an answer aa is its reward minus the average reward of the round2. After this, the parameters move in the direction that increases the following sum over the answers a1,a2,…,ama_1, a_2, \ldots, a_m of the round:

A(a1)⋅ln⁡p(a1)+A(a2)⋅ln⁡p(a2)+⋯+A(am)⋅ln⁡p(am)A(a_1) \cdot \ln p(a_1) + A(a_2) \cdot \ln p(a_2) + \cdots + A(a_m) \cdot \ln p(a_m)

This means that answers with a positive advantage get a higher probability, and answers with a negative advantage get a lower probability.

As an example, consider the question “What is 17 + 25?” and a policy that only chooses among the answers 42, 32 and 43. The policy has one parameter z(a)z(a) for each answer, and p(a)p(a) is proportional to ez(a)e^{z(a)}. Initially, each parameter is 0, so each answer has probability 1/3. Suppose that the policy gives four answers in the first round, where the average reward is 0.5:

answer 42 42 32 43
reward 1 1 0 0
advantage 0.5 0.5 −0.5 −0.5

For this policy, the derivative of ln⁡p(a)\ln p(a) with respect to z(b)z(b) is 1−p(b)1 - p(b) when b=ab = a, and −p(b)-p(b) otherwise. Using this, we can calculate the change of each parameter. With the learning rate 1, z(42)z(42) grows by 2⋅0.5⋅2/3+2⋅0.5⋅1/3=12 \cdot 0.5 \cdot 2/3 + 2 \cdot 0.5 \cdot 1/3 = 1. A similar calculation shows that z(32)z(32) and z(43)z(43) fall by 0.5:

answer 42 32 43
zz before 0 0 0
zz after 1 −0.5 −0.5
pp before 1/3 1/3 1/3
pp after 0.69 0.15 0.15

After one round, the probability of the correct answer has grown from 1/3 to about 0.69.

Note that the average reward is subtracted because rewards can be all positive, for example scores between 0 and 10. Without the subtraction, each answer would push its own probability up, also an answer that is worse than the others. When every answer of a round gets the same reward, every advantage is 0, and the round changes nothing.

In practice, writing the answers usually takes most of the training time, because the policy writes each answer one token (a piece of a word) at a time.

12.2Reward models

A program can score an answer when its result can be checked: a number, or code that must pass tests. However, most requests have no such program, for example a request to write a polite reply to an upset customer.

The idea is to ask people to compare two answers to the same question and to train a reward model that gives each answer a score. Let s(x)s(x) denote the score of the answer xx. The reward model predicts that people prefer the answer xx to the answer yy with the following probability:

11+e−(s(x)−s(y))\frac{1}{1 + e^{-(s(x) - s(y))}}

For example, when s(x)=2s(x) = 2 and s(y)=0s(y) = 0, the probability is 1/(1+e−2)≈0.881/(1 + e^{-2}) \approx 0.88. The following picture shows the probability for each difference of the scores:

−4 −2 0 2 4 0 0.5 0.88 1 s(x) − s(y) probability

When the scores are equal, the probability is 0.5, and a larger difference moves it nearer to 0 or 1.

The training changes the reward model so that the comparisons that people made get high probabilities, and after this, the score s(a)s(a) can be used as the reward of the answer aa. This combination is called reinforcement learning from human feedback3.

12.3Reward hacking

Reward hacking is the case where the policy gets high rewards with answers that are not good. For example, if the people who compared the answers often preferred longer answers, the reward model gives higher scores to longer answers. The policy then learns to write long answers, also when a short answer is better. The reward goes up, and the quality does not.

For this reason, the reward usually includes a penalty for moving away from the starting model, i.e., the model before reinforcement learning: s(a)−β⋅ln⁡p(a)p0(a)s(a) - \beta \cdot \ln \frac{p(a)}{p_0(a)} where p0(a)p_0(a) is the probability of aa in the starting model, and β\beta sets the strength of the penalty. For example, when β=0.1\beta = 0.1, an answer whose probability has grown from 0.1 to 0.4 loses 0.1⋅ln⁡4≈0.140.1 \cdot \ln 4 \approx 0.14 of its reward.

A larger β\beta keeps the policy nearer to the starting model, so the policy forgets less (see Section 11.1). On the other hand, a larger β\beta also limits how much the policy can improve.


  1. The method was published in 1992 by R. J. Williams: Simple statistical gradient-following algorithms for connectionist reinforcement learning.↩︎

  2. This form of the advantage is based on a method published in 2024: DeepSeekMath: Pushing the Limits of Mathematical Reasoning in Open Language Models.↩︎

  3. Learning a reward from human comparisons was published in 2017: Deep reinforcement learning from human preferences. It was first applied to language models in 2019: Fine-Tuning Language Models from Human Preferences.↩︎