Search

Search papers and lessons

Behavior Cloning

Level 3 · 10 min read

Behavior cloning teaches a policy to copy an expert by treating each observation and expert action as a labelled example for ordinary supervised learning.

On this page

  • Colour = topic mix
  • Linear algebra
  • Probability
  • Neural network basics
  • Learning basics
  • Losses & regularization
  • Reinforcement learning
  • Sign in to see your progress

TL;DR

Behavior cloning learns a policy by copying demonstrations. You record an expert doing the task, pairing each observation the expert saw with the action it took. You then train a model with ordinary supervised learning to predict that action from that observation. No reward function is needed.

It is fast and simple, but the learned policy drifts into situations the expert never demonstrated, and small mistakes compound there.

Where you'll see it: teaching robot arms to grasp and pour from human teleoperation data, and as the initial stage of training large robot-control models before reinforcement learning or other fine-tuning.

Intuition

Learning to drive by watching

Think of a new driver sitting beside an instructor. Each time the instructor turns the wheel, the student notes what the road looked like and which way the wheel went. After enough hours, the student has a rough map from "what I see" to "what to do". That map is the policy. Behavior cloning builds the same map from recorded data instead of from a human teacher in the seat.

The appeal is that no one has to write a reward function. Designing a reward that makes a robot pour water without spilling is hard. Recording a person doing it is easy.

Where it breaks

The student has only ever seen the instructor's good driving. If the car drifts toward the kerb, the student has never seen how to recover, so it turns the wrong way, drifts further, and ends up in a state with no training examples at all. Each small error takes the car somewhere the training data never covered, and the next error is worse. Errors compound.

This is the central tension of the topic: the training data comes from the expert's distribution of states, but the learner's own actions decide which states it visits. The mismatch between training inputs and deployment inputs is the source of most failures.

flowchart LR
  A[Expert demonstrations] --> B[Pairs of observation and action]
  B --> C[Supervised training]
  C --> D[Learned policy]
  D --> E[Visits states the expert never showed]
  E --> F[Errors compound]
  F --> E

The math

Setup

Let s∈Ss \in \mathcal{S} be an observation (or state) and a∈Aa \in \mathcal{A} an action. An expert policy π⋆\pi^\star produces a distribution over actions. The demonstration dataset is

D={(si,ai)}i=1N,ai∼π⋆(⋅∣si),\mathcal{D} = \{(s_i, a_i)\}_{i=1}^{N}, \qquad a_i \sim \pi^\star(\cdot \mid s_i),

where NN is the number of recorded pairs. A learned policy πθ(a∣s)\pi_\theta(a \mid s) is a model with parameters θ\theta, for example a feedforward neural network that outputs a distribution over actions.

The objective

Behavior cloning is maximum-likelihood fitting of πθ\pi_\theta to the demonstrations:

θ⋆=arg⁡max⁡θ∑i=1Nlog⁡πθ(ai∣si).\theta^\star = \arg\max_\theta \sum_{i=1}^{N} \log \pi_\theta(a_i \mid s_i).

Equivalently, minimise the negative log-likelihood. For discrete actions with a Softmax output this is the cross-entropy loss. For continuous actions with a Gaussian output of fixed variance, it reduces to the squared error between predicted and demonstrated actions.

Derivation for the Gaussian case. Let πθ(a∣s)=N(a;μθ(s),σ2)\pi_\theta(a \mid s) = \mathcal{N}(a; \mu_\theta(s), \sigma^2). Then

log⁡πθ(a∣s)=−(a−μθ(s))22σ2−12log⁡(2πσ2).\log \pi_\theta(a \mid s) = -\frac{(a - \mu_\theta(s))^2}{2\sigma^2} - \tfrac{1}{2}\log(2\pi\sigma^2).

With σ\sigma fixed, maximising the sum is the same as minimising ∑i(ai−μθ(si))2\sum_i (a_i - \mu_\theta(s_i))^2. That is ordinary least squares on the demonstrations, with a neural network as the function μθ\mu_\theta.

What the learner is trying to match

If the expert is deterministic, a=π⋆(s)a = \pi^\star(s), the objective pushes πθ(s)\pi_\theta(s) toward π⋆(s)\pi^\star(s) on states that appear in D\mathcal{D}. Nothing in the objective says what to do on states not in D\mathcal{D}. The network's behaviour there is set by generalisation, which is not guaranteed to be sensible.

Why the error grows with the horizon

