Modern Deep Reinforcement Learning/Imitation Learning: Cloning, DAgger, and Inverse RL

Lesson 5.112,642 words

Imitation Learning: Cloning, DAgger, and Inverse RL

When a reward is hard to specify but an expert is easy to watch, learn from demonstrations instead. Behavioral cloning treats control as supervised learning of the expert's state-to-action map, and fails through compounding error: small mistakes carry the agent off the expert's distribution, where it was never trained.

╌╌╌╌

Every method so far assumed a reward. The environment hands the agent a scalar , and all the machinery — value functions, Bellman backups, policy gradients — exists to turn that signal into behavior. But in many of the tasks people most want to automate, the reward is the hard part. What is the numeric reward for driving well, for a surgical suture, for a natural backflip? Write one down by hand and the agent will exploit its every gap: reward forward speed and it drives into walls sideways, reward staying on the road and it circles forever going nowhere. The signal a designer can articulate is rarely the signal the task actually needs.

What is often available instead is an expert. A human driver, a teleoperated robot arm, a champion game log — a policy we cannot write down but can watch, producing demonstrations

trajectories of states paired with the actions the expert chose. Imitation learning asks: given , recover a policy that behaves like , using the demonstrations in place of a reward. Inverse reinforcement learning asks the deeper question: what reward was the expert optimizing, so that we could re-run ordinary RL and perhaps surpass the demonstrator? Both sidestep reward specification; they differ in whether they copy the behavior or reverse-engineer its cause.

This lesson builds the classical trio: behavioral cloning and its compounding-error flaw, DAgger's fix by querying the expert, and inverse RL's recovery of the reward itself. A companion lesson then dissolves the reward entirely, casting imitation as an adversarial game (GAIL) and reading a transferable reward back out of it (AIRL).

Behavioral cloning

The simplest idea is also the most tempting: forget that this is a control problem at all. Each demonstrated transition is a labelled example — input state, target action — so a supervised learner can fit a policy by maximum likelihood, exactly as it would fit any classifier or regressor.

This is behavioral cloning (BC): imitation reduced to a single pass of supervised learning over the demonstration set.1 No environment interaction, no value function, no reward — just a labelled dataset and a cross-entropy (for discrete actions) or mean-squared (for continuous actions) loss. When it works it is unbeatably cheap, and it is genuinely useful as a warm start: pretraining a policy by BC before switching to policy-gradient fine-tuning is standard practice, because a randomly initialized policy wastes enormous effort just discovering the plausible actions BC already knows.

Behavioral cloning as supervised learning. Each demonstrated transition is treated as a labeled example: the state is the input, the expert's action is the target. A single supervised fit maps states to actions, ignoring that the policy will later choose the states it sees.

The fatal flaw: compounding error

Behavioral cloning makes an assumption supervised learning always makes and control never permits: that the test inputs are drawn from the same distribution as the training inputs. In classification, the images you evaluate on look like the images you trained on. In control, the policy chooses its own inputs. The states the cloned policy visits at test time are generated by its own actions, not the expert's, and the moment it makes one imperfect prediction it lands in a state the expert would never have entered — a state absent from , where the policy's behavior is pure extrapolation.

This is covariate shift, and control turns it into a feedback loop. Suppose the cloned policy matches the expert with per-step error probability . A single mistake nudges the trajectory slightly off the demonstrated path. Off-path states were undertrained, so the next prediction is more likely to err, nudging the trajectory further still. Errors do not average out; they accumulate, because each one changes the distribution of states the next decision faces.

Compounding error in behavioral cloning. The expert path (blue) stays in the demonstrated region (shaded); the cloned policy (red) makes a small error, enters an off-distribution state it was never trained on, errs more there, and drifts away — quadratically in the horizon rather than linearly.

Ross and Bagnell made the scaling precise: a policy with per-step error under the expert's distribution can suffer total cost that grows as over a horizon of steps, rather than the a naive per-step bound would suggest.2 The extra factor of is precisely the compounding: the policy is not just wrong at steps, it is wrong in states it drove itself into, which are worse than the states the bound was measured on. Double the demonstrations and shrinks, but the structure remains — behavioral cloning is brittle in a way no amount of data of the same kind fully removes.

Where the extra comes from, concretely. Picture a corridor task with an absorbing off-track trap: at each step the cloned policy matches the expert with probability and, with probability , takes a wrong action that dumps it into the trap, where it stays and accrues cost per remaining step. A mistake at step therefore costs about — the entire remaining horizon — not a single unit. Summing over when the first mistake lands, the expected total cost is on the order of

Put numbers on it: with steps and a per-step error (the clone is right of the time), the naive bound suggests about one unit of cost, but the compounding bound gives fifty — a fiftyfold gap, and it widens with the horizon. To halve the realized cost you must halve , which under supervised learning means roughly quadrupling the demonstration set; the horizon term never goes away. This is why a behavioral-cloning driving policy that looks flawless on held-out frames still wanders off the road after a few seconds of control: the held-out frames measure , the road measures .

