Skip to article frontmatterSkip to article content

7 Imitation Learning

7.1Introduction

Imagine you are tasked with learning how to drive. How do, or did, you go about it? At first, this task might seem insurmountable: there are a vast array of controls, and the cost of making a single mistake could be extremely high, making it hard to explore by trial and error. Luckily, there are already people in the world who know how to drive who can get you started. In almost every challenge we face, we “stand on the shoulders of giants” and learn skills from experts who have already mastered them.

a robot imitating the pose of a young child (Photo by Pavel Danilyuk: https://www.pexels.com/photo/a-robot-imitating-a-girl-s-movement-8294811/)

Now in machine learning, we are often trying to teach machines to accomplish tasks that humans are already proficient at. In such cases, the machine learning algorithm is the one learning the new skill, and humans are the “experts” that can demonstrate how to perform the task. Imitation learning is an approach to reinforcement learning where we aim to learn a policy that performs at least as well as the expert. It is often used as a first step for complex tasks where it is impractical to learn from scratch.

We’ll see that the most naive form of imitation learning, called behavioral cloning (or “behavior cloning”), is really an application of supervised learning to interactive tasks. We’ll then explore dataset aggregation (DAgger) as a way to query an expert and learn even more effectively.

7.2Behavioral cloning

This notion of “learning from human-provided data” may remind you of the basic premise of 4 Supervised learning. In supervised learning, there is some mapping from inputs to outputs, such as the task of assigning the correct label to an image, that humans can implicitly compute. To teach a machine to calculate this mapping, we first collect a large training dataset by getting people to label a lot of inputs, and then use some optimization algorithm to produce a predictor that maps from the inputs to the outputs as closely as possible.

How does this relate to interactive tasks? Here, the input is the observation seen by the agent and the output is the action it selects, so the mapping is the agent’s policy. What’s stopping us from applying supervised learning techniques to mimic the expert’s policy? In principle, nothing! This is called behavioral cloning.

Typically, this second task can be framed as empirical risk minimization (which we previously saw in Definition 4.1):

π~=argminπΠn=0N1loss(π(sn),an)\widetilde{\pi} = \arg\min_{\pi \in \Pi} \sum_{n=0}^{N-1} \text{loss}(\pi(s^n), a^n)

where Π is some class of possible policies, loss\text{loss} is the loss function to measure how different the policy’s prediction is from the true observed action, and the supervised learning algorithm itself, also known as the fitting method, tells us how to compute this argmin\arg\min.

How should we choose the loss function? In supervised learning, we saw that the mean squared error is a good choice for continuous outputs. However, how should we measure the difference between two actions in a discrete action space? In this setting, the policy acts more like a classifier that picks the best action in a given state. Rather than considering a deterministic policy that just outputs a single action, we’ll consider a stochastic policy π that outputs a distribution over actions. This allows us to assign a likelihood to observing the entire dataset D\mathcal{D} under the policy π, as if the state-action pairs are independent:

Pπ(D)=n=1Nπ(ansn)\pr_\pi(\mathcal{D}) = \prod_{n=1}^{N} \pi(a_n \mid s_n)

Note that the states and actions are not, however, actually independent! A key property of interactive tasks is that the agent’s output -- the action that it takes -- may influence its next observation. We want to find a policy under which the training dataset D\mathcal{D} is the most likely. This is called the maximum likelihood estimate of the policy that generated the dataset:

π~=argmaxπΠPπ(D)\widetilde{\pi} = \arg\max_{\pi \in \Pi} \pr_{\pi}(\mathcal{D})

This is also equivalent to doing empirical risk minimization with the negative log likelihood as the loss function:

π~=argminπΠlogPπ(D)=argminπΠn=1Nlogπ(ansn)\begin{align*} \widetilde{\pi} &= \arg\min_{\pi \in \Pi} - \log \pr_\pi(\mathcal{D}) \\ &= \arg\min_{\pi \in \Pi} \sum_{n=1}^N - \log \pi(a_n \mid s_n) \end{align*}

7.2.1Performance of behavioral cloning

Can we quantify how well this algorithm works? For simplicity, let’s consider the case where the action space is finite and both the expert policy and learned policy are deterministic. Suppose the learned policy obtains ε\varepsilon classification error. That is, for trajectories drawn from the expert policy, the learned policy chooses a different action at most ε\varepsilon of the time:

Eτρπexpert[1Hh=0H11{π~(sh)πexpert(sh)}]ε\mathbb{E}_{\tau \sim \rho_{\pi_{\text{expert}}}} \left[ \frac 1 \hor \sum_{\hi=0}^{\hor-1} \ind{ \widetilde{\pi}(s_\hi) \ne \pi_{\text{expert}} (s_\hi) } \right] \le \varepsilon

Then, their value functions differ by

VπexpertVπ~H2ε| V^{\pi_{\text{expert}}} - V^{\widetilde{\pi}} | \le H^2 \varepsilon

where HH is the horizon.

7.3Distribution shift

Let us return to the driving analogy. Suppose you have taken some driving lessons and now feel comfortable in your neighbourhood. But today you have to travel to an area you haven’t visited before, such as a highway, where it would be dangerous to try and apply the techniques you’ve already learned. This is the issue of distribution shift: a policy learned under a certain distribution of states may not perform well if this distribution changes.

This is already a common issue in supervised learning, where the training dataset for a model might not resemble the environment where it gets deployed. In interactive environments, this issue is further exacerbated by the dependency between the observations and the agent’s behavior; if you take a wrong turn early on, it may be difficult or impossible to recover in that trajectory.

How could you learn a strategy for these new settings? In the driving example, you might decide to install a dashcam to record the car’s surroundings. That way, once you make it back to safety, you can show the recording to an expert, who can provide feedback at each step of the way. Then the next time you go for a drive, you can remember the expert’s advice, and take a safer route. You could then repeat this training as many times as desired, thereby collecting the expert’s feedback over a diverse range of locations. This is the key idea behind dataset aggregation.

7.4Dataset aggregation (DAgger)

The DAgger algorithm (Ross et al. (2010)) assumes that we have query access to the expert policy. That is, for a given state ss, we can ask for the expert’s action πexpert(s)\pi_{\text{expert}}(s) in that state. We also need access to the environment for rolling out policies. This makes DAgger an online algorithm, as opposed to pure behavioral cloning, which is offline since we don’t need to act in the environment at all.

You can think of DAgger as a specific way of collecting the dataset D\mathcal{D}.

We leave the implementation as an exercise. How well does DAgger perform? A full proof can be found in Ross et al. (2010) that under certain assumptions, the DAgger algorithm can better approximate the expert policy:

VπexpertVπDAggerHε|V^{\pi_{\text{expert}}} - V^{\pi_{\text{DAgger}}}| \le H \varepsilon

where ε\varepsilon is the “classification error” guaranteed by the supervised learning algorithm.

7.5Summary

For tasks where it is too difficult or expensive to learn from scratch, we can instead start off with a collection of expert demonstrations. Then we can use supervised learning techniques to find a policy that imitates the expert demonstrations.

The simplest way to do this is to apply a supervised learning algorithm to an already-collected dataset of expert state-action pairs. This is called behavioral cloning. However, given query access to the expert policy, we can do better by integrating its feedback in an online loop. The DAgger algorithm is one way of doing this, where we use the expert policy to augment trajectories and then learn from this augmented dataset using behavioral cloning.

References
  1. Ross, S., Gordon, G. J., & Bagnell, J. (2010, November). A Reduction of Imitation Learning and Structured Prediction to No-Regret Online Learning. International Conference on Artificial Intelligence and Statistics.