# End-to-End Differentiable Adversarial Imitation Learning

# Introduction

The ability to imitate an expert policy is very beneficial in the case of automating human demonstrated tasks. Assuming that a sequence of state action pairs (trajectories) of an expert policy are available, a new policy can be trained that imitates the expert without having access to the original reward signal used by the expert. There are two main approaches to solve the problem of imitating a policy; they are Behavioural Cloning (BC) and Inverse Reinforcement Learning (IRL). BC directly learns the conditional distribution of actions over states in a supervised fashion by training on single time-step state-action pairs. The disadvantage of BC is that the training requires large amounts of expert data, which is hard to obtain. In addition, an agent trained using BC is unaware of how its action can affect future state distribution. The second method using IRL involves recovering a reward signal under which the expert is uniquely optimal; the main disadvantage is that it’s an ill-posed problem.

To address the problem of imitating an expert policy, techniques based on Generative Adversarial Networks (GANs) have been proposed in recent years. GANs use a discriminator to guide the generative model towards producing patterns like those of the expert. This idea was used by (Ho & Ermon, 2016) in their work titled Generative Adversarial Imitation Learning (GAIL) to imitate an expert policy in a model-free setup. The disadvantage of GAIL’s model-free approach is that backpropagation required gradient estimation which tends to suffer from high variance, which results in the need for large sample sizes and variance reduction methods. This paper proposed a model-based method (MGAIL) to address these issues.

# Background

## Imitation Learning

A common technique for performing imitation learning is to train a policy __(pi)__ that minimizes some loss function __l()__ with respect to a discounted state distribution encountered by the expert: __dPi(...)__. This can be obtained using any supervised learning (SL) algorithm, but the policy's prediction affects future state distributions; this violates the independent and identically distributed (i.i.d) assumption made my most SL algorithms. This process is susceptible to compounding errors since a slight deviation in the learner's behavior can lead to different state distributions not encountered by the expert policy.

This issue was overcome through the use of the Forward Training (FT) algorithm which trains a non-stationary policy iteratively overtime. At each time step a new policy is trained on the state distribution induced by the previously trained policies. This is continued till the end of the time horizon to obtain a policy that can mimic the expert policy. This requirement to train a policy at each time step till the end makes the FT algorithm impractical for cases where the time horizon is very large or undefined. This short coming is resolved using the Stochastic Mixing Iterative Learning (SMILe) algorithm. SMILe trains a stochastic stationary policy over several iterations under the trajectory distribution induced by the previously trained policy.

## Generative Adversarial Networks

GANs learn a generative model that can fool the discriminator by using a two-player zero-sum game: __formula__.

In the above equation, pE represents the expert distribution and pZ represents the input noise distribution from which the input to the generator is sampled. The generator produces patterns and the discriminator judges if the pattern was generated or from the expert data. When the discriminator cannot distinguish between the two distributions the game ends and the generator has learned to mimic the expert. GANs rely on basic ideas such as binary classification and algorithms such as backpropagation in order to learn the expert distribution.

GAIL applies GANs to the task of imitating an expert policy in a model-free approach. GAIL uses similar objective functions like GANs, but the expert distribution in GAIL represents the joint distribution over state action tuples: __formula__

This problem cannot be solved using the standard methods described for GANs because the generator in GAIL represents a stochastic policy. The exact form of the first term in the above equation is given by: __formula__.

The two-player game now depends on the stochastic properties (__theta__) of the policy, and it is unclear how to differentiate the above equation with respect to __theta__. This problem can be overcome using score functions such as REINFORCE to obtain an unbiased gradient estimation: __2 functions__.

REINFORCE gradients suffer from high variance which makes them difficult to work with even after applying variance reduction techniques. In order to better understand the changes required to fool the discriminator we need access to the gradients of the discriminator network, which can be obtained from the Jacobian of the discriminator. This paper demonstrates the use of a forward model along with the Jacobian of the discriminator to train a policy, without using high-variance gradient estimations.

# Algorithm

This section first analyzes the characteristics of the discriminator network, then describes how a forward model can enable policy imitation through GANs. Lastly, the model based adversarial imitation learning algorithm is presented.

## The discriminator network

The discriminator network is trained to predict the conditional distribution: __formula__ where __formula__.

The discriminator is trained on an even distribution of expert and generated examples; hence __p(pi) = p(piE) = 1/2__. Given this, we can rearrange and factor __D(s,a) __ to obtain: __formula 6, 7 and 8__.

__Q(s, a) __ represents a policy likelihood ratio, and __W(s) __ represents a state distribution likelihood ratio. Based on these expressions, the paper states that the discriminator makes its decisions by answering two questions. The first question relates to state distribution: what is the likelihood of encountering state __s__ under the distribution induces by __piE vs pi__? The second question is about behavior: given a state s, how likely is action a under __piE vs. pi__? The desired change in state is given by (__formula__); this information can by obtained from the partial derivatives of __D(s,a): formula__.