Cost growth with the horizon. A method trained on its own induced distribution (DAgger, blue) accrues cost linearly in T; behavioral cloning (red) accrues cost quadratically, because each early mistake pushes the policy into off-distribution states that stay costly for the rest of the episode. The gap widens without bound as the horizon grows.

The root cause is the i.i.d. assumption. Supervised learning is sound only when train and test draw from one fixed distribution; a policy breaks that assumption by construction, since depends on itself. Any honest fix must confront the distribution the learner actually visits — not the one the expert did.

DAgger: fixing the distribution mismatch

If the problem is that the learner is trained on the expert's states but tested on its own, the fix writes itself: train it on its own states. The catch is that we only have labels for the expert's states — for a state the learner drifts into, we do not know what the expert would have done. DAgger (Dataset Aggregation) resolves this by keeping the expert in the loop: run the current learner, let it visit its own (possibly mistaken) states, and query the expert for the correct action at each one.3

The result is an iterative dataset-aggregation loop. At iteration , roll out the current policy to collect the states it actually visits, ask the expert to label those states with correct actions, add the new labelled pairs to the growing dataset, and retrain on the aggregate. Over iterations the training distribution converges to the distribution the learner induces — the very distribution it will be tested on.

Algorithm:DAgger\textsc{DAgger} — dataset aggregation with an interactive expert
  1. 1
    input: expert πE\pi_E, number of iterations NN, mixing schedule βi\beta_i
  2. 2
    D\mathcal{D} \gets \varnothing
  3. 3
    initialize π1\pi_1 (e.g. by behavioral cloning on expert demonstrations)
  4. 4
    for i=1i = 1 to NN do
  5. 5
    πβiπE+(1βi)πi\pi \gets \beta_i\,\pi_E + (1 - \beta_i)\,\pi_i
    mix in the expert early on
  6. 6
    sample trajectories by running π\pi in the environment
  7. 7
    collect the visited states {s}\{s\} into a dataset Di\mathcal{D}_i
  8. 8
    for each sDis \in \mathcal{D}_i do
  9. 9
    query the expert for a=πE(s)a = \pi_E(s)
    label the LEARNER's states
  10. 10
    DDDi\mathcal{D} \gets \mathcal{D} \cup \mathcal{D}_i
    aggregate, do not discard
  11. 11
    train πi+1\pi_{i+1} on all of D\mathcal{D} by supervised learning
  12. 12
    return the best πi\pi_i on validation

The mixing coefficient is the one subtlety. Early on the learner is bad, so rolling it out unguided produces useless (or dangerous) states; DAgger blends in the expert with weight , starting near (roll out the expert) and decaying to (roll out the pure learner). By the final iterations the states collected are the learner's own, and the aggregated dataset covers exactly the region the learner drifts into.

The DAgger loop. Run the current policy to collect the states it actually visits, have the expert label those states with correct actions, aggregate them into the dataset, and retrain — closing the gap between the training distribution and the distribution the learner induces.

DAgger converts imitation into a no-regret online learning problem, and Ross et al. prove that under this scheme the total error grows linearly, , recovering the benign scaling that behavioral cloning loses. The price: DAgger needs an interactive expert, one that can be queried on arbitrary states the learner invents — not a fixed log of past demonstrations. A recorded dataset of expert driving cannot tell you what the expert would do in the weird half-off-road state your bad policy produced; only a live expert can. Where that expert is available (a human labeller, a slow-but-correct planner, a teleoperator), DAgger is the standard fix for compounding error. Where it is not, you are back to either plain cloning or the reward-recovery methods below.

Inverse reinforcement learning

Cloning copies what the expert did. Inverse reinforcement learning (IRL) recovers the why: the reward function under which the observed behavior is optimal. The payoff is generalization: a reward is a far more portable object than a policy. If you can recover the reward, you can re-run ordinary RL from it in a new environment, with different dynamics, or for longer than the expert ever demonstrated — and potentially exceed the demonstrator, since a reward describes the goal rather than one particular way of reaching it.4

Why it is ill-posed

IRL is fundamentally underdetermined, and Ng and Russell made the point exactly in the paper that framed the problem: for any expert policy, many reward functions make it optimal. The trivial witness is : under a constant zero reward every policy is optimal, so the expert's policy is optimal too, and explains any demonstration whatsoever. Less trivially, reward shaping shows that adding a potential-based term to any reward leaves the optimal policy unchanged, so an entire family of rewards induces identical behavior. The mapping from behavior back to reward is many-to-one; observing a policy determines only an equivalence class of rewards, never a single one.

Inverse RL is ill-posed. Many distinct reward functions (left) all make the same expert policy optimal (right), so behavior alone cannot single one out — the trivial among them. A criterion beyond consistency, such as maximum entropy, is needed to choose.

Early IRL work resolved the ambiguity with heuristics — prefer a reward under which the expert beats alternatives by the largest margin, or match the expected feature counts of the demonstrations. Feature matching is the key structural idea: assume the reward is linear in known features, , and require that the learner's policy match the expert's expected discounted feature counts, . Matching feature counts guarantees the learner's value equals the expert's under that linear reward. But feature matching alone is still degenerate: many policies (many stochastic mixtures) match the same feature counts, and nothing selects among them.

