Recall Q^i,t from the last lecture. Q^i,t is the estimate of the expected reward if you take action ai,t in state si,t. We can improve this estimate by replacing it with its estimate:
Q^i,t≈t′=t∑TEπθ[r(st′,at′)∣st,at]
If we plug this back into the reward from last lecture, we can still use the baseline function (this time adapted to be the average Q^i,t value bt=N1∑iQ(si,t,ai,t)) to perform better than average. The baseline depending on action leads to bias, but we can improve even further by making it depend on the state. This leads to the following function for the baseline:
V(st)=Eat∼πθ(at∣st)Q(st,at)
This is the average reward over all the possibilities that start in a given state--the value function! Plugging it back in we get the equation below, where Q−V makes sense because it represents your estimate of how much better ai,t is than the average action in si,t.
Q−V is so important that it is called the Advantage function, A. Q, V, and A are often written with the superscript π to denote that they rely on the policy π. To recap the three:
Qπ(st,at): Total reward from taking at in st
Vπ(st): Total reward from st
Aπ(st,at)=Qπ(st,at)−Vπ(st): How much better at is
This provides us with a large reduction in variance with the cost of a small increase in bias.
Policy Evaluation
Which of Qπ, Vπ, and Aπ should we fit and what should we fit it to? We'll choose Vπ since it's only dependent on st and the other two functions can be approximated by it as follows:
Fitting Vπ(st) is called policy evaluation. J(θ) can be expressed as
J(θ)=Es1∼p(s1)[Vπ(s1)]
How can we perform policy evaluation? We can use a NN function approximator (ϕ) for Vπ which uses the supervised regression algorithm L(ϕ)=21∑iV^ϕπ(si)−yi2 on the training data {(si,t,yi,t)}. How can we find yi,t?
Monte Carlo Method
The Monte Carlo target estimates it using a single sample: yi,t=∑t′=tTr(si,t′,ai,t′)
Note that we're directly using the previous fitted value function V^ϕπ(si,t+1), which reduces variance but slightly lowers accuracy. This is called the bootstrapped estimate.
The Algorithm
Batch Algorithm
A basic batch actor-critic algorithm follows the steps below:
Sample {si,t,ai,t} from πθ(ai,t∣si,t) (run the simulation)
If the episode length T is infinite, the value function V^ϕπ can itself approach infinity. We can avoid this by making the agent aware of its own mortality, and having it prioritize getting rewards sooner rather than later. This is represented mathematically by a discount factor γ∈[0,1] (usually use 0.99).
yi,t≈r(si,t,ai,t)+γV^ϕπ(si,t+1)
When it comes to Monte Carlo policy gradients there are two options for integrating discount factors.
We can take the single-sample reward-to-go calculation and add the discount factor to it:
Generally we use option 1, which doesn't focus quite so much on short-term rewards.
Online Algorithm
We can also create an algorithm which updates our policy after each simulator/real-world time step, not just in batches as is the case with the standard batch algorithm:
Take action a∼πθ(a∣s) to get (s,a,s′,r)
Update V^ϕπ using the target r+γV^ϕπ(s′)
Evaluate A^π(s,a)=r(s,a)+V^ϕπ(s′)−V^ϕπ(s)
Find ∇θJ(θ)≈∇θlogπθ(a∣s)A^π(s,a)
Do gradient descent θ←θ+α∇θJ(θ)
The Architecture
A good starting point for architecture is to use two NNs: one which outputs a scalar s→V^ϕπ(s), and the second outputs either the parameters of a continuous distribution, or the softmax of discrete actions s→πθ(a∣s) as the case may be. This is simple and stable, though it doesn't share features between the two networks. The other option is to use one network with a shared backbone which can output both values.
There's also the question of whether to use synchronous or asynchronous AC, when there are multiple actors.
Critics as Baselines
Actor-critic algorithms have a lower variance but are biased, whereas policy gradients have no bias, but higher variance. Critics can be used as either action- or state-dependent baselines. A method that relies on both can be referred to as a control variate.
If used as a state-dependent baseline, as shown below, we can achieve no bias with a variance very close to that of the reward.
If we use a control variate we get the equation below where the first term is just the policy gradient with the baseline, the second term is the gradient of the expected value under the policy of the baseline.
This can achieve a very low variance, with the caveat that you must be able to evaluate the second term.
Generalized Advantage Estimation
Lets compare the advantage estimator in an Actor-critic algorithm to that in a Monte Carlo algorithm. The latter has low variance, but a high bias if the value is wrong, while the former has no bias and a high variance.
The larger n is, the lower the bias, the smaller n is, the higher the variance. The first term contributes variance while the second term contributes bias. We can create a weighted average of the results of the n-step return estimators which we call Generalized Advantage Estimators (GAE) which uses the formula below where wn∝λn−1: