5.2.9Deep & Advanced RL

Proximal Policy Optimization (PPO)

1,893 words9 min readdifficulty · medium5 backlinks

WHY does PPO exist?


WHAT are we actually optimizing? (Derivation from scratch)

Step 1 — The policy gradient objective

We want to maximize expected return J(θ)=Eτπθ[R(τ)]J(\theta)=\mathbb{E}_{\tau\sim\pi_\theta}[R(\tau)]. The policy gradient theorem gives:

θJ(θ)=E[θlogπθ(as)A^(s,a)]\nabla_\theta J(\theta)=\mathbb{E}\big[\nabla_\theta \log\pi_\theta(a|s)\,\hat A(s,a)\big]

Why? A^\hat A (the advantage) says "how much better than average was this action." We push up logπ\log\pi for good actions (A^>0\hat A>0), push down for bad ones.

Step 2 — Reuse old data via importance sampling

We collect data with an old policy πθold\pi_{\theta_{old}}, then optimize a new πθ\pi_\theta. To reuse the old samples, we reweight:

Eaπθold ⁣[πθ(as)πθold(as)A^]\mathbb{E}_{a\sim\pi_{\theta_{old}}}\!\left[\frac{\pi_\theta(a|s)}{\pi_{\theta_{old}}(a|s)}\hat A\right]

Define the probability ratio:

rt(θ)=πθ(atst)πθold(atst)r_t(\theta)=\frac{\pi_\theta(a_t|s_t)}{\pi_{\theta_{old}}(a_t|s_t)}

Why a ratio? rt>1r_t>1 means the new policy makes this action more likely than the old one did. This surrogate objective LCPI=E[rtA^t]L^{CPI}=\mathbb{E}[r_t\hat A_t] is the "conservative policy iteration" objective.

Step 3 — The clipped surrogate objective

LCLIP(θ)=Et[min(rt(θ)A^t, clip(rt,1ϵ,1+ϵ)A^t)]\boxed{L^{CLIP}(\theta)=\mathbb{E}_t\Big[\min\big(r_t(\theta)\hat A_t,\ \text{clip}(r_t,1-\epsilon,1+\epsilon)\hat A_t\big)\Big]}

Figure — Proximal Policy Optimization (PPO)

HOW is the advantage estimated? (GAE)

PPO uses Generalized Advantage Estimation. With TD error δt=rt+γV(st+1)V(st)\delta_t=r_t+\gamma V(s_{t+1})-V(s_t):

A^tGAE=l=0(γλ)lδt+l\hat A_t^{GAE}=\sum_{l=0}^{\infty}(\gamma\lambda)^l\,\delta_{t+l}

Why? λ\lambda trades bias vs variance: λ=0\lambda=0A^=δt\hat A=\delta_t (low variance, biased, 1-step); λ=1\lambda=1 → full Monte-Carlo return (unbiased, high variance). Typical λ0.95\lambda\approx0.95.


The full PPO loss

L(θ)=LCLIPpolicy  c1(Vθ(s)Vttarget)2value loss+  c2H[πθ]entropy bonusL(\theta)=\underbrace{L^{CLIP}}_{\text{policy}}-\;c_1\underbrace{(V_\theta(s)-V_t^{target})^2}_{\text{value loss}}+\;c_2\underbrace{H[\pi_\theta]}_{\text{entropy bonus}}

Why entropy HH? Encourages exploration by keeping the policy from becoming deterministic too fast. Why one shared step? Actor and critic often share a network; we run K epochs of minibatch SGD on the same collected batch — that's the sample-efficiency win over REINFORCE.


Worked examples


Recall Feynman: explain to a 12-year-old

Imagine you're learning to shoot basketball hoops. After some shots you know which moves helped and which didn't. You want to change your technique — but if you change everything at once based on a few lucky shots, you'll ruin your form. So you make a rule: "I'll adjust my technique, but never by more than a little bit at a time." If a move worked, I do a bit more of it — but I stop once I've already changed enough. If a move was bad, I do a bit less — but again, only a little. That "only change a little" rule is the clip in PPO. It lets you learn fast without wrecking what already works.


Active recall


