Video link
The REINFORCE Algorithm
Given the finite-horizon case of the RL objective:
θ∗=argmaxθt=1∑TE(st,at)∼pθ(st,at)[r(st,at)]We can rewrite the expectation as:
J(θ)=E(st,at)∼pθ(st,at)[r(st,at)]≈N1i∑t∑r(si,t,ait)This can be approximated by making rollouts of our policy, running the policy N times to collect N-sampled trajectories resulting in the approximation above. A bigger N yields a more accurate estimation of the expected value. Now, we don't just want to estimate the objective, we actually want to improve it, by estimating it's derivative.
Note: ∑t=1Tr(si,t,ait) will be simplified as r(τ) from here on out.
{: .notice--info}
If continuous, we can expand the expectation out to the integral (sum, if discrete) of the products of the probability and the value:
J(θ)=Eτ∼pθ(τ)[r(τ)]=∫pθ(τ)r(τ)dτThe gradient of which is:
∇θJ(θ)=∫∇θpθ(τ)r(τ)dτNote: The following identity is useful at several points in this lecture x∇θlogx=xx∇θx.
{: .notice--info}
The identity above can be used to simplify the above equation to:
∇θJ(θ)≈N1i=1∑N[(i=1∑T∇θlogπθ(at∣st))(i=1∑Tr(at,st))]Recall the maximum likelihood equation used in supervised learning:
∇θJML(θ)≈N1i=1∑N(t=1∑T∇θlogπθ(ai,t∣si,t))Our equation for the gradient is a reward-weighted maximum likelihood objective!
Thus, we come to the REINFORCE algorithm:
- Sample {τi} from πθ(at∣st) (run the policy).
- Use the simplified equation above to approximate ∇θJ(θ).
- Take a step of gradient descent θ←θ+α∇θJ(θ).
This algorithm does not require the initial state distribution or the transition probabilities.
{: .notice--warning}
This algorithm does not actually use the Markov property, so it can be used in partially observed MDPs.
{: .notice--warning}
Reducing Variance
The main problem with policy gradient methods is high variance. In the image below where rewards are denoted with green lines we would expect to see the reward function move from the blue line to the blue dotted line.

If we added an offset to the rewards we would expect the behavior below:

We can use the concept of causality to reduce the variance of the policy gradient. Causality is the idea that the policy at time t′ cannot affect the reward at time t when t<t′. This allows us to simplify the equation for ∇θJ(θ) to:
∇θJ(θ)≈N1i=1∑Nt=1∑T∇θlogπθ(ai,t∣si,t)Q^i,tWhere Q^i,t is called the "reward to go":
Q^i,t=t′=t∑T∇θlogπθ(ai,t∣si,t)Baselines
We want to center rewards around 0, and to do so we'll subtract a quantity b from them:
∇θJ(θ)≈N1i=1∑N∇θlogpθ(τ)[r(τ)−b]Where b is the average reward:
b=N1i=1∑Nr(τ)Subtracting this baseline does not bias the expectation. The average reward, though good, is not actually the best baseline. The best baseline is the expected reward, weighted by magnitude values:
b∗=E[(∇θlogpθ(τ))2]E[(∇θlogpθ(τ))2r(τ)]On/Off Policy
Policy gradients are considered to be on policy (the policy cannot be improved without having to regenerate samples). This is problematic because NNs are nonlinear and thus require taking many small steps, at each of which the samples have to be regenerated by re-running the policy. This is why policy gradient algorithms are generally good when generating samples is cheap.
Importance Sampling
We're calculating the expectation below:
J(θ)=Eτ∼pθ(τ)[r(τ)]But what if instead of sampling from pθ(τ) we had samples from another distribution, pˉθ(τ)?
Importance sampling allows us to calculate an expectation under one distribution using samples from another:
Ex∼p(x)[f(x)]=∫p(x)f(x)dx=∫q(x)q(x)p(x)f(x)dx=∫q(x)q(x)p(x)f(x)dx=Ex∼q(x)[q(x)p(x)f(x)]We can then use that log identity from HW1 to estimate the value of new parameters θ′:
J(θ′)=Eτ∼pθ(τ)[pθ(τ)pθ′(τ)r(τ)]The gradient of which is:
∇θ′J(θ′)=Eτ∼pθ(τ)[(t=1∏Tπθ(at∣st)πθ′(at∣st))(t=1∑T∇θ′logπθ′(at∣st))(t=1∑Tr(at,st))]Where the terms in parentheses are:
- Importance weights over all timesteps (the ratio of the products of the policy probabilities).
- Sum over all timesteps of the gradient.
- Sum over all timesteps of the reward.
The importance weights are problematic because they're all <1 and thus are exponential in T, making the variance exponential as well. This kills the gradient. However, we can rewrite J(θ′) as the expectation under the state action marginals to get a reasonable approximation:
J(θ)~≈t=1∑TEst∼pθ(st)[Eat∼πθ(at∣st)(πθ(at∣st)πθ(at∣st)r(st,at))]Tips:
- Using large batches can reduce variance
- Tweaking learning rates is hard
- Using adaptive step sizes (e.g. with ADAM) can work
Part 6 of this lecture, Advanced Policy Gradients is not covered here.
{: .notice--danger}