I thought I would write about some theory behind Reinforcement Learning, with eligibility traces, contraction mapping, POMDP and so on, but then I realized if I go down that rabbit hole, I would probably never finish this. So here are some practical stuff that people are actually using these days.

Two main approaches in model-free RL are *policy-based*, where we build a model of the policy function, and *value-based*, where we learn the value function. For many practical problems, the structure of the (unknown) policy function might be easier to learn than the (unknown) value function, in which case it makes more sense to use policy-based methods. Moreover, with policy-based methods, we will have an explicit policy, which is the ultimate goal of Reinforcement learning *to control* (the other type being *learn-to-predict*, more on that some other time). With *value-based *methods, like DQN, we will need to do an additional inference step to get the optimal policy.

The hybrid of *policy-based* and *value-based* is called *Actor-Critic* methods, which hopefully will be mentioned in another post.

One of the most straightforward approach in *policy-based* RL is, unsurprisingly, *evolutionary algorithms.* In this approach, a population of policies is maintained and evolutionized over time. People show that this works pretty well, e.g. for the Tetris game. However, due to the randomness, this is apparently only applied to problems where the number of parameters of the policy function is small.

A big part of policy-based methods, however, is based on *Policy Gradient*, where an exact estimate of the gradient of the expected future reward can be computed. Roughly speaking, there is an exact formulation for the *gradient* of the *policy*, which we can then use to optimize the policy. Since Deep Learning people basically *worship* gradients (pun intended), this method suites very well and became trendy about 2 years ago.

So, what is it all about? It is based on a very simple trick called *Likelihood Ratio.*

Let’s say we have a sequence of states and actions (called an *episode* or *rollout*). Let:

- be the immediate reward at timestep
*t*after the agent be in state and performs action . - be the total reward of the whole trajectory
- be the transition model (for presentation purpose only, we won’t need to know this quantity at the end of the day, because we are in
*model-free*RL). - be the
*policy*function, which is basically a distribution of actions given state. We will build a model with parameters and learn it from the observed sequences ‘s. - be the expected future reward.

Using the definition of the expectation, we have , where is the probability of trajectory under the policy .

Now, the whole purpose of learning is to estimate the parameters such that the expected reward, in other words:

Just like any good mathematician, we would just compute the derivative of :

This might look too trivial, but this simple trick makes the right hand side becomes the expectation of the function over all trajectories. Using the law of large number, we can approximate it with:

During training, what we do is we take the episodes one at a time, compute the above estimation of the gradient (i.e. using ), and update using this gradient. Since we will be doing stochastic gradient descent anyway, the above estimation still works even with .

However, this estimation is extremely noisy (say *high-variance*), and *data-inefficient*. It is high variance mainly because of the sum of total rewards can varies depending on the immediate rewards the agent gets during the whole episode. It is *data-inefficient* because we can only update the parameters *once per episode*, therefore we need many episodes for this method to work.

We will see later how to address those issues. For now, let’s finish computing the only missing piece on the right hand side:

We could drop the first part of the sum because that is the transition model, and does not depends on . Note that the result is pretty beautiful. Basically it is the sum of the derivatives of *log-probability* of the policy function over all timesteps in the episode. Plug this into the above function, we will have an estimate of the gradient to be used to update the paramters .

In practice, people would use a deep neural net to model the policy function . For continuous control problems, the output layer would be continuous. For discrete control problems, the output layer could be Gaussian (for binary controls) or softmax. The parameters of the nets will be updated with gradient *ascent*, because we want to maximize the objective function .

The annoying thing though, is there is no explicit loss function in this case. We can only compute the derivative, while the true objective function is still intractable. Therefore, monitoring the training process will be quite manual.

In** tensorflow**, since there is no explicit loss function, we would need to use *tf.gradients() *and *apply_gradients() *separately* *to train the policy net.

There are couple of approaches for reducing the variance of the gradient estimate. One simple way is to use a baseline, which is the expected rewards over each episode. This, combined with a discount rate for the discounted future rewards, is used in the well-known REINFORCE paper. Using a discount rate though, will make the estimate become bias. A rate of gives unbias estimate, but gives bias estimate because, intuitively, it assumes the rewards of timesteps too far in the future have less effect than the near future.

More sophisticated approaches include *Natural Gradients* and *Trust-Region Policy Optimization* (TRPO), where a *surrogate objective* function is used instead of the function (more of that in here and here). *Deterministic Policy Gradients* is also another approach.

Yet another way is to pre-train the policy net to mimic (human) expert behavior in a supervised setting, of course only when such a labelled dataset is available. This is one of the trick used in the AlphaGo paper.

However, the trend nowadays is to use some form of *Actor-Critic*, which, as David Silver said, is the most general form of RL.