Skip to content

11.1 Behavior Cloning and Interactive Imitation Learning

Chapter 10: Offline Reinforcement Learning improves a policy using fixed historical data, but the data still retain rewards. Imitation learning operates with less information: the data only tell us which action an expert took in a given state, without a ready-made reward function explaining why that action was good.

Chapter 11 proceeds in three steps. We first learn expert actions directly through behavior cloning and DAgger, then infer rewards from demonstrations with inverse reinforcement learning and GAIL, and finally study how policies adapt rapidly to new tasks through MAML, RL², PEARL, and in-context reinforcement learning. This section begins with four questions: how behavior cloning is trained, why its errors compound, how DAgger collects error states, and what data each approach requires.

1. Formulating Expert Demonstrations as Supervised Learning

Chapter 6: Policy Gradients assumes that the environment provides rewards. In many real tasks, however, we have only expert demonstrations—trajectories from human drivers, operation logs from skilled workers, or high-quality question-answer pairs. Imitation learning learns a policy directly from demonstrations, bypassing reward-function design.

1.1 The Behavior-Cloning Objective

The most direct method treats expert data as supervised examples: the state is the input, and the expert action is the label. The higher the probability that the policy assigns to the expert action, the lower the loss:

Here, is the expert-demonstration dataset, and is the probability that the policy selects expert action in state . The negative sign turns “increasing the probability of the expert action” into a minimization problem. Discrete actions usually use cross-entropy, while continuous actions can use mean squared error or the negative log-likelihood of a probability distribution. Supervised fine-tuning of an LLM uses the same conditional-likelihood objective, except that the action is the next token.

Consider one numerical example. An expert demonstration contains a state in which a car is centered in its lane and the expert action is a slight turn to the left. Two policies assign different probabilities to that action:

  • Policy 1 gives , so its loss is .
  • Policy 2 gives , so its loss is .

Policy 2 agrees more strongly with the expert and therefore has the smaller loss. Behavior cloning minimizes this quantity over every demonstration, using the same cross-entropy mechanism as handwritten-digit classification.

python
def behavior_cloning_step(policy_net, expert_batch):
    states, actions = expert_batch
    log_probs = policy_net.log_prob(states, actions)
    loss = -log_probs.mean()  # Negative log-likelihood
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    return loss.item()

2. Why Behavior-Cloning Errors Compound

During training, BC sees the state distribution visited by the expert, . During deployment, it visits the distribution induced by the current policy, . A small error can take the agent to a state absent from the training set, making subsequent errors more likely.

Suppose the policy has an error rate of only on states visited by the expert. It agrees with the expert 99% of the time, but the probability of completing an entire -step task without an error is :

Task length 10 steps50 steps100 steps1,000 steps
Probability of no error0.900.610.370.00004

In a 100-step task, two runs out of three contain at least one error. This is still optimistic because it assumes the error rate remains 1% after the policy leaves the expert trajectory. Ross et al. (2011) express the resulting cumulative task cost as order :

Here, is the task horizon and is the supervised-learning error. The factor indicates that an early error affects both the current step and the states encountered during many later steps. The longer the task, the greater the cost of training only on expert states.

3. Using DAgger to Collect States the Policy Actually Visits

Dataset Aggregation directly supplements the states that the policy will visit but the expert dataset does not cover. The current policy first performs the task, and the expert then supplies the correct actions for those states.

python
def dagger(env, expert, policy_net, n_iterations=20, n_traj_per_iter=50):
    dataset = []
    for it in range(n_iterations):
        # 1. Roll out the current policy (not the expert).
        trajectories = []
        for _ in range(n_traj_per_iter):
            s = env.reset()
            traj = []
            done = False
            while not done:
                # beta mixture: favor the expert early for safety, then the policy later
                beta = max(0.0, 1.0 - it / 10)
                if np.random.rand() < beta:
                    a = expert(s)
                else:
                    a = policy_net.act(s)
                s_next, r, done, _ = env.step(a)
                traj.append((s, a))
                s = s_next
            trajectories.append(traj)

        # 2. Crucially, ask the expert to relabel all policy-visited states,
        #    including failure states.
        for traj in trajectories:
            for s, _ in traj:
                a_expert = expert(s)
                dataset.append((s, a_expert))

        # 3. Retrain the policy on the expanded dataset.
        train_bc(policy_net, dataset)

Following the iterations makes the distribution change concrete. During the first rounds, is close to 1, so the expert performs most actions and the data mainly contain ordinary states such as a centered car at a suitable speed. Later, falls toward one half and the imperfect policy begins producing states such as crossing a lane marking or driving too fast. The expert labels these previously unseen states, filling precisely the gaps in which the policy is likely to fail. Once reaches zero, every new failure state produced by independent policy execution can be labeled and learned in the next round. The growing dataset therefore approaches the policy's own state distribution .

Under conditions such as no-regret online learning, the cumulative cost can improve from BC's to order . The cost is that the expert must be queried repeatedly during training.

4. Comparing BC, DAgger, and GAIL

MethodSource of training dataAddresses distribution shiftRequires online expert labels
BCOffline expert data only
DAggerExpert data + policy-visited states✅ (key limitation)
GAILExpert data + policy rollouts✅ (implicitly)❌ (only state-action pairs)

DAgger's engineering bottleneck is its requirement for online expert interaction. A human driver, for example, cannot easily label the correct action in real time for every unusual state visited by a policy. This limitation motivates the inverse-RL approach in the next section, which infers rewards from demonstrations.

Section Summary

Behavior cloning (BC) is the simplest form of imitation learning: it treats expert trajectories as supervised data for policy training. Its central difficulty is distribution shift: training covers only the expert state distribution, and once the deployed policy deviates, it may not recover. DAgger addresses this problem by having an expert correct the agent's actual trajectories.

The next section, 11.2 Inverse Reinforcement Learning and GAIL, no longer imitates actions directly. Instead, it infers the reward function from expert behavior—the defining idea of inverse reinforcement learning (IRL).

Hands-on Modern Reinforcement Learning