## Backpropagating through stochastic units

There is interest in training stochastic policies because stochasticity encourages exploration for Policy Gradient methods. This is a problem for algorithms that build differentiable computation graphs where the gradients flow from one component to another since it is unclear how to backpropagate through stochastic units. The following subsections show how to estimate the gradients of continuous and categorical stochastic elements for continuous and discrete action domains respectively.

### Continuous Action Distributions

In the case of continuous action policies, re-parameterization was used to enable computing the derivatives of stochastic models. Assuming that the stochastic policy has a Gaussian distribution the __policy (pi) __ can be written as __pi(a|s)…, where e=N(0,1) __. This way, the authors are able to get a Monte-Carlo estimator of the derivative of the expected value of __D(s, a) __ with respect to __theta: formula__.

### Categorical Action Distributions

In the case of discrete action domains, the paper uses categorical re-parameterization with Gumbel-Softmax. This method relies on the Gumble-Max trick which is a method for drawing samples from a categorical distribution with class probabilities __…: formula__.

Gumbel-Softmax provides a differentiable approximation of the samples obtained using the Gumble-Max trick: __formula__.

In the above equation, the hyper-parameter __tau__ (temperature) trades bias for variance. When __tau__ gets closer to zero, the softmax operator acts like argmax resulting in a low bias, but high variance; vice versa when the __tau__ in large.

The authors use __a_softmax__ to interact with the environment; argmax is applied over __a_softmax__ to obtain a single “pure” action, but the continuous approximation is used in the backward pass using the estimation: __formula__.

## Backpropagating through a Forward model

The above subsections presented the means for extracting the partial derivative (__formula__). The main contribution of this paper is incorporating the use of (__formula__). In a model-free approach the state __s__ is treated as a fixed input, therefore (__formula__) is discarded. This work uses a model-based approach which makes incorporating (formula) more involved. In the model-based approach, a state __st__ can be written as a function of the previous state action pair: __formula__, where __f__ represents the forward model. Using the forward model and the law of total derivatives we get: __formula_11__.

Using this formula, the error regarding deviations of future states (__~__) propagate back in time and influence the actions of policies in earlier times. __This is summarized in figure 3__.

## MGAIL Algorithm

Shalev- Shwartz et al. (2016) and Heess et al. (2015) built a multi-step computation graph for describing the familiar policy gradient objective; in this case it is given by: (__formulas__). Using the results from Heess et al. (2015) this paper demonstrates how to differentiate __J(theta)__ over a trajectory of __(s,a,s’)__ transitions: __formula_12_13__. The policy gradient (__~__) is calculated by applying __equations 12 and 13__ recursively for __t__ iterations.

__ Main Algorithm __

## Forward Model Structure

The stability of the learning process depends on the prediction accuracy of the forward model, but learning an accurate forward model is challenging by itself. The authors propose methods for improving the performance of the forward model based on two aspects of its functionality. First, the forward model should learn to use the action as an operator over the state space. To accomplish this, the actions and states, which are sampled form different distributions need to be first represented in a shared space. This is done by encoding the state and action using two separate neural networks and combining their outputs to form a single vector. Additionally, multiple previous states are used to predict the next state by representing the environment as an __n’th__ order MDP. A GRU layer is incorporated into the state encoder to enable recurrent connections from previous states. Using these modifications, the model is able to achieve better, and more stable results compared to the standard forward model based on a feed forward neural network. The comparison is presented in __Figure 5__.

# Experiments

The proposed algorithm is evaluated on three discrete control tasks (Cartpole, Mountain-Car, Acrobot), and five continuous control tasks (Hopper, Walker, Half-Cheetah, Ant, and Humanoid), which are modeled by the MuJoCo physics simulator (Todorov et al., 2012). Expert policies are trained using the Trust Region Policy Optimization (TRPO) algorithm (Schulman et al., 2015). Different number of trajectories are used to train the expert for each task, but all trajectories are of length 1000. The discriminator and generator (policy) networks contains two hidden layers with ReLU non-linearity and are trained using the ADAM optimizer. The total reward received over a period of __N__ steps using BC, GAIL and MGAIL is presented in __Table 1__. The proposed algorithm achieved the highest reward for most environments while exhibiting performance comparable to the expert over all of them.

# Discussion

This paper presented a model-free algorithm for imitation learning. It demonstrated how a forward model can be used to train policies using the exact gradient of the discriminator network. A downside of this approach is the need to learn a forward model, since this could be difficult in certain domains. Learning the system dynamics directly from raw images is considered as one line of future work. Another future work is to address the violation of the fundamental assumption made by all supervised learning algorithms, which requires the data to be i.i.d. This problem arises because the discriminator and forward models are trained in a supervised learning fashion using data sampled from a dynamic distribution.

# Source

- Baram, Nir, et al. "End-to-end differentiable adversarial imitation learning." International Conference on Machine Learning. 2017.