Policy gradient theorem

Let’s assume an stochastic environment from which to sample states and rewards. Consider a stochastic control policy 1 parameterized by a parameter vector , that is, a distribution over the action set conditioned on a state . is a D-dimensional real valued vector, , where is the number of parameters (dimensions) and . The agent acting under policy is to maximize the (possibly discounted)2 sum of rewards obtained inside environment , over a time horizon (possibly infinitely long). Recall that in this introductory blog to Reinforcement Learning we mathematically boiled down Reinforcement Learning as a quest to find a divine policy, rightful maximizer of the expected reward obtained in an environment:

When we explicitly parameterize a policy by a parameter vector we can rewrite the equation above as:

There are strong motivations for describing the optimization problem on the parameter space of a policy instead of the already discussed approaches:

  1. It offers a more direct way of approaching the reinforcement learning problem. Instead of computing the value functions or and from those deriving a policy function, we are calculating the policy function directly.

  2. Using stochastic policies smoothes the optimization problem. With a deterministic policy, changing which action to execute in a given state can have a dramatic effect on potential future rewards3. If we assume a stochastic policy, shifting a distribution over actions slightly will only slightly modify the potential future rewards. Furthermore, Many problems, such as partially observable environments or adversarial settings have stochastic optimal policies (Degris 2012), (Lanctot 2017).

  3. Often can be simpler than or .

  4. If we learn in a large or continuous actions space, it can be tricky to compute .

For an episode of length let be the trajectory followed by an agent in an episode. This trajectory is a sequence of state-action tuples . We overload the notation of the reward function thus: , indicating the total reward obtained by following trajectory . From here, the utility of a policy parameterized by is defined as:

Where denotes the probability of trajectory happening when taking actions sampled from a parameterized policy . More informally, how likely is this sequence of state-action pairs to happen as a result of an agent following a policy . Linking equations  and , the optimization problem becomes:

Policy gradient methods attempt to solve this maximization problem by iteratively updating the policy parameter vector in a direction of improvement w.r.t to the policy utility . This direction of improvement is dictated by the gradient of the utility . The update is usually done via the well known gradient descent algorithm. This idea of iteratively improving on a parameterized policy was introduced by (Williams 1992) under the name of policy gradient theorem. In essence, the gradient of the utility function aims to increase the probability of sampling trajectories with higher reward, and reduce the probability of sampling trajectories with lower rewards.

Equation  presents the gradient of the policy utility function. The next section shows the derivation from equation  to equation .

Policy gradient theorem derivation

Let’s jam

Having defined the utility of a function given a parameter vector . We could solve equation $\ref{equation:utility}$ by following the Utility’s gradient wr.t to $\theta$. Hence, the goal is to find the expression that will allow us to update our policy parameter vector in a direction that improves the estimated value of the utility of the policy . Taking the gradient w.r.t gives:

This derived equation is very similar, but not quite like, equation $\ref{equation:utility-gradient}$:

This leaves us with an expectation for the term . Note that as of now we have not discussed how to calculate , the probability of a trajectory under a policy . Let’s define just that:

Because we assume that we are working in a Markovian environment; The probability of a trajectory happening is nothing more than the concatenated multiplication of (1) the probability of each action that was taken (2) The transition probability from each state in the trajectory and its empirical successor.

From here we can calculate the term present in equation $\ref{equation:expectance-gradient-partial-derivation}$:

Plugging this last derived result of into equation $\ref{equation:expectance-gradient-partial-derivation}$ we obtain the following equation for the gradient of the utility function w.r.t to parameter vector :

(Sutton 1999) offers a different approach to this derivation by calculating the gradient for the state value function on an initial state , calculating .

A key advantage of the policy gradient theorem, as inspected by (Sutton 1999) is that equation  does not contain any term of the form . This means that we don’t need to model the effect of policy changes on the transition probability function . Policy gradient methods therefore classify as model-free methods.

How to actually compute this gradient

It’s all well and good when you derive this formula for the first time. Everybody pats each other’s backs and leave the office with a satisfied face. But somebody stays behind and asks the real question: How do you actually compute the gradient in equation $\ref{equation:utility-gradient}$?

We can use Monte Carlo methods to generate an empirical estimation of the expectation in equation . This is done by sampling trajectories under the policy . This works even if the reward function is unkown and/or discontinuous, and on both discrete and continuous state spaces. The equation for the empirical approximation of the utility gradient is the following:

The estimate is an unbiased estimate and it works in theory. However it requries an impractical amount of samples, otherwise the approximation is very noisy (has high variance). There are various techniques that can be introduced to reduce variance.

It’s all about that Baseline

Intuitively, we want to reduce the probability of sampling trajectories that are worse than average, and increase the probability of trajectories that are better than average. (Williams 1992), in the same paper that introduces the policy gradient theorem, explores the idea of introducing a baseline as a method of variance reduction. These authors also prove that introducing a baseline keeps the estimate unbiased. It is imporant to note that this estimate is not biased as long as the baseline at time does not depend on action . Introducing a baseline in equation yields the equation:

The most basic type of baseline is the global average reward, which keeps track of the average reward across all episodes. We can also add time dependency to the baseline, such as keeping a running average reward. (Greensmith 2004) derives the optimal constant value baseline.

