




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





We will present a model-free algorithm called REINFORCE that does not require the notion of value functions and Q functions. It turns out to be more convenient to introduce REINFORCE in the finite horizon case, which will be assumed throughout this note: we use τ = (s 0 , a 0 ,... , sT − 1 , aT − 1 , sT ) to denote a trajectory, where T < ∞ is the length of the trajectory. Moreover, REINFORCE only applies to learning a randomized policy. We use πθ(a|s) to denote the probability of the policy πθ outputting the action a at state s. The other notations will be the same as in previous lecture notes. The advantage of applying REINFORCE is that we only need to assume that we can sample from the transition probabilities {Psa} and can query the reward function R(s, a) at state s and action a,^1 but we do not need to know the analytical form of the transition probabilities or the reward function. We do not explicitly learn the transition probabilities or the reward function either. Let s 0 be sampled from some distribution μ. We consider optimizing the expected total payoff of the policy πθ over the parameter θ defined as
η(θ) , E
t=
γtR(st, at)
(^1) In these notes we will work with the general setting where the reward depends on both
the state and the action.
Recall that st ∼ Pst− 1 at− 1 and at ∼ πθ(·|st). Also note that η(θ) = Es 0 ∼μ [V πθ^ (s 0 )] if we ignore the difference between finite and infinite hori- zon. We aim to use gradient ascent to maximize η(θ). The main challenge we face here is to compute (or estimate) the gradient of η(θ) without the knowledge of the form of the reward function and the transition probabilities. Let Pθ(τ ) denote the distribution of τ (generated by the policy πθ), and let f (τ ) =
t=0 γ tR(st, at). We can rewrite η(θ) as
η(θ) = Eτ ∼Pθ [f (τ )] (21.2)
We face a similar situation in the variational auto-encoder (VAE) setting covered in the previous lectures, where we need to take the gradient with respect to a variable that shows up under the expectation — the distribution Pθ depends on θ. Recall that in VAEs, we used the reparameterization trick to address this problem. However, it does not apply here because we do not know how to compute the gradient of the function f. (We only have an efficient way to evaluate the function f by taking a weighted sum of the observed rewards, but we do not necessarily know the reward function itself to compute the gradient.) The REINFORCE algorithm uses another approach to estimate the gra- dient of η(θ). We start with the following derivation:
∇θEτ ∼Pθ [f (τ )] = ∇θ
Pθ(τ )f (τ )dτ
∇θ(Pθ(τ )f (τ ))dτ (swap integration with gradient)
(∇θPθ(τ ))f (τ )dτ (because f does not depend on θ)
Pθ(τ )(∇θ log Pθ(τ ))f (τ )dτ
(because ∇ log Pθ(τ ) = ∇ PPθθ (^ (ττ )^ ) ) = Eτ ∼Pθ [(∇θ log Pθ(τ ))f (τ )] (21.3)
Now we have a sample-based estimator for ∇θEτ ∼Pθ [f (τ )]. Let τ (1),... , τ (n) be n empirical samples from Pθ (which are obtained by running the policy πθ for n times, with T steps for each run). We can estimate the gradient of
Interpretation of the policy gradient formula (21.8). The quantity ∇θ log Pθ(τ ) =
t=0 ∇θ^ log^ πθ(at|st) is the score of the trajectory. It points in the direction of parameter change that locally increases the log-probability, and hence the probability, of the trajectory τ (or of choosing the actions a 0 ,... , aT − 1 along that trajectory). The scalar f (τ ) is the total payoff of this trajectory. Thus, by taking a gradient step, intuitively we are trying to increase the likelihood of sampled trajectories, but with a different emphasis or weight for each τ. If τ is very rewarding, then f (τ ) is large and the update strongly reinforces the direction that increases the probability of that trajectory; if τ has low payoff, the same direction receives a smaller weight. An interesting fact that follows from formula (21.3) is that
Eτ ∼Pθ
t=
∇θ log πθ(at|st)
To see this, we take f (τ ) = 1 (that is, the reward is always a constant), then the LHS of (21.8) is zero because the payoff is always a fixed constant∑ T t=0 γ
t. Thus the RHS of (21.8) is also zero, which implies (21.9). In fact, one can verify that Eat∼πθ (·|st)∇θ log πθ(at|st) = 0 for any fixed t and st.^2 This fact has two consequences. First, we can simplify formula (21.8) to
∇θη(θ) =
t=
Eτ ∼Pθ
∇θ log πθ(at|st) ·
j=
γj^ R(sj , aj )
t=
Eτ ∼Pθ
∇θ log πθ(at|st) ·
j≥t
γj^ R(sj , aj )
where the second equality follows from
Eτ ∼Pθ
∇θ log πθ(at|st) ·
0 ≤j whereas the inner expectation is over the randomness of at (conditioned on s 0 , a 0 ,... , at− 1 , st.) We see that we’ve made the estimator slightly simpler. The second consequence of Eat∼πθ (·|st)∇θ log πθ(at|st) = 0 is the following: for any value B(st) that only depends on st, it holds that
Eτ ∼Pθ [∇θ log πθ(at|st) · B(st)] = E [E [∇θ log πθ(at|st)|s 0 , a 0 ,... , st− 1 , at− 1 , st] B(st)] = 0 (because E [∇θ log πθ(at|st)|s 0 , a 0 ,... , st− 1 , at− 1 , st] = 0)
Again here we used the law of total expectation. The outer expecta- tion in the second line above is over the randomness of s 0 , a 0 ,... , at− 1 , st, whereas the inner expectation is over the randomness of at (conditioned on s 0 , a 0 ,... , at− 1 , st.) It follows from equation (21.10) and the equation above that
∇θη(θ) =
t=
Eτ ∼Pθ
∇θ log πθ(at|st) ·
j≥t
γj^ R(sj , aj ) − γtB(st)
t=
Eτ ∼Pθ
∇θ log πθ(at|st) · γt
j≥t
γj−tR(sj , aj ) − B(st)
Therefore, we will get a different estimator for estimating the ∇η(θ) with a different choice of B(·). The benefit of introducing a proper B(·) — which is often referred to as a baseline — is that it helps reduce the variance of the estimator.^3 It turns out that a near optimal estimator would be the expected
future payoff E
T − 1 j≥t γ
j−tR(sj , aj )|st
, which is pretty much the same as the
value function V πθ^ (st) (if we ignore the difference between finite and infinite horizon.) Here one could estimate the value function V πθ^ (·) in a crude way, because its precise value doesn’t influence the mean of the estimator but only the variance. This leads to a policy gradient algorithm with baselines stated in Algorithm 7.^4
(^3) As a heuristic but illustrating example, suppose for a fixed t, the future reward ∑T − 1 j≥t γ j−tR(sj , aj ) randomly takes two values 1000 + 1 and 1000 − 2 with equal proba-
bility, and the corresponding values for ∇θ log πθ (at|st) are vector z and −z. (Note that because E [∇θ log πθ (at|st)] = 0, if ∇θ log πθ (at|st) can only take two values uniformly, then the two values have to be two vectors in opposite directions.) In this case, without subtracting the baseline, the estimators take two values (1000 + 1)z and −(1000 − 2)z, whereas after subtracting a baseline of 1000, the estimator has two values z and 2z. The
A convenient way to separate the ideas in PPO is to first ignore baselines and advantages, and focus only on how to reuse data sampled from an old policy. Let θold be the parameter that generated the sampled trajectories. For a generic sampled trajectory τ = (s 0 , a 0 ,... , sT − 1 , aT − 1 , sT ), define the reward-to-go by R≥t =
j=t γ
j−tR(sj , aj ). Suppose we have taken a few gradient updates and arrived at a policy θ that is not far from θold. We would like to estimate an update for the policy πθ using trajectories sampled from πθold. In the policy-gradient estimator, each time step contributes a reward-to-go term. If the state st were fixed, then the basic correction for changing the action distribution from πθold to πθ is the likelihood ratio
rt(θ) =
πθ(at|st) πθold (at|st)
Let dθold,t denote the distribution of st under the old policy. The correspond- ing importance-weighted local surrogate has the form
JIS(θ) =
t=
Est∼dθold,t Eat∼πθold (·|st) [rt(θ)R≥t]. (21.16)
Here the outer expectation is still over states visited by the old policy, while the likelihood ratio converts the inner action expectation from πθold to πθ. Thus this expression should be viewed as a local surrogate: it corrects the sampled actions, but it still uses the state distribution and rollouts generated by the old policy. A simple importance-sampling-based gradient algorithm would collect trajectories under πθold , compute the reward-to-go, and then take several gradient steps on the local objective (21.16). PPO starts from this local surrogate and adds two changes. First, it replaces the raw reward-to-go by subtracting a value-function baseline, turning it into an advantage estimate with lower variance, and adds a KL-divergence regularizer, as in (18.2), to penalize drift from the old policy. Second, it clips the likelihood ratio so that if θ has drifted too far from the old policy on a sampled action, the update does not keep increasing the incentive to move farther in that direction. Recall that subtracting a state-dependent baseline from the reward-to-go does not change the expected policy-gradient direction, but it can reduce the variance of the estimator. In particular, we can subtract an approximated value-function baseline under the old policy and use the advantage estimate
 t = R≥t − Vold(st).
