Equations · Schulman et al., “Proximal Policy Optimization Algorithms” (2017)

The PPO clipped objective, explained term by term

PPO improves a policy using data collected by a slightly older copy of itself. The objective rewards making good actions more likely and bad ones less likely, but the clip removes any incentive to move the probability ratio more than ϵ\epsilon away from 1 in one update. The objective is maximized.

LCLIP(θ)=E^t[min⁡(rt(θ)A^t, clip⁡(rt(θ),1−ϵ,1+ϵ)A^t)],rt(θ)=πθ(at∣st)πθold(at∣st)L^{\mathrm{CLIP}}(\theta) = \hat{\mathbb{E}}_t\left[\min\left(r_t(\theta)\hat{A}_t,\ \operatorname{clip}\left(r_t(\theta), 1 - \epsilon, 1 + \epsilon\right)\hat{A}_t\right)\right], \quad r_t(\theta) = \frac{\pi_\theta(a_t \mid s_t)}{\pi_{\theta_{\mathrm{old}}}(a_t \mid s_t)}

Open in the decoder →

Term by term

  1. rt(θ)=πθ(at∣st)πθold(at∣st)r_t(\theta) = \frac{\pi_\theta(a_t \mid s_t)}{\pi_{\theta_{\mathrm{old}}}(a_t \mid s_t)}

    The probability ratio: how much more (or less) likely the new policy makes the action ata_t taken in state sts_t than the policy that collected the data. It is 1 at the start of each update. Here rtr_t is not a reward, despite the letter.

  2. A^t\hat{A}_t

    The estimated advantage: how much better action ata_t turned out than the policy's average in that state. Positive means "do this more". The hat marks an estimate, usually from GAE.

  3. clip⁡(rt(θ),1−ϵ,1+ϵ)\operatorname{clip}(r_t(\theta), 1 - \epsilon, 1 + \epsilon)

    The ratio, forced into [1−ϵ,1+ϵ][1 - \epsilon, 1 + \epsilon]. ϵ=0.2\epsilon = 0.2 in the paper.

  4. min⁡(…,…)\min(\ldots, \ldots)

    The smaller of the clipped and unclipped terms: a pessimistic bound. The clip can only lower the objective, never raise it, so the policy gains nothing by moving the ratio past the clip range in the helpful direction.

  5. E^t[…]\hat{\mathbb{E}}_t[\ldots]

    An empirical average over the time steps in the collected batch.

  6. LCLIPL^{\mathrm{CLIP}}

    The superscript is a label naming this objective, not a power. The full PPO loss adds a value-function error and an entropy bonus.

A small worked example

With ϵ=0.2\epsilon = 0.2: a good action, A^t=2\hat{A}_t = 2, whose ratio has grown to rt=1.5r_t = 1.5. Unclipped: 33; clipped: 1.2×2=2.41.2 \times 2 = 2.4; the min is 2.42.4, a constant, so there is no gradient pushing the ratio further. A bad action, A^t=−1\hat{A}_t = -1, with rt=0.5r_t = 0.5: unclipped −0.5-0.5, clipped −0.8-0.8; the min is −0.8-0.8, again flat in θ\theta.

In code

import torch

def ppo_loss(logp_new, logp_old, adv, eps=0.2):
    ratio = torch.exp(logp_new - logp_old)          # r_t(theta)
    unclipped = ratio * adv
    clipped = torch.clamp(ratio, 1 - eps, 1 + eps) * adv
    return -torch.min(unclipped, clipped).mean()    # minimize the negative

Every symbol in it

πθ(a∣s)\pi_\theta(a \mid s)Pipθ(x)p_\theta(x)Thetap(y∣x)p(y \mid x)Given (conditional bar)st,at,rt,st+1s_t, a_t, r_t, s_{t+1}State s and action aab\frac{a}{b}FractionAπ(s,a)=Qπ(s,a)−Vπ(s)A^\pi(s, a) = Q^\pi(s, a) - V^\pi(s)Advantage Ay^\hat{y}Hat (estimate)clip⁡(rt(θ),1−ϵ,1+ϵ)\operatorname{clip}(r_t(\theta), 1 - \epsilon, 1 + \epsilon)Clipϵ\epsilonEpsilonmax⁡(0,x)\max(0, x)Max and minEx∼p[f(x)]\mathbb{E}_{x \sim p}[f(x)]Expectationxi,  Wq,  pθ,  dmodelx_i,\; W_q,\; p_\theta,\; d_{\text{model}}Subscript (index or label)x2,  βt,  WQx^2,\; \beta^t,\; W^QSuperscript (power or label)J(θ)=Eτ∼πθ[R(τ)]J(\theta) = \mathbb{E}_{\tau \sim \pi_\theta}\left[R(\tau)\right]Objective J

Other equations