REINFORCE Algorithm Primer
REINFORCE is the basic policy gradient algorithm. Policy gradient methods try to increase the probabilities of actions that lead to higher return and decrease the probabilities of actions that lead to lower return.
REINFORCE is (natively):
- an online reinforcement learning algorithm as it uses new data generated by the learned policy (the policy interacts with the environment while learning)
- on-policy as the trajectories used for the update are sampled from the current policy
- model-free as it learns directly from interacting with the environment without building an internal transition or reward model of the environment
1. Problem Setting
We aim to learn a policy
A trajectory is a sequence of states/observations and actions:
We have a reward function
The objective is to maximise expected total return:
The probability of a trajectory under the current policy is
Here,
2. Monte Carlo Approximation of the Objective
The expectation over all possible trajectories is generally impossible to compute exactly as an integral. Instead, we sample
and approximate the expectation with the sample mean:
We will switch back to integrals for now and evaluate
3. Deriving the Policy Gradient
How do we change
For simplicity, define the trajectory return as
Then our objective can be written as
Writing the expectation as an integral gives
To take the gradient with respect to our parameters
As it is,
Substituting this into the gradient gives
Because the integral now contains
We now need to evaluate
Recall that
Taking the logarithm turns the products into sums:
Taking the gradient with respect to
Therefore,
This is the basic REINFORCE gradient.
4. Monte Carlo Approximation of the Gradient
We cannot compute this expectation over all possible trajectories, so we approximate it using
Intuition
REINFORCE is behavior cloning with a weight on the log-probability gradient in the form of the return.
The gradient in the behavior cloning (supervised learning) setup is simply
REINFORCE adds the weight
which increases the likelihood of actions you took in trajectories that returned high reward and decreases the likelihood of actions you took in trajectories that returned low reward.
5. Improving REINFORCE
Without introducing a new algorithm, REINFORCE can be improved with two simple modifications.
5.1 Reward-to-Go
If you look to our REINFORCE gradient update, we are weighing the likelihood of actions taken by the return of the entire trajecotry (reward of the trajectory from the first state to the final state in the trajectory). Intuitively, this dilutes and weakens our learning signal (particularly for later actions in the episode). Why should the likelihood of an action be weighted by rewards that preceded that action being taken in our gradient?
Therefore, one simple modification of our REINFORCE gradient is to weigh the likelihood of an action by the rewards that follow that action being taken. This changes our gradient update to:
5.2 Baseline
Another simple trick to reduce the variance of the policy-gradients is to introduce a baseline for our return.
Given that we have re-defined our weight to be based on the rewards that follow an action being taken:
Then our policy gradient can become:
I need to confirm this. Most REINFORCE discussions define the baseline by averaging total returns across a batch of trajectories. Here, we are trying to factor in our reward-to-formulation and define the baseline across a batch as well as across the timesteps of those trajectories. Either way, this is not very important as this baseline will simply become the value function when we move to actor-critic architectures.
6. Algorithm and Implementation
for each training iteration:
1. Collect trajectories
Sample
2. Compute reward-to-go
For every timestep
3. Compute the baseline
Using a simple batch-level baseline,
4. Form the policy-gradient estimate
5. Update the policy
end for
6.1 Implementation Detail
One limitation of the algorithm we presented above is the need to conduct N*T backward passes as form our policy-gradient estiamte as a summation over all episodes in our buffer as well as all timesteps of those episodes. This is computationally inefficient.
Ideally, we want to be able to conduct one backward pass by using automatic differentiation on the full objective. By using the fact that differentiation is linear, we can define a scalar surrogate loss whose gradient is equal to the REINFORCE policy gradient we presented above.
Starting from
define the surrogate objective
where the sampled returns and baseline are treated as constants with respect to the policy parameters:
Then, by linearity of differentiation,
Thus, all timestep-level policy-gradient contributions can be accumulated into one scalar loss and computed with a single backward pass.
Additionally, the form of
7. Can we make this off-policy?
One issue that arises with policy gradients is the sampling process. Step 1 of our algorithm collects trajectories from
This arises because policy gradients is an on-policy algorithm, which learns from data generated by the same policy that is currently being optimized. Wheras off-policy algorithms can learn from data generated by a different or older policy. We can exploit this “older policy” feature of off-policy algorithms to improve policy gradients in order to “use more” of our batch of data (learn more from single batch of data without having to immediately resample new trajectories after each paramter update).
7.1 Off-Policy Version of Policy Gradient
The core motivation of this off-policy variant is being able to use samples from a previous policy,
We now distinguish between our past (often called, behaviour) policy
So far, our on-policy’s objective was based on what would happen under the proposed policy (which made sense when our trajectories were sampled from that same policy). This no longer applies here, which means we need to change our objective.
Importance Sampling
The generic importance-sampling identity is
In our policy-gradient setting, the equivalent distributions are
Therefore,
This means we can use trajectories generated by the old policy
We can simplify the importance ratio using the trajectory probabilities:
The initial-state distribution and environment dynamics are identical under both policies, so they cancel:
Therefore,
Off-Policy Policy Gradient
Recall our REINFORCE gradient with reward-to-go and a baseline:
If our trajectories instead come from the past policy
A simple trajectory-wise importance-sampling version is
Cumulative Importance Ratio:
However, for the gradient contribution at timestep
We can therefore define the cumulative importance ratio
This gives the per-decision importance-sampling policy gradient
The cumulative ratio is important because the difference between the old and new policies affects more than just the probability of taking the current action
However, there is an important practical limitation. Products of importance ratios can have extremely high variance, particularly at longer time horizons (
Approximating the State-Visitation Correction
Instead of correcting the probability of the whole trajectory prefix, we can look directly at the distribution over the state-action pair
Under the current policy,
while under the behaviour policy,
Therefore,
The first term, which we call the *state-visition ratio, corrects for how likely each policy is to reach
To connect this to what we did with cumulative importance sampling, the state-visition ratio at
The difficulty is that the state-visitation ratio
is hard to compute directly. A common approximation is to assume the old and new policies are close enough that
This leaves only the single-step action ratio and gives
This is less exact than cumulative importance sampling, but has much lower variance because it avoids multiplying importance ratios across the trajectory history.