This gives the advantage-based loss
Jadv(θ) = −
t=
Est∼dθold,t Eat∼πθold (·|st)
rt(θ)Â t
which has the same importance-weighted structure as (21.16), but with R≥t replaced by  t. PPO then clips the per-time-step contribution by replacing rt(θ)  t with
Ct(θ) = min
rt(θ)  t, clip1−clip,1+clip  t
And the PPO surrogate loss is then
JPPO(θ) = −
t=
Est∼dθold,t Eat∼πθold (·|st) [Ct(θ)] , (21.19)
Here the min operator can be understood by considering the sign of the advantage. If  t > 0, then the sampled action leads to a reward-to-go that is better than the baseline, so the update would like to increase the probability of that action. When rt(θ) ≤ 1 + clip, PPO uses the ordinary contribution
rt(θ)  t; when rt(θ) > 1 + clip, the clipped term caps the gain at (1 + clip) t, so the objective stops rewarding further increases in that action probability; this makes sense because the probability of the action under the current policy is already substantially higher than the probability under πθold , so further increasing it is unnecessary. On the other hand, if  t < 0, then the sampled action leads to a reward- to-go that is worse than the baseline, so the update would like to decrease the probability of that action. When rt(θ) ≥ 1 − clip, PPO again uses the ordinary contribution; when rt(θ) < 1 − clip, the clipped term caps the improvement from making that bad action even less likely, which again avoids potentially unnecessary overshooting in that direction because the probability of the action is already lower than the probability under the old policy. We minimize this surrogate over θ. The clipping term prevents the objective from continuing to reward changes that have already made an action much more or much less likely than it was under πθold. Finally, in many practical settings the advantage estimates are improved with generalized advantage estimation (GAE) [Schulman et al., 2016]. In the RLVR setting discussed in Section 18.2, the advantage estimate is often sim- plified further, for example by using group-relative or mean-reward baselines rather than a learned value function.