What is the probability ratio rtr_t in PPO?
rt(θ)=πθ(atst)/πθold(atst)r_t(\theta)=\pi_\theta(a_t|s_t)/\pi_{\theta_{old}}(a_t|s_t) — how much more/less likely the new policy makes the sampled action vs the old policy.
Write the PPO clipped objective.
LCLIP=Et[min(rtA^t, clip(rt,1ϵ,1+ϵ)A^t)]L^{CLIP}=\mathbb{E}_t[\min(r_t\hat A_t,\ \text{clip}(r_t,1-\epsilon,1+\epsilon)\hat A_t)]
Why does PPO take the min of clipped and unclipped terms?
To form a pessimistic lower bound: it removes incentive to move too far when it helps, but still lets you fully correct updates that move in the wrong direction.
For A^>0\hat A>0 and rt>1+ϵr_t>1+\epsilon, what is the objective gradient?
Zero — the clipped branch is active, so no incentive to increase rtr_t further.
What problem does clipping replace from TRPO?
The hard KL trust-region constraint; clipping approximates it with cheap first-order updates.
What does ϵ\epsilon control and its typical value?
The trust-region width [1ϵ,1+ϵ][1-\epsilon,1+\epsilon]; typically ϵ=0.2\epsilon=0.2.
What is GAE and what does λ\lambda trade off?
Generalized Advantage Estimation, A^t=(γλ)lδt+l\hat A_t=\sum(\gamma\lambda)^l\delta_{t+l}; λ\lambda trades bias (low) vs variance (high), typically 0.95.
Why can PPO run multiple SGD epochs on one batch?
Importance sampling (rtr_t) lets it reuse data collected by the old policy, giving sample efficiency over REINFORCE.
What are the three terms of the full PPO loss?
Clipped policy surrogate, value-function MSE loss, and entropy bonus for exploration.
Why is the entropy bonus included?
To keep the policy stochastic and encourage exploration, preventing premature collapse to a deterministic policy.

Connections

Concept Map

only valid locally

hard KL trust region

first-order clip

90 percent benefit 10 percent cost

reuse old data

surrogate

maximize freely fails

fix by clipping

min of clipped and unclipped

removes incentive to stray

weights log-prob update

Policy gradient objective

Big steps collapse policy

TRPO second-order

PPO clipped objective

Importance sampling ratio r_t

L_CPI = r_t times A_hat

Ratio explodes on noisy A_hat

clip r_t to 1-eps 1+eps

L_CLIP objective

Advantage A_hat

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, PPO ka core idea bahut simple hai. Policy gradient methods me hum policy ko improve karte hain, par problem yeh hai ki agar ek hi update me policy ko bahut zyada change kar diya, toh policy collapse ho jaati hai — kyunki gradient sirf current policy ke aas-paas hi reliable hota hai. Toh PPO bolta hai: "improve toh karo, par thoda-thoda, apne purane policy ke close raho." Isko hi trust region kehte hain.

Iske liye PPO ek ratio use karta hai: rt=πnew/πoldr_t = \pi_{new}/\pi_{old} — matlab naya policy us action ko kitna zyada ya kam likely bana raha hai. Fir yeh ratio ko [0.8,1.2][0.8, 1.2] me clip kar deta hai (jab ϵ=0.2\epsilon=0.2). Agar action accha tha (A^>0\hat A > 0) aur ratio bahut upar chala gaya, PPO ka gradient zero ho jaata hai — matlab "bas, ab aur mat badha." Agar action bura tha, toh usko zyada suppress karne se rokta hai. Par ek important baat: agar galti se humne bure action ko zyada likely kar diya, toh min wali trick usko fully penalize karti hai — yaani apni galti sudharne ki hamesha permission hai.

Yeh matter isliye karta hai kyunki PPO ek hi data batch pe multiple SGD epochs chala sakta hai (importance sampling ki wajah se), toh REINFORCE se zyada sample-efficient hai. Aur TRPO jaisi complicated second-order maths (Fisher matrix, conjugate gradient) ki zaroorat nahi — sirf normal Adam optimizer se kaam ho jaata hai. Isiliye PPO aaj real-world RL (robotics, ChatGPT ka RLHF) me sabse zyada use hota hai — simple, stable, aur efficient.

Go deeper — visual, from zero

Test yourself — Deep & Advanced RL

Connections