PPO Algorithm › The Complete PPO Objective

Michael BrenndoerferDecember 27, 202510 min read
Language AI HandbookAll chapters
Part 37 of 67Alignment and RLHF
Chapter 278PPO Algorithm › The Complete PPO Objective
Reading section 8 of 14 · The Complete PPO Objective · 10 minSection 8 of 14 · 10 min

Book contents

Section 8 of 14 · 10 min read

PPO Algorithm › The Complete PPO ObjectiveOne section · Sections in this chapter8 / 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

The full PPO objective combines three terms: policy improvement, value function training, and exploration. These components work together synergistically. Accurate value estimates enable meaningful advantages, while exploration discovers strategies that improve both the policy and value function. Training all three simultaneously with a shared network backbone allows the network to develop representations that serve all three purposes at once, improving sample efficiency compared to training separate networks.

The complete objective is:

LPPO(θ)=Et[LCLIP(θ)−c1LVF(θ)+c2S[πθ](st)]L^{\text{PPO}}(\theta) = \mathbb{E}_t\left[L^{\text{CLIP}}(\theta) - c_1 L^{\text{VF}}(\theta) + c_2 S[\pi_\theta](s_t)\right]

Each component serves a distinct purpose in training a capable agent:

  • LPPO(θ)L^{\text{PPO}}(\theta): the complete PPO objective function, to be maximized
  • Et\mathbb{E}_t: expectation over timesteps in the batch
  • LCLIP(θ)L^{\text{CLIP}}(\theta): the clipped surrogate objective for policy improvement, defined earlier
  • c1c_1: coefficient for the value function loss, typically 0.5
  • LVF(θ)L^{\text{VF}}(\theta): the value function loss, which encourages accurate value estimates
  • c2c_2: coefficient for the entropy bonus, typically 0.01
  • S[πθ](st)S[\pi_\theta](s_t): the entropy of the policy distribution at state sts_t, which encourages exploration
  • θ\theta: parameters of both the policy and value networks, when they share parameters
  • sts_t: the state at timestep tt

The value function loss has a negative sign because we minimize loss while maximizing the overall objective. This sign convention means that minimizing value function error increases the overall objective, aligning all components toward the same optimization direction.

The three terms work together synergistically. The clipped surrogate loss LCLIPL^{\text{CLIP}} improves the policy by increasing probabilities of high-advantage actions while respecting the trust region constraint. The value function loss LVFL^{\text{VF}} trains the value function to make accurate predictions, which are needed for computing meaningful advantages in future updates. The entropy term SS encourages exploration by penalizing overly deterministic policies, preventing premature convergence and helping the agent discover better strategies.

Value Function Loss

The value function Vϕ(s)V_\phi(s) predicts expected cumulative returns and is trained alongside the policy. Accurate value estimates are needed because the value function is the baseline in advantage computation. Without accuracy, advantages become noisy and unreliable, leading to high-variance policy gradients that destabilize training. Think of the value function as a sports commentator who tells you whether a play was good or bad: if the commentator has no idea what a typical play looks like, their assessments of "better than average" or "worse than average" are meaningless. The policy can only learn from advantage estimates if those estimates are grounded in an accurate model of expected future rewards.

The value function predicts expected cumulative future rewards starting from each state under the current policy. We train the value function by minimizing the mean squared error between its predictions and target values:

LVF(ϕ)=Et[(Vϕ(st)−Vttarget)2]L^{\text{VF}}(\phi) = \mathbb{E}_t\left[(V_\phi(s_t) - V_t^{\text{target}})^2\right]

Understanding each component reveals the standard regression structure of value function training:

  • LVF(ϕ)L^{\text{VF}}(\phi): the value function loss, the mean squared error
  • Et\mathbb{E}_t: expectation over timesteps in the batch
  • ϕ\phi: parameters of the value function network
  • Vϕ(st)V_\phi(s_t): the value function's prediction for state sts_t under current parameters
  • VttargetV_t^{\text{target}}: the target value we want the value function to predict
  • sts_t: the state at timestep tt
  • (Vϕ(st)−Vttarget)2(V_\phi(s_t) - V_t^{\text{target}})^2: the squared difference between prediction and target

The target value is computed as:

Vttarget=A^t+Vϕold(st)V_t^{\text{target}} = \hat{A}_t + V_{\phi_{\text{old}}}(s_t)

Each term in this target construction serves a specific purpose:

  • VttargetV_t^{\text{target}}: the target value for the value function to predict at timestep tt
  • A^t\hat{A}_t: the advantage estimate at timestep tt, computed using GAE
  • Vϕold(st)V_{\phi_{\text{old}}}(s_t): the value prediction from the old value function, before this update
  • ϕold\phi_{\text{old}}: parameters of the value function from the previous update
  • sts_t: the state at timestep tt

This target construction guides the value function to predict returns accurately using computed advantages. The advantage A^t\hat{A}_t represents how much better actual returns were than expected, or equivalently, the residual between actual returns and predicted returns. Adding this residual to the old value estimate Vϕold(st)V_{\phi_{\text{old}}}(s_t) yields an improved return estimate.

