RL @ ECE-UofT - Lecture 10: Trust Region PGM -- TRPO and PPO
Overview
Ali Bereyhi explains why vanilla and baseline policy gradient methods can collapse even with larger minibatches: their surrogate objectives use importance sampling, whose variance grows when the candidate policy differs greatly from the sampling policy. He derives trust-region updates, presents TRPO's KL constraint, Fisher-style Hessian estimate, conjugate-gradient solve, and backtracking line search, then contrasts PPO's clipped surrogate and discusses short-term sample reuse and practical RL libraries.
Key takeaways
- Policy-gradient collapse is not explained only by small batches: importance sampling can have high variance when the candidate policy πθ differs greatly from the sampling policy πθk.
- A trust region constrains the policy distribution, commonly through average KL divergence, because small parameter changes alone do not guarantee small behavioral changes in a neural network.
- Natural policy gradient uses the inverse KL Hessian to reshape the ordinary gradient, but forming and inverting a dense Hessian over network parameters is computationally impractical.
- TRPO makes the natural-gradient approach feasible with a sampled Fisher-style Hessian estimate, conjugate-gradient solution of Hy = g, and backtracking line search to meet the KL bound.
- PPO's clipped surrogate uses the importance ratio and an ε-dependent clipping range to remove incentives for excessively increasing or decreasing action probabilities, avoiding TRPO's second-order machinery.
- PPO can reuse collected trajectories for multiple epochs, but data must be refreshed as policies drift; trajectories from a mixed-policy buffer need their own behavior-policy probabilities.
Chapters
- The policy gradient objective J maximizes policy value across states sampled according to the policy-dependent state distribution.
- The policy gradient theorem expresses updates using the action value and the gradient of the log policy.
- A state-dependent baseline, commonly the value function, gives the advantage Q minus V without changing the expected gradient.
- The basic advantage update multiplies a TD-style advantage estimate by the log-policy gradient.
- The lecture assumes access to a value function for now; the next lecture will replace it with a learned value network.
- Ali Bereyhi introduces TRPO and PPO as practical approaches for stabilizing policy-gradient training.
- Policy-gradient training can show reward saturation followed by a sudden collapse after a seemingly small update.
- A common initial explanation is high variance from estimating gradients with only one trajectory.
- A minibatch averages gradients from B trajectories sampled under the same policy, but the lecture's comparison shows that batching alone does not eliminate sharp drops.
- The baseline policy-gradient update can be viewed as gradient ascent on the expected policy objective.
- A surrogate function is defined at each current parameter setting, with the same gradient as the true objective at that point.
- This perspective turns each policy update into optimization over a sequence of locally anchored surrogate functions.
- Only the candidate policy πθ varies in the importance-sampling ratio; the old policy πθk and advantage samples are fixed.
- At θ = θk, differentiating the likelihood ratio yields the log-policy gradient.
- The surrogate's gradient at the current parameters therefore equals the original policy gradient.
- A fixed learning rate takes a local step on the surrogate, after which a new surrogate is formed at the next parameters.
- Maximizing each surrogate without restricting the candidate policy can move far from the current policy.
- The surrogate is useful for estimating improvement locally, but its unrestricted maximum may not be reliable.
- The surrogate weights advantages sampled under πθk by the ratio πθ divided by πθk.
- That ratio is an importance-sampling correction for estimating the candidate policy's expected advantage from data collected under the current policy.
- Repeatedly maximizing the surrogate amounts to choosing a candidate policy based on this reweighted estimate.
- Importance sampling can recover an expectation under target distribution Q using samples from distribution P and weights Q/P.
- The weighted estimate has a variance term involving Q/P, unlike direct sampling from Q.
- When P and Q differ substantially, rare samples with large weights can make the estimate highly variable.
- In the policy surrogate, the sampling policy is πθk and the target policy is πθ.
- A candidate policy that changes too much can produce extreme importance ratios and unreliable advantage estimates.
- Increasing batch size can reduce sampling noise, but it does not remove the underlying variance caused by a large policy mismatch.
- The proposed fix is to maximize the surrogate only among policies sufficiently close to the current policy.
- Small parameter changes do not guarantee small behavioral changes in a neural network, since a sensitive parameter can strongly alter the policy.
- The constraint should compare action distributions, rather than merely limiting the distance between parameter vectors.
- The lecture uses KL divergence to compare the candidate action distribution with the current policy's distribution.
- The average KL is taken over states sampled under the current policy, πθk.
- KL divergence is nonnegative and zero for identical distributions, but it is asymmetric and is not a true distance.
- TRPO maximizes the importance-sampled surrogate subject to an upper bound Dmax on average KL divergence.
- The KL bound defines a region of policy changes for which the surrogate estimate is considered trustworthy.
- The trust-region update trades larger potential gains for more conservative, better-controlled policy changes.
- A Lagrange multiplier can convert the constrained objective into a penalized optimization problem.
- Because the KL expression is difficult to optimize directly over neural-network parameters, the lecture introduces local Taylor approximations.
- The resulting natural policy-gradient update is an approximate, closed-form route to the constrained step.
- The surrogate is expanded around θk and approximated by a linear function of θ minus θk.
- The surrogate is zero at θk, so the local objective reduces to the gradient at θk dotted with the parameter change.
- That gradient is the ordinary policy gradient, which can be estimated from advantage-weighted log-policy gradients.
- The KL divergence is expanded around θk to second order.
- At θk, the KL value and its first derivative are zero, leaving a quadratic constraint involving the KL Hessian.
- The local optimization becomes a linear objective subject to a quadratic trust-region constraint.
- The approximate constrained problem yields a parameter step proportional to the inverse KL Hessian times the policy gradient.
- The bound Dmax controls step magnitude, replacing a conventional fixed learning rate in this derivation.
- The Hessian inverse can also change the gradient direction to account for how parameter changes affect the policy distribution.
- The natural-gradient step is only approximate, so it does not by itself guarantee that the actual update satisfies the KL constraint.
- Computing a full Hessian over all policy-network parameters is expensive, and directly inverting an n by n matrix has roughly cubic cost in standard methods.
- For a network with around 10^5 parameters, a dense matrix inverse is impractical.
- TRPO adds backtracking line search to check and shrink candidate updates that exceed the trust region.
- It estimates the KL Hessian from policy-gradient information rather than calculating every second derivative directly.
- It uses conjugate gradient to obtain the needed Hessian-inverse direction without explicitly forming the inverse.
- TRPO first tries the full natural-gradient step, then checks the resulting KL divergence.
- If the update exceeds the target region, the step is repeatedly scaled by powers of a factor α less than 1.
- The line search changes step length while preserving the computed natural-gradient direction.
- The required direction can be written as the solution y of Hy = g, where H is the KL Hessian and g is the policy gradient.
- Conjugate gradient solves this linear system iteratively, avoiding an explicit dense inverse.
- TRPO uses the resulting vector in the natural-gradient update and its normalization.
- The KL Hessian at the current policy can be expressed as an expectation of outer products of log-policy gradients.
- Each sampled gradient vector contributes a rank-one outer product; averaging these estimates gives a Fisher-style matrix estimate.
- The required log-policy gradients are already computed during the policy-gradient update.
- TRPO samples multiple trajectories under the current policy and computes advantage-weighted log-policy gradients.
- It estimates the Hessian, uses conjugate gradient to solve for the update direction, and applies backtracking to satisfy the KL limit.
- The resulting procedure combines policy-gradient estimation, second-order geometry, and constrained step checking.
- PPO targets the same policy-change problem without TRPO's explicit KL-Hessian solve and line search.
- It focuses on the importance ratio πθ(a|s) divided by πθk(a|s), which directly appears in the surrogate and its variance.
- Keeping that ratio near one is used as an intuitive way to discourage large policy changes.
- PPO compares the ratio-weighted advantage with a clipped alternative and takes the minimum of the two terms.
- The clipping function Lε is piecewise linear, using 1 + εx for positive x and 1 - εx for negative x.
- The clipped objective reduces the incentive for updates that push the probability ratio too far from one.
- A positive advantage means the sampled action performed better than the state's expected value, so the policy should increase its probability.
- When the probability ratio exceeds 1 + ε, the clipped term limits further objective improvement from increasing that action's probability.
- For ratios below the clipping threshold, the ordinary importance-sampled advantage still supplies the update signal.
- A negative advantage indicates an action performed worse than the expected value, so the policy should reduce its probability.
- The clipped objective limits the incentive to push the ratio below 1 - ε.
- Together, the positive and negative cases discourage excessively large probability changes without explicitly optimizing a KL constraint.
- PPO optimizes the mean clipped surrogate with ordinary gradient ascent and can run multiple optimization epochs on the collected data.
- The implementation needs the sampled advantage, the current policy probability, and the stored probability under the sampling policy.
- Unlike TRPO, PPO avoids explicit Hessian construction, conjugate-gradient solving, and KL-based backtracking.
- TRPO and PPO can reuse a batch for multiple updates while candidate policies remain sufficiently close to the sampling policy.
- For a buffer containing trajectories from multiple policies, each trajectory must retain its own behavior-policy probability for the importance ratio.
- Old data eventually stops supporting useful updates, so the buffer or dataset must be refreshed.
- The lecture contrasts short-term PPO and TRPO data reuse with DQN experience replay, whose off-policy updates can keep using older samples.
- Policy-gradient samples become less useful as the current policy moves away from the policy that generated them.
- The trust-region restriction supports stability but limits how far the policy can move using any one batch.
- The next lecture will introduce a value network to estimate advantages instead of assuming access to an ideal value function.
- Ali Bereyhi points students to TorchRL, Stable Baselines3, CleanRL, and RLlib for maintained implementations and reference code.
- Stable Baselines3 is highlighted as a source of PPO, TRPO, and Deep Q-learning implementations and documentation.
- TRPO and PPO address policy-gradient instability by limiting updates that would make importance-sampling estimates unreliable.
- PPO is more commonly used in practice because its clipped objective is simpler and less computationally demanding than TRPO.
- Policy-gradient methods generally trade some sample efficiency for more controlled policy updates.
Transcript excerpt
0:00 how we saw. Okay, so uh we talked about policy gradient methods last time. So, do you remember what was the main idea? So, the difference to the value-based approach is is that here we are optimizing the policy net, right? And then in order to do that, the question was, okay, you have the policy network. How you can find a gradient that this is going to update this network size that the network goes towards being optimal, right? And then we talked about one theorem which was the policy gradient theorem, right? Remember that, yes? So, the policy gradient theorem, I mean, we had the basic form, but yeah, this is the last one that we ended up with. What
0:50 did it say? It says that you have an objective that is J, right? You remember what was J? J was the difference or it was actually the value of your policy pi theta expected over all possible states, right? So, if you have policy pi theta and then you consider one particular state, then the value of that particular state you want to maximize it, right? But the point is that you don't want to do it for a single state. You want to do it for all the states, right? We talked about it. It looks like a multi-objective optimization and if you want to do that, then you end up with such expression. If
1:37 you remember that guy was the expectation of the value of this policy at all states, right? And then if you want to compute the gradient of this guy, that gradient is essentially something that you can use to update your policy network because at the end of the day you want to optimize or maximize the value of your policy,
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Ali Bereyhi.