Suppose the learned policy makes a mistake with probability ϵ\epsilon at each step, and that a mistake puts the agent in a state where it may make further errors. A simple analysis in Ross et al. (2011) shows that the expected cost of plain behavior cloning can grow with the square of the horizon TT, roughly like ϵT2\epsilon T^2, rather than linearly like ϵT\epsilon T. The intuition is that an error early in a trajectory changes every state after it. Ross et al. show this quadratic growth is tight: there are problems where it really occurs. They propose a fix described below.

Worked examples

Example
Cloning a deterministic lane keeper

A car has a single steering observation ss, the lateral offset from the lane centre in metres. The expert steers a=−0.5 sa = -0.5\,s (a proportional correction). The dataset has three pairs: (s,a)=(−1,0.5)(s, a) = (-1, 0.5), (0,0)(0, 0), (2,−1)(2, -1). We fit a linear policy π(s)=ws\pi(s) = w s by least squares.

  1. Minimise ∑i(ai−wsi)2=(0.5+w)2+(0−0)2+(−1−2w)2\sum_i (a_i - w s_i)^2 = (0.5 + w)^2 + (0 - 0)^2 + (-1 - 2w)^2.
  2. Differentiate with respect to ww: 2(0.5+w)+2(−1−2w)(−2)=02(0.5 + w) + 2(-1 - 2w)(-2) = 0.
  3. Expand: 1+2w+4+8w=01 + 2w + 4 + 8w = 0, so 10w=−510w = -5.
  4. Solve: w=−0.5w = -0.5.
  5. The fitted policy is π(s)=−0.5 s\pi(s) = -0.5\,s, exactly the expert.

Interpretation: with noiseless demonstrations from a linear expert, behavior cloning recovers the expert on the states that were demonstrated. The next example shows what happens off that set.

Example
Drift off the demonstrated states

Use the same expert and fitted policy π(s)=−0.5 s\pi(s) = -0.5\,s, but now the learner starts at offset s0=3s_0 = 3, which the demonstrations never covered. Assume the car moves by st+1=st+at+0.2s_{t+1} = s_t + a_t + 0.2 per step, where the 0.20.2 is an unmodelled drift to the right.

  1. Step 0: s0=3s_0 = 3, action a0=−1.5a_0 = -1.5, next offset s1=3−1.5+0.2=1.7s_1 = 3 - 1.5 + 0.2 = 1.7.
  2. Step 1: a1=−0.85a_1 = -0.85, s2=1.7−0.85+0.2=1.05s_2 = 1.7 - 0.85 + 0.2 = 1.05.
  3. Step 2: a2=−0.525a_2 = -0.525, s3=1.05−0.525+0.2=0.725s_3 = 1.05 - 0.525 + 0.2 = 0.725.
  4. The offset shrinks each step and settles near 0.2/1.5≈0.130.2/1.5 \approx 0.13 (where s=−0.5s+0.2s = -0.5s + 0.2 balances), because the policy's correction is stronger than the drift.

Change the policy to a slightly worse fit, π(s)=−0.2 s\pi(s) = -0.2\,s. Repeat from s0=3s_0 = 3:

  1. Step 0: a0=−0.6a_0 = -0.6, s1=3−0.6+0.2=2.6s_1 = 3 - 0.6 + 0.2 = 2.6.
  2. Step 1: a1=−0.52a_1 = -0.52, s2=2.6−0.52+0.2=2.28s_2 = 2.6 - 0.52 + 0.2 = 2.28.
  3. Step 2: a2=−0.456a_2 = -0.456, s3=2.28−0.456+0.2=2.024s_3 = 2.28 - 0.456 + 0.2 = 2.024.

Interpretation: the weaker correction converges to a fixed point near s=0.2/0.2=1s = 0.2/0.2 = 1 rather than to zero, because it never balances the drift. This is a small but real version of compounding error: a policy that is nearly right on the demonstrations can settle into a bad state off them, and the demonstrations give no signal about that.

Example
A realistic setting: robot grasping from teleoperation

A robot arm observes a camera image and its joint angles, and outputs a 7-dimensional joint-velocity command. A human teleoperates it through 200 successful grasps, giving roughly N=200×TN = 200 \times T pairs, where TT is the number of control steps per grasp. A policy network trained with a squared loss will learn the average demonstrated motion for each visual situation.