This bootstrapping approach allows the value function to learn from its own predictions while incorporating new information from observed rewards. The process is iterative: better value estimates lead to better advantage estimates, which lead to better policy updates, which generate better training data for the value function. Some implementations also clip the value function loss similarly to the policy loss, preventing large changes to the value function that might destabilize this iterative process. This value clipping is particularly useful in RLHF settings where the value function must adapt to reward model outputs that can change significantly during alignment training.

Entropy Bonus

Entropy measures randomness in the policy's action distribution. High entropy spreads probability across multiple actions instead of putting on one choice. Encouraging higher entropy prevents the policy from becoming deterministic too early, which keeps exploration alive and enables discovery of better strategies. Without the entropy bonus, PPO tends to quickly commit to whatever actions seem best in the current training data, potentially missing superior alternatives that have not yet been explored. The entropy bonus is a small but important regularizer that maintains the policy's flexibility throughout training.

For a policy distribution at state sts_t, entropy is defined as:

S[πθ](st)=−∑aπθ(a∣st)log⁡πθ(a∣st)S[\pi_\theta](s_t) = -\sum_a \pi_\theta(a|s_t) \log \pi_\theta(a|s_t)

Each component of this formula connects to information-theoretic concepts:

  • S[πθ](st)S[\pi_\theta](s_t): the entropy of the policy distribution at state sts_t
  • ∑a\sum_a: sum over all possible actions in the action space
  • aa: an action from the action space
  • πθ(a∣st)\pi_\theta(a|s_t): the probability of action aa in state sts_t under the current policy
  • log⁡πθ(a∣st)\log \pi_\theta(a|s_t): the log probability of action aa
  • θ\theta: parameters of the policy network
  • sts_t: the state at timestep tt

This entropy term encourages exploration by rewarding policies that maintain uncertainty over actions. The formula measures the expected information content, or surprise, in the policy's action distribution. Mathematically, when action probabilities are uniform, meaning all actions are equally likely, the sum of πθ(a∣st)log⁡πθ(a∣st)\pi_\theta(a|s_t) \log \pi_\theta(a|s_t) terms is maximized in magnitude, giving high entropy. When the policy is deterministic, one action has probability 1 and all others have probability 0, so the entropy collapses to zero.

Out[8]:
Visualization
Grouped bar chart showing probability distributions for five actions under three policies: a uniform green policy with equal bars, a moderate blue policy with one tall bar, and a concentrated red policy with one dominant bar near action 2.
Three policy distributions show how entropy measures exploration readiness. The uniform policy (green) assigns equal probability to all five actions with maximum entropy (1.61), representing maximal exploration where the policy is completely uncertain about which action to take. The moderate policy (blue) concentrates more probability on better actions, reducing entropy to 1.35 as uncertainty decreases. The concentrated policy (red) commits strongly to action 2 with entropy only 0.28, representing high confidence and minimal exploration. This spectrum illustrates the exploration-exploitation tradeoff.
Line chart of entropy decreasing from its maximum value near 1.61 as the probability of the best action increases from 0.2 to 1.0, with colored scatter points marking the three example policies.
Policy entropy versus action concentration reveals why entropy encourages exploration during training. When the policy maintains uniform probabilities across actions, entropy is maximized (approximately 1.61), giving maximum exploration and discovery of better strategies. As the policy concentrates probability on higher-value actions, entropy drops sharply, eventually approaching zero when the policy becomes fully deterministic. During training, the entropy bonus prevents this collapse to determinism too quickly, maintaining exploration that helps discover better strategies.

When all actions have similar probabilities, showing high uncertainty, entropy is maximized. This reflects the fact that an observer would be maximally uncertain about which action the policy will choose. When the policy becomes deterministic, meaning one action has probability near 1 and all others near 0, entropy approaches zero. The policy reveals little information because its behavior is predictable. While deterministic policies can be desirable in final deployment, during training we need exploration to discover better strategies.

The coefficient c2c_2 (typically 0.01) controls the exploration-exploitation tradeoff. Higher values encourage exploration but may slow convergence to optimal behavior. Lower values enable faster convergence but risk getting stuck in suboptimal local minima. In language model fine-tuning, the entropy coefficient is often set quite small because the base model already has a well-developed prior over language, and aggressive exploration would destroy linguistic coherence. The goal is gentle nudging toward human-preferred outputs, not wholesale behavioral reshaping.

Putting It Together

The typical coefficient values that Schulman et al. used in the original paper are:

  • c1=0.5c_1 = 0.5: weight for value function loss
  • c2=0.01c_2 = 0.01: weight for entropy bonus
  • ϵ=0.2\epsilon = 0.2: clipping parameter

Sharing parameters between policy and value networks, which is common in practice, combines all three losses for joint optimization. This parameter sharing encourages the network to learn representations useful for both predicting values and selecting actions. It often improves sample efficiency compared to separate networks. The shared layers act as a general-purpose feature extractor: features that help predict which actions lead to good outcomes (value function) are often the same features that should drive action selection (policy). The separate output heads then specialize these shared representations for their specific task.