Adding a baseline does not bias the gradient estimate

Take a equation $\ref{equation:utility-gradient-baseline}$, representing the gradient of the utility of a parameterized policy $\pi_{\theta}$ using a constant and real valued baseline $b \in \mathbb{R}$:

In order to prove that substracting the baseline $b$ leaves the estimation unbiased, we need to show that the right hand side of the substraction evaluates to $0$. Which is indeed the case:

Thus, adding a baseline leaves the gradient of the policy utility unbiased!

Improvements over constat Baselines

Furthermore, it is not optimal to scale the probability of taking an action by the whole sum of rewards. A better alternative is, for a given episode, to weigh an action by the reward obatined from time onwards, otherwise we would be ignoring the Markov property underlying the environment’s Markov Decission Process by adding history dependency. Removing the terms which don’t depend on the current action reduces variance without introducing bias. This changes equation  to:

A powerful idea is to make the baseline state-dependent  (Baxter2001). For each state , this baseline should indicate what is the expected reward we will obtain by following policy starting on state . By comparing the empirically obtained reward with the estimated reward given by the baseline , we will know if we have obtained more or less reward than expected. Note how this baseline is the exact definition of the state value function , as shown in equation . This type of baseline allows us to increase the log probability of taking an action proportionally to how much its returns are better than the expected return under the current policy.

The term can be regarded as an estimate of for a single roll out. This term has high variance because it is sample based, where the amount of variance depends on the stochasticity of the environment. A way to reduce variance is to include a discount factor , rendering the equation: . However, this introduces a slight bias. Even with this addition, the estimation remains sample based, which means that it is not generalizable to unseen state-action pairs. This issue can be solved by using function approximators to approximate the function . We can define another real valued parameter vector , where is the dimensionality of the parameter vector. From here, we can use to parameterize the function approximator . This function will be able to generalize for unseen state-action pairs.

Deciding on how many steps into the future to use for the state-value function entails a variance-bias tradeoff. The more actual sampled rewards used in our state-value function estimation, the more variance is introduced, whilst reducing the variance from the function approximator.

Notice how we use the parameter vector to approximate the state value function . This approach can be viewed as an actor-critic architecture where the policy is the actor and the baseline is the critic.

Advantage function and Generalized Advantage Function (GAE)

Nice post about TD error vs Advantage vs Bellman error

Finally, let’s introduce the The advantage function. The advantage function is defined as and it denotes how much better or worse the result of taking a specific action at a specific state is, compared to the policy’s default behaviour. This is captured by the expression:

Using the advantage function inside of the policy gradient estimation yields almost the lowest variance, although this equation is not known in practice, and must be estimated. This can be done, as mentioned before, by approximating the function .

 (Schulman 2015) introduces a very smart idea, which generalizes the -step lookahead of equation $\ref{equation:q-n-step-lookahead}$. Instead of deciding on a single value for the number of lookahead steps, it is possible to take into account all of them simultaneously. Let’s define as the -step lookahead advantage function. (Schulman 2015) introduces the generalized advantage estimation (GAE), parameterized by the discount factor and a special parameter , which is used to manually tune yet another variance-bias tradeoff.

By choosing low values for $\lambda$, we are biasing the estimation of the advantage function towards low values of for all -step lookahead cases, reducing variance and increasing bias. If we use a higher value for , we increase the weight of the higher values of the -step lookahead cases. The GAE can be analytically written as:

Where is the TD residual for a given policy as introduced in (Sutton 1999). There are two notable cases of this formula, obtained by setting and :

The first one is the TD error. The second one is the sample based estimation of $Q_{\pi_{\theta}}(s_t, a_t)$

Policy gradient equation summary

In summary, policy gradient methods maximize the expected total reward by repeatedly estimating the policy’s utility gradient . There are many expresessions for the policy gradient that have the form:

The variable can be one of the following:

  1. : total trajectory reward.

  2. : reward following action .

  3. : baseline version.

  4. : state-action value function.

  5. : Advantage function.

  6. : TD residual.

  7. : GAE

Out of the 7 different possibilities, state of the art algorithms use GAE(,), as it has been proved empirically and loosely theoretically that it introduces an “acceptable” amount of bias, whilst being one of the methods that most reduces variance.

  1. Some researchers prefer the notation , or . These notations are equivalent. 

  2. (Williams 1992), (Sutton 1999) present proofs of this same derivation using a discount factor, which makes policy gradient methods work for environments with infinite time horizons. 

  3. An example of this concept are greedy or -greedy policies derived thus:

Related article

A Historical Introduction: Self-Play in Reinforcement Learning

Introduction ------------ In the classical single agent reinforcement learning (RL) scenarios described by [(Sutton 1999)](https://papers.nips.cc/paper/1713-policy-gradient-methods-for-reinforcement-learning-with-function-approximation.pdf), [where...

Read more

Environment models (Beyond Markov Decision Processes)

Even though Markov Decision Processes are the most famous mathematical structure used to model an...

Read more

Classification of RL algorithms

Every RL algorithm attempts to learn an optimal policy for a given environment . So...

Read more