Maximum-entropy IRL

The clean resolution is a principled tie-breaker: among all policies (or all trajectory distributions) that match the expert's feature counts, pick the one that is maximally random otherwise. Committing to nothing beyond what the data forces is the maximum-entropy principle, and applying it to IRL gives a distribution over trajectories that is exponential in reward.5 Maximum-entropy IRL models the expert as producing trajectories with probability proportional to the exponential of their total reward,

where higher-reward trajectories are exponentially more likely but no trajectory is ever ruled out. This is a soft, probabilistic model of a near-optimal expert: it tolerates the occasional suboptimal action rather than demanding strict optimality, which is both more realistic and mathematically better-behaved. Fitting by maximum likelihood on the demonstrations has a clean gradient — the difference between the expert's expected features and the model's expected features — and its maximum-entropy form uniquely resolves the ambiguity that made plain feature matching degenerate. Ziebart et al. introduced it for modeling real driver-route choices, and it became the template for essentially all subsequent probabilistic IRL, including the deep variants that replace with a neural reward.

A worked feature-matching step. Take a gridworld with two features per state: if is on a highway, and if is a scenic back road, both otherwise, with reward . Suppose the expert's demonstrated trajectories spend, in expected discounted feature counts, — mostly highway. Start the reward at ; then every action is equally good, the model's induced policy wanders uniformly, and its feature counts come out . The maximum-entropy gradient equals the feature-count mismatch,

so a gradient step raises (reward highway more) and lowers (reward back roads less). Re-solve the forward problem under the new reward, the induced policy now favors the highway, its feature counts move toward , the gradient shrinks, and at convergence the model matches the expert's counts exactly — — which under the linear reward means the model's value equals the expert's. The recovered encodes that the expert values highway over scenery — an explanation of the behavior that transfers to a new map with different roads, where a cloned policy transfers nothing.

The recurring cost of IRL is that it is a double loop. Every gradient step on the reward requires computing the model's expected feature counts (the above), which means solving the forward RL problem under the current reward — a full planning or RL run nested inside each reward update. In the worked example, each of the arrows re-solve the forward problem is an entire value iteration. IRL recovers a portable reward, but pays for it with an expensive inner optimization that BC never needs.

The IRL double loop. An outer loop adjusts the reward weights w by the feature-count mismatch (expert counts minus model counts); each such update requires an inner loop that fully re-solves the forward RL problem under the current reward to recompute the model's feature counts. The nested forward solve is what makes IRL expensive.

Where this leaves us

Three ways to learn from demonstrations are now in hand, sorted by how they confront the covariate shift that sinks naive cloning:

  • Behavioral cloning treats control as supervised learning and pays for the category error with compounding — it trains on the expert's states but is tested on the ones its own mistakes create.
  • DAgger fixes the mismatch directly by querying an interactive expert on the learner's own states, recovering the benign scaling.
  • Inverse RL recovers the reward the expert seems to optimize — a portable object that transfers to new dynamics — resolving the ill-posedness with the maximum-entropy principle, at the cost of a forward-RL solve in the inner loop.

That IRL double loop is the opening the next lesson exploits. If the point of recovering a reward is only to re-run RL and match the expert, maybe you can skip the reward and match the behavior directly — an occupancy-matching game that turns out to be a GAN. That reframing (GAIL), and the way to read a transferable reward back out of it (AIRL), plus a comparison of all the methods and the modern lines that grew from them, continue in Imitation as Adversarial Matching: GAIL and AIRL.

Footnotes

  1. The supervised-imitation baseline predates deep RL; it appears as ALVINN (Pomerleau, 1989), ALVINN: An Autonomous Land Vehicle in a Neural Network, NeurIPS — a neural net cloning a human driver's steering, and an early demonstration of both the appeal and the brittleness of behavioral cloning.
  2. Ross & Bagnell (2010), Efficient Reductions for Imitation Learning, AISTATS — the analysis showing behavioral cloning incurs cost growing as in the horizon, versus for methods that train on the learner's own induced distribution.
  3. Ross, Gordon, Bagnell (2011), A Reduction of Imitation Learning and Structured Prediction to No-Regret Online Learning, AISTATS — introduces DAgger, aggregating expert labels on the learner's visited states and proving the resulting linear-in-horizon error bound.
  4. Ng & Russell (2000), Algorithms for Inverse Reinforcement Learning, ICML — the paper that framed IRL, characterized the set of rewards consistent with an optimal policy, and noted the degeneracy (including ) that makes the problem ill-posed.
  5. Ziebart, Maas, Bagnell, Dey (2008), Maximum Entropy Inverse Reinforcement Learning, AAAI — resolves IRL's ambiguity with the maximum-entropy principle, modeling trajectories as and fitting the reward by maximum likelihood on demonstrated feature counts.

╌╌ END ╌╌