Lecture 6
1. A Better Reward-To-Go?
Recall that our policy gradients are given by:
The sum \(\sum_{t'=t}^T r(\mathbf{s}_{t'}^{(i)}, \mathbf{a}_{t'}^{(i)})\) is known as the reward-to-go. This is simply the sum of rewards from the current time step to the end. Note that this is using only the current trajectory to estimate the reward obtained by taking action \(\mathbf{a}_t\) in state \(\mathbf{s}_t\) and then following the policy \(\pi\). However, as we may get different trajectories (because of the stochasticity of the system) each time we take the same action \(\mathbf{a}_t\) in state \(\mathbf{s}_t\), what we really need is the expected reward. Now, while an estimate of the expectation of a distribution using a single sample is always unbiased (in expectation we get the right answer), it has high variance1.
Recall that we had earlier defined the Q-function as:
This is the true expected reward obtained from taking action \(\mathbf{a}_t\) in state \(\mathbf{s}_t\). Therefore, we modify our policy gradients as:
We have also subtracted a baseline. Recall that the value function \(V^\pi(\mathbf{s}_t)\) is just:
i.e. it is just the expected value of the Q-function under the actions. Subtracting it has the intuition of assigning better-than-average actions a positive reward-to-go and worse-than-average ones a negative reward-to-go. Note that subtracting a state-dependent baseline is unbiased (as we showed in Homework 2).
We refer \(Q^\pi(\mathbf{s}_t, \mathbf{a}_t)-V^\pi(\mathbf{s}_t)\) to as the advantage and denote it with \(A^\pi(\mathbf{s}_t,\mathbf{a}_t)\). So the better the estimate \(A^\pi(\mathbf{s}_t,\mathbf{a}_t)\) is, the lower the variance.
2. Value Function Fitting
Note that:
where the fifth line followed from Markov’s assumption.
We can estimate the expectation using a single-sample only:
Therefore:
Hence, we only need to fit the value function to a function approximator in order to estimate the advantages.
In order to train a function approximator (say, a neural network) we need a training set:
One way to estimate \(V^\pi(\mathbf{s}_t)\) is through Monte Carlo evaluation (as in policy gradients):
However, this requires us to reset our simulator multiple times to state \(\mathbf{s}_t\) which may be expensive or not possible. In practice, estimating \(V^\pi\) with a single-sample works well:
Our training set is thus given by:
For supervised regression we would minimize:
where \(\hat{V}^{\pi}_{\phi}\) is the function approximator with parameters \(\phi\).
While this works fine in practice, we can do something even better:
This estimate is often called as the bootstrapped estimate. So we can modify our training set to be:
i.e. we use our previous estimate of the value function to generate new labels. Note that as our estimate of the value function would be (hopefully) closer to the true value function than a single-sample estimate, this will reduce variance (though at the expense of introducing some bias, because the estimate after all is incorrect). We can now use this training set for supervised regression.
3. The Actor Critic Algorithm
The batch actor-critic algorithm repeats the following until convergence:
- Sample \(\{\mathbf{s}^{(i)},\mathbf{a}^{(i)}\}\) from \(\pi(\mathbf{a}\vert\mathbf{s})\)
- Fit \(\hat V^{\pi}_{\phi}\) to the sampled rewards
- Evaluate \(A^\pi (\mathbf{s}_t,\mathbf{a}_t) \approx r(\mathbf{s}_t,\mathbf{a}_t) + \hat V^{\pi}_{\phi}(\mathbf{s}_{t+1}) - \hat V^{\pi}_{\phi}(\mathbf{s}_t)\)
- Compute \(\nabla_\theta J(\theta) \approx \frac{1}{N}\sum_{i=1}^N\sum_{t=1}^T \nabla_\theta \log \pi_\theta(\mathbf{a}_t^{(i)}\vert\mathbf{s}_t^{(i)})A^\pi (\mathbf{s}_t^{(i)},\mathbf{a}_t^{(i)})\)
- Update \(\theta := \theta + \alpha\nabla_\theta J(\theta)\)
Step 2 boils down to two steps:
- Compute \(y^{(i)}_t=r(\mathbf{s}_t^{(i)},\mathbf{a}_t^{(i)})+\hat V^\pi_\phi(\mathbf{s}_{t+1}^{(i)})\) for all \(i\)
- Minimize \(\mathcal{L}(\phi) = \sum_i\vert\vert \hat{V}^{\pi}_{\phi}(\mathbf{s}^{(i)})-y^{(i)})\vert \vert^2\)
4. Discount Factors
Suppose we have an infinite horizon. If, for example, the rewards are all positive, then just adding them together will result in an infinitely large number. Discount factors are used to remedy this. We modify our labels \(y^{(i)}\) to be:
where \(\gamma\in [0,1]\) is the discount factor. To compute the advantages we use:
For policy gradients we have two options. We can either use:
or use:
which after noting causality gives:
The second option has the intuition (because of the term \(\gamma^{t-1}\)) that later steps do not matter as much as earlier steps. However, the first option is often used in practice. Discounts may also serve as a variance-reduction technique as they essentially reduce the weightage of states further in the future which are usually more uncertain than the ones closer.
Discounts result in the online version of the actor-critic algorithm:
- Take action \(\mathbf{a}\sim \pi(\mathbf{a}\vert\mathbf{s})\) and get \((\mathbf{s},\mathbf{a},\mathbf{s'},r)\)
- Update \(\hat V^{\pi}_{\phi}\) using target \(r+\hat V^{\pi}_{\phi}(\mathbf{s'})\)
- Evaluate \(A^\pi (\mathbf{s},\mathbf{a}) \approx r(\mathbf{s},\mathbf{a}) + \gamma\hat V^{\pi}_{\phi}(\mathbf{s'}) - \hat V^{\pi}_{\phi}(\mathbf{s'})\)
- Compute \(\nabla_\theta J(\theta) \approx \nabla_\theta \log \pi_\theta(\mathbf{a}\vert\mathbf{s})A^\pi (\mathbf{s},\mathbf{a})\)
- Update \(\theta := \theta + \alpha\nabla_\theta J(\theta)\)
The online actor-critic algorithm, however, works best in batches. This can be done in two ways: synchronized and asynchronous parallel actor critic mechanisms. We’ll discuss them in a later lecture note.
5. Architecture Design
For the Actor-Critic algorithm, we need to fit two neural networks (to the value function and the policy). We can either opt for a two-network design where the two networks do not share weights. Such a architecture is simple and stable but has the disadvantage that the actor and the critic cannot share features with each other. However, a shared network design is much harder to optimize.
6. Critics as State-Dependent Baseline
Note that the policy gradients were unbiased but had high variance. In contrast, the actor-critic is not unbiased but has lower variance. The lower variance is primarily because of the critic. Suppose that we only use the critic as a state-dependent baseline:
Recall that using a state-dependent baseline is unbiased. So in this way, not only is our gradient unbiased but it also has lower variance.
7. Control Variates: Action-Dependent Baselines
We may use baselines that are also dependent on actions (such as the Q-function):
Note that if the critic i.e. \(\hat Q^\pi_\phi\) is exactly equal to the true Q-function, then the advantage will become zero in expectation (as the Q-function is equal to the expected sum of rewards). However, in practice this is not possible and the advantage will be a small non-zero albeit number. However, because smaller numbers have smaller variances, this estimate of the advantage will result in a lower variance for the policy gradients. Unfortunately, in general:
which means that the policy gradients are no longer unbiased. One way to make them unbiased again is to add back this expectation:
If we choose our function approximator carefully (such as quadratic in the actions under a Gaussian policy), then the second term above can have a closed form solution. So, in this way, we have been to reduce the variance of the first term by using an action-dependent baseline and have corrected for the bias as a result of doing so through the second term.
8. Eligibility Traces & N-Step Returns
For the infinite horizon case, the actor-critic computes advantages using:
while the Monte-Carlo (policy gradients) uses:
\(A_C^\pi\) has a lower variance but is biased while \(A^\pi_{MC}\) has a higher variance but is unbiased. One way of controlling this bias-variance tradeoff is to use:
Here we’re using summing up the rewards for the first \(n\) steps and then using our function approximator for the remaining steps. The intuition behind this is that time steps further in the future are more uncertain (i.e. have higher variance) than the ones closer. So for those time steps, we can use a lower-variance, though slightly biased, estimator (the \(\hat V^\pi_\phi\)). For time steps in the near future, variance is not that big of an issue. So it is better to use an unbiased estimator (the sum of rewards) for them. In practice, choosing \(n>1\) works better.
9. Generalized Advantage Estimation
We may use more than one value of \(n\) in the \(n\)-step returns above and then weight the advantages:
The weights can be chosen such that:
where \(\gamma \in [0,1]\), i.e. \(n\)-step returns with smaller values of \(n\) are preferred. It can easily be shown2 that:
where:
-
Consider a random variable \(y\) such that \(\mathbb{E}[y]=\mu\). Suppose that we estimate \(\mu\) using \(N\) samples: \(\bar{\mu} = \frac{1}{N}\sum_{i=1}^N y^{(i)}\). Then the expected value of \(\bar{\mu}\) is: \(\mathbb{E}[\bar\mu] =\) \(\mathbb{E}\left[\frac{1}{N}\sum_{i=1}^N y^{(i)}\right]=\) \(\frac{1}{N}\sum_{i=1}^N \mathbb{E}\left[y^{(i)}\right]\) \(=\frac{1}{N}\sum_{i=1}^N \mu=\mu\). Hence, estimating the expectation of \(y\) with any number of samples gives the right answer in expectation and so is unbiased. However, the individual samples \(y^{(i)}\) might be far away from \(\mu\). (and each other), which leads to high variance. Of course, as \(N\) increases the estimate \(\bar{\mu}\) becomes better and better. One intuition behind this is that the samples to the left and right of \(\mu\) start to cancel out each other’s effect, bringing the result closer to \(\mu\). ↩
-
See the original paper for this derivation: Schulman, Moritz, Levine, Jordan, Abbeel (2016). High-dimensional continuous control using generalized advantage estimation: batch-mode actor-critic with blended Monte Carlo and function approximator returns. ↩