Where the demonstrations contain several valid ways to grasp (approach from above, or from the side), the average of those motions is a motion that grasps nothing. This is a failure of averaging, not of the optimiser. Models that represent several modes, such as mixture density networks or diffusion-based policies, are used to avoid it.

Interpretation: behavior cloning works well when the demonstrations are consistent and the task is close to the demonstrated distribution. Multi-modal data and new situations are where the simple objective breaks down.

Tradeoffs and failure modes

SituationWhat goes wrongBetter choice
Plenty of expert data, task close to demonstrationsWorks well and is cheap to trainBehavior cloning
Long horizon, errors compoundDrift into unseen states, growing errorInteractive imitation (DAgger), or fine-tune with reinforcement learning
Demonstrations contain several valid strategiesAveraging yields an invalid actionMixture density output, or a diffusion or flow policy
No way to query the expert during trainingCannot collect recovery dataAdd noise to the expert during collection so the data covers recoveries
Reward is easy to write down and the policy must beat the expertCloning caps performance at roughly expert levelStart from cloning, then [[reinforcement-learning
Expert uses information the robot cannot observeThe policy learns an average over hidden causesInclude the information in the observation, or model latent variables

Two practical points. First, the choice of loss matters more than it looks: a squared loss on a multi-modal action target gives the averaging failure above, and a cross-entropy loss over discretised actions does not. Second, behavior cloning is often used as a warm start. A policy trained by cloning begins near the expert, which makes later reinforcement learning far less likely to get stuck.

History

Dean Pomerleau's ALVINN system (late 1980s) is one of the first well-known uses of learning from human driving. A small neural network mapped camera images to steering commands, trained on recorded human driving, and steered a test vehicle. It is often cited as an early demonstration of behavior cloning for driving.

Stéphane Ross, Geoffrey Gordon and Drew Bagnell (2011) analysed why cloning errors compound and proposed DAgger, which repeatedly runs the learned policy, asks the expert to label the states it reaches, and adds those labels to the training set. The paper reduces imitation to no-regret online learning and shows DAgger avoids the quadratic growth in the horizon.

The Diffusion Policy paper (Chi et al., 2023) uses a diffusion model over action sequences, which can represent the multiple modes of demonstrations that squared-loss cloning averages away.

Check yourself

QuestionIn one sentence, what is the supervised learning problem that behavior cloning creates from a set of demonstrations?Show answer

Learn a function from observations ss to actions aa, using the demonstrated pairs (si,ai)(s_i, a_i) as labelled examples, by maximising the likelihood of the demonstrated actions. Behavior cloning is supervised learning with the expert's actions as labels.

QuestionA deterministic expert uses a=2sa = 2s. A learner fits π(s)=ws\pi(s) = w s to demonstrations at s=1s = 1 and s=2s = 2. What is the least-squares estimate of ww?Show answer

Minimise (2−w)2+(4−2w)2(2 - w)^2 + (4 - 2w)^2. Differentiating gives −2(2−w)−4(4−2w)=0-2(2 - w) - 4(4 - 2w) = 0, so −4+2w−16+8w=0-4 + 2w - 16 + 8w = 0, giving 10w=2010w = 20 and w=2w = 2. The learner recovers the expert exactly, because the expert is linear and the data is noiseless.

QuestionWhy does a behavior-cloned policy with a small per-step error rate still fail badly over a long trajectory?Show answer

Each error moves the agent to a state the demonstrations did not cover, so the next action is less reliable, and the next error makes things worse. Errors compound along the trajectory rather than staying independent, so the total cost grows faster than linearly in the horizon.

QuestionA demonstration set contains two valid ways to reach a cup: from the left and from the right. A learner with a squared loss outputs an action halfway between them. Explain why this fails and name one model class that avoids it.Show answer

The average of the two valid actions points at neither side. The squared loss rewards predicting the mean of the demonstrated actions, and that mean is not a valid motion. A mixture density network or a diffusion policy can represent both modes and sample one of them, so the learned action is a real strategy.

QuestionApplied: you have 500 teleoperated grasps and a policy that works in the lab but fails when the cup is moved to a new position. Which two changes would you consider first, and why?Show answer

First, collect demonstrations that cover the new positions, because the failure is a shift in the states the policy sees and the policy has no data there. Second, consider an interactive method such as DAgger, which asks the expert to label the states the current policy actually reaches. Both target the cause, which is that the policy has never been trained on the states where it now fails. Changing the network size alone would not address the coverage gap.

Sources