Policy Gradient Methods › The Objective Function
Language AI HandbookAll chapters Policy Gradient Methods › The Objective FunctionOne section · Sections in this chapter3 / 14 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
To optimize our policy, we need a clear mathematical objective. In RLHF, we want to maximize the expected reward over all possible responses the model might generate. This expectation is important because language generation is inherently stochastic. The same prompt can lead to many different responses depending on which tokens get sampled at each step. Some responses will be excellent, others will be mediocre, and still others will be poor. We want to tune the model so that, on average, it produces responses that receive high rewards.
Think of the objective function as a report card for a policy, but one computed by averaging over all possible exams the policy might face. A policy scores well on this report card if, when we sample random responses from it, those responses tend to be good. The objective does not ask that every response be perfect; it asks that the distribution over responses be skewed toward high-quality outcomes.
Given a prompt , the model generates a response by sampling tokens according to its policy. The reward model then scores the complete response, giving us . Our objective is to find parameters that maximize the expected reward:
where:
- : the objective function we want to maximize
- : the parameters of the language model
- : the expectation over sequences sampled from the policy
- : the policy (language model) distribution conditioned on prompt
- : the reward scalar for the generated response
This expectation is taken over all possible responses the model could generate. Some responses will be excellent (high reward), others mediocre, others poor. The expectation weights each by its probability under the current policy. This weighting matters: if the model rarely generates a particular response, that response contributes little to the expected value, even if it happens to have high reward. A policy that occasionally produces brilliant responses but usually produces mediocre ones will score lower than a policy that consistently produces good responses.
We can expand this expectation using the probability of generating the complete sequence:
where:
- : summation over all possible output sequences in the vocabulary
- : probability of generating sequence given prompt
- : reward for the specific sequence
This summation form makes the objective's structure transparent, but it also reveals a computational challenge. The number of possible sequences is astronomically large: for a vocabulary of 50,000 tokens and sequences of length 100, there are possible sequences. We cannot enumerate them all. We need a way to optimize this objective without explicitly summing over all sequences.
The probability represents the likelihood of generating the entire response . It is computed as the product of individual token probabilities:
where:
- : the length of the generated sequence
- : the token generated at timestep
- : the history of tokens generated before step , that is
- : the probability the model assigns to token given the full context
This autoregressive factorization, which we have seen throughout our discussion of language models in earlier chapters, connects the policy formulation directly to how transformers and LSTMs generate text. Each factor in the product corresponds to one step of the generation process, one call to the model's forward function, one decision about which token to emit next. The full sequence probability is simply the product of all these individual decisions.
Why Direct Optimization Is Hard
You might wonder why we cannot just directly maximize by computing its gradient. The obstacle is that involves an expectation over sequences sampled from , and sampling is not a differentiable operation. When we sample a token from a probability distribution, we make a discrete choice, and discrete choices are not differentiable with respect to the parameters that shape that distribution.
Think of it this way: if you tweak the parameters of the model slightly so that the probability of token "excellent" increases by 0.001, you cannot directly measure how that change affects the expected reward. The reward depends on which specific tokens get sampled, and sampling is random. We need a mathematical trick to convert the gradient of an expectation into an expectation of gradients that we can estimate from samples.