In reinforcement learning, an agent discovers a policy from rewards. But sometimes it is much easier to demonstrate the desired behavior than to write a reward function for it.
Imitation learning starts with expert trajectories
τ(i)=(o1(i),a1(i),…,oH(i),aH(i)),
and learns a policy that predicts the expert's action from the information available at each time step:
at∼πθ(at∣ot).
This looks like supervised learning: ot is the input, at is the label, and the policy is a conditional predictive model. The difference is that a policy changes its future inputs:
at∼πθ(at∣ot),st+1∼p(st+1∣st,at).
A prediction error can move the agent to an unfamiliar state, where another error becomes more likely. This feedback loop is the central difficulty in imitation learning.
When the policy observes the complete state, ot=st, we can use a Markov policy
πθ(at∣st).
The state is Markov when it contains everything needed to predict the next state:
p(st+1∣s1,a1,…,st,at)=p(st+1∣st,at).
In a partially observed environment, the policy only receives
ot∼p(ot∣st).
The same observation can correspond to different hidden states. We may then condition the policy on its history
ht=(o1,a1,…,ot−1,at−1,ot),
so that
πθ(at∣ht)=πθ(at∣o1:t,a1:t−1).
Imitation learning does not require us to explicitly learn the observation or transition model. However, those dynamics still determine which inputs the policy encounters after deployment.
Behavioral Cloning
The simplest imitation-learning algorithm is behavioral cloning. We collect a dataset of expert trajectories
D={τ(i)}i=1N
and maximize the likelihood of the demonstrated actions:
Behavioral cloning fits actions on observations generated by the expert:
ot∼pπ∗(ot).
At deployment, observations are generated by the learned policy:
ot∼pπθ(ot).
These distributions need not agree:
pπ∗(ot)=pπθ(ot).
Ordinary supervised learning assumes that predictions do not determine which example appears next. Sequential decision making breaks this assumption. A steering error moves a vehicle away from the lane center, creating a camera image absent from the clean demonstrations. The policy is less reliable there, so another error becomes more likely.
The objective controls error under the expert distribution,
Es∼pπ∗[ℓ(πθ(s),π∗(s))],
but deployment performance depends on
Es∼pπθ[ℓ(πθ(s),π∗(s))].
A low behavioral-cloning loss does not guarantee a low on-policy loss.
Compounding errors
Let c(s,a)=1[a=π∗(s)]. Suppose the learner has error at most ϵ under expert states:
Est∼pπ∗[at∼πθ(⋅∣st)Pr(at=π∗(st))]≤ϵ.
If the H decisions were independent test examples, the expected number of errors would be O(ϵH). But an early policy error changes every later input. In the worst case,
The probability of making at least one mistake before time t is bounded by
Pr(an earlier mistake)≤(t−1)ϵ.
If no earlier mistake occurs, the learner remains on an expert-like trajectory and makes an error with probability at most ϵ. After a mistake, we pessimistically allow the cost to be one. Hence
E[c(st,at)]≤ϵ+(t−1)ϵ=tϵ.
Summing over the horizon,
JH(πθ)≤t=1∑Htϵ=ϵ2H(H+1)=O(ϵH2).
Pr(k=1⋃t−1Ek)≤k=1∑t−1Pr(Ek).
This is the union bound; independence is not required.
The argument is deliberately pessimistic: it permits one mistake to place the learner in states where it fails forever.
The bound explains why imperfect demonstrations can sometimes be more useful than pristine ones: a mistake followed by a good correction teaches the policy how to recover.
Dataset Aggregation
Behavioral cloning trains on expert-induced states. DAgger, or Dataset Aggregation, instead asks the expert to label states induced by the learner:
st∼pπθ(st),at∗=π∗(st).
Starting with an expert dataset D0, iteration k trains
θk=argθmax(s,a∗)∈Dk∑logπθ(a∗∣s).
We then execute πθk, ask the expert to label the visited states, and aggregate the new examples:
Dk+1=Dk∪{(s,π∗(s)):s∼pπθk}.
DAggerD←initial expert demonstrations.Repeat:train πθ on D,roll out πθ to collect states Sπ,query a∗(s)=π∗(s) for every s∈Sπ,D←D∪{(s,a∗(s)):s∈Sπ}.Return the final policy.
Aggregation retains expert states, states visited by weak policies, recovery states, and states visited by stronger policies. For safer early rollouts, we can execute a mixture
πkmix=βkπ∗+(1−βk)πθk,
where βk decreases over time.
DAgger directly attacks distributional shift, but it requires interactive expert access. Labeling arbitrary off-trajectory states can be expensive or unsafe. A practical variant lets the expert intervene only when needed and stores the corrective segments. This reduces annotation, but no longer guarantees coverage of every learner-induced state.
Policies with Memory
Distributional shift is not the only reason behavioral cloning fails. The policy class may be unable to represent the expert.
A reactive policy assumes
at⊥(o1,a1,…,ot−1,at−1)∣ot.
This is unsuitable when the correct action depends on an earlier instruction, hidden velocity, a completed subtask, or an object that is now occluded.
A fixed window conditions on recent context:
πθ(at∣ot−K+1:t,at−K+1:t−1).
A recurrent policy instead maintains a learned summary
ht=fθ(ht−1,ot,at−1),πθ(at∣ht).
A causal transformer directly processes (o1,a1,…,ot). In either case, the sequence objective becomes
During training, previous expert actions are usually available through teacher forcing. During deployment, the history contains the learner's actions instead. Memory solves partial observability but can therefore introduce another train-test mismatch.
More context does not guarantee better decisions. A policy can exploit a variable that predicts the expert's action without representing its cause.
Suppose a brake indicator is visible whenever a human driver brakes. A model may learn to respond to the indicator rather than the pedestrian that caused the braking. The indicator is correlated with the label, but arrives too late to determine the correct action:
predictive correlation=causal reason.
History helps when it exposes the true cause, but hurts when it supplies easier spurious shortcuts.
Multimodal Behavior
The same state can admit several valid actions. A driver can pass an obstacle on either side, and a robot can grasp an object from several directions.
Suppose the expert distribution has two modes:
p∗(a∣o)=21δ(a−a(1))+21δ(a−a(2)).
A deterministic policy trained with squared error predicts
μ∗(o)=E[a∣o]=2a(1)+a(2).
But the average of two good actions can be a bad action. Averaging a left turn and a right turn may point directly at the obstacle. We need a policy distribution that preserves distinct modes.
Autoregressive discretization
Discretizing each of d action dimensions into K bins creates Kd joint categories. The probability chain rule avoids this exponential output layer:
πθ(at∣st)=j=1∏dπθ(at,j∣st,at,<j).
The negative log-likelihood decomposes into d categorical losses:
−logπθ(at∣st)=−j=1∑dlogπθ(at,j∣st,at,<j).
This reduces the output size from Kd to roughly dK, while retaining dependencies between action dimensions. The trade-off is sequential decoding and sensitivity to the dimension order.
Conditional latent-variable policies
A generative policy introduces a latent variable z that can select different valid behaviors:
pθ(a∣o)=∫pθ(a∣o,z)p(z∣o)dz.
A conditional VAE uses an approximate posterior qϕ(z∣o,a) during training and maximizes
The reconstruction term teaches the decoder to produce the expert action. The KL term aligns the posterior used for training with the prior available at test time. The general ELBO derivation is developed in the variational autoencoder notes.
At deployment,
z∼p(z∣o),a∼pθ(a∣o,z).
Different samples of z can represent different plans. This only works if the decoder actually uses z; otherwise the latent variable collapses and the policy becomes unimodal again.
Flow-matching policies
Flow matching learns a vector field that transports noise into the action distribution. Let
a0∼N(0,I),a1∼pdata(a∣o).
Along the straight interpolation
aτ=τa1+(1−τ)a0,τ∈[0,1],
the target velocity is
dτdaτ=a1−a0.
We train a conditional vector field with
LFM(θ)=E[∥vθ(o,aτ,τ)−(a1−a0)∥22].
At deployment, we sample a0∼N(0,I) and integrate
dτdaτ=vθ(o,aτ,τ).
Repeated updates transform noise into a sample from a multimodal action distribution. Diffusion policies use a related sequence of noisy actions, but learn a reverse denoising process instead of deterministic ODE transport.
Action chunking
Rather than predict one action, a policy can predict a coherent chunk:
at:t+K∼πθ(at:t+K∣ot).
Chunks capture correlations between adjacent controls and reduce the number of high-level policy queries. The cost is an open-loop interval. Long chunks are smooth but slow to react to disturbances. Receding-horizon control predicts a long chunk, executes only its first few actions, and then replans.
Coverage and Recovery
Perfect demonstrations show what success looks like, but may never show how to recover. A robust dataset should include both ideal states
(s,a∗(s))
and plausible deviation states
(s~,a∗(s~)).
Dataset
Strength
Weakness
Narrow, high-quality demonstrations
Consistent successful behavior
Little state and recovery coverage
Broad, imperfect behavior
Diverse situations and corrections
Contains suboptimal actions
Several techniques try to obtain both quality and coverage.
Data augmentation
An observation transformation T may synthetically create a deviation state. But control labels must usually change with the observation:
o~=T(o),a~=Ta(a,o).
The augmented objective is
Laug(θ)=−E(o,a)∼DET∼T[logπθ(Ta(a,o)∣T(o))].
A side-facing driving camera can approximate lateral displacement, provided its steering label is corrected accordingly. A visually plausible augmentation is harmful when it changes the correct physical action without changing the label.
Pre-training and post-training
Broad data can first teach representations, environment regularities, and recovery behaviors:
θpre=argθminLbroad(θ).
We then fine-tune from θpre on high-quality demonstrations:
θpost=argθminLexpert(θ;θpre).
Pre-training supplies coverage; post-training specifies the behavior we want. Domain mismatch and undesirable behavior in the broad dataset remain important risks.
Goal-Conditioned Imitation
When the same state supports different tasks, the goal must be part of the policy input:
πθ(a∣s,g).
Otherwise, actions for different goals appear as conflicting labels.
Given a successful trajectory
τ(i)=(s1(i),a1(i),…,sTi(i)),
we can treat its terminal state as the achieved goal g(i)=sTi(i). Every earlier transition becomes a goal-conditioned example:
The recognition model explains how the demonstration reached the goal, while the proposal model learns to choose a compatible plan without seeing the future.
Hindsight relabeling
A rollout that fails at its requested goal may still succeed at another. If
τ=(s1,a1,…,sT)
reaches sT, relabel it with g′=sT and train on
logπθ(at∣st,g′).
The trajectory becomes a demonstration for what it actually achieved. Repeating rollout, relabeling, and supervised learning turns the agent's own experience into an expanding goal-conditioned dataset.
Choosing the Right Method
Imitation-learning failures are easier to fix once we separate their causes:
Symptom
Likely cause
Useful response
High demonstration loss
Insufficient capacity or optimization
Improve the model, training, memory, or action distribution
Low training loss but deployment drift
Distributional shift
Add recovery data, augmentation, interventions, or DAgger
Averaged or indecisive actions
Multimodal expert behavior
Use autoregressive, latent-variable, flow, or diffusion policies
Correct action depends on earlier context
Partial observability
Use a recurrent or transformer policy
Jittery one-step control
Missing temporal structure
Predict action chunks or add sequence structure
Poor transfer to new tasks
Narrow state or goal coverage
Use broader pre-training and multi-task data
Plain behavioral cloning is a strong baseline when demonstrations densely cover deployment states, the horizon is short, the system naturally recovers, the action distribution is nearly unimodal, and the current observation contains enough information.
Interactive imitation becomes valuable when learner errors create qualitatively new states and an expert can safely label or correct them. Expressive sequence and generative models solve different problems: memory addresses hidden state, while multimodal distributions address multiple valid actions. Neither one, by itself, removes policy-induced distribution shift.
DAgger follows the learner's state distribution, recovery data broadens it, memory restores missing context, and generative policies preserve the expert's distinct behavior modes.