A cloned driver fails two ways. One slip takes it where no demo went. Two good demos, one left of a rock and one right, average into a drive straight at the rock.
Learn the repairs from lecture 6 of CMU 16-831: DAgger, history, privileged teachers, GAIL, action chunks and Diffusion Policy.
Pick a method and watch 50 cars try the forked road. Cloning drifts off. DAgger recovers but aims at the rock. DAgger + diffusion recovers and picks a side. Raise the slip chance and see what breaks. Then we build each repair piece by piece.
You need behavior cloning from lecture 5, a little Python and what a probability is. We build the rest from zero.
Re-taught from Lecture 6, Imitation Learning Part 2, of CMU 16-831 Introduction to Robot Learning (Spring 2024), taught by Guanya Shi. The toys and numbers here are our own and illustrative.
Illustrative toy. Each car takes 240 steps. A slip turns it by 0.05 rad. Cloning steers only inside the demo band and holds its heading outside it. All policies trained with squared error aim at the mean of the two forks. The diffusion policy samples one fork per car.
Chapter 0
See the two diseases of behavior cloning side by side: drift and averaging
You recorded an expert driving a road with a rock in the middle. Some runs pass left of the rock. Some pass right. You trained a network to copy the expert's action at each state. This is behavior cloning (BC), and lecture 5 built it.
The clone fails in two ways. The first is drift. The clone slips once and lands in a state the expert never visited. It has no example there, so its next action is a guess. The training states came from the expert; the test states come from the clone. That mismatch is distribution shift.
The second is averaging. At the rock, half the demos say "swerve left" and half say "swerve right". A network trained with squared error outputs the mean of its labels. The mean of left and right is straight ahead, into the rock.
Left: blue is where demos went, teal is where 30 clones went. Right: the demo actions at the rock and the clone's one answer. Raise the slip chance.
Illustrative toy, the same road as the hero. The demo band is 0.35 m either side of the centre line.
The two diseases need different cures. More demos do not fix either one. Drift needs data from the states the clone visits. Averaging needs a policy that can hold two answers at once.
Chapter 1
Let the learner choose the states and the expert choose the labels, and the cost grows with T instead of T squared
Lecture 5 ended with a fix for drift. DAgger (Dataset Aggregation, Ross, Gordon and Bagnell, 2011) collects data where the learner goes:
The learner picks the states. The expert picks the labels. After a few rounds, D holds expert actions for the learner's own mistakes. The imitation learning lesson runs these rounds step by step.
How much does it buy? Call the episode length T steps. Call the policy's error rate ε. Call the most one wrong action can add to the future cost u. For plain cloning, lecture 5 gave a worst case of T² ε extra cost. DAgger's guarantee is this:
J of pi-hat is at most J of pi-star plus u times T times epsilon.
At each of the T steps the policy errs with chance epsilon, and each error costs at most u. DAgger's data covers the states after an error, so one error does not spoil the steps after it. The costs add up, so they grow with T.
With made-up numbers: epsilon 0.005, T = 100, u = 2. DAgger's ceiling is 2 × 100 × 0.005 = 1 extra cost. Plain cloning's ceiling is 100² × 0.005 = 50.
Grey is cloning's ceiling, T² ε. Purple is DAgger's, u T ε. Drag the horizon and the error rate.
The two bounds are Theorem 2.1 and Theorem 3.2 of Ross, Gordon and Bagnell (2011), with u = 2. Both are ceilings, capped at T because the cost per step is at most 1.
The failure. DAgger needs an expert you can ask about any state. The symptom: labels arrive slowly or disagree in odd states. To measure it, give two experts the same states and count agreement. The fix: an expert that is a program, which is chapter 4.
Chapter 2
Feed the policy its recent observations so it keeps the choice it already made
A car reaches a fork. The camera frame looks the same whether the driver planned left or right. A policy that sees only the current frame gives the same answer distribution every time it sees that frame. If it says "left 50%, right 50%", it can pick left now and right one frame later. The car zigzags into the divider.
A human driver does not work like that. The driver remembers "I chose left two frames ago" and keeps going left. Human demos depend on what happened before. So give the policy a window of past observations.
Call the camera frame at step t ot (the observation). Call the action at step t at. Call the network with weights θ πθ. A history policy picks the action from all frames so far:
a-t is drawn from pi-theta of a-t given o-one through o-t.
Show the network every frame o-one to o-t. It returns a probability for each action. Sample a-t from it. The same frame can now get different answers, because the frames before it differ.
With made-up numbers: at the fork, a one-frame policy says left with 0.5 on each of 10 frames. It picks the same side all 10 times with chance 2 × 0.510 ≈ 0.002. A history policy that chose left once says left with 0.97, so it stays left for 9 more frames with chance 0.979 ≈ 0.76.
The road splits. Each frame the policy picks a side. Choose the input, then press Drive.
Illustrative toy. 12 frames at the fork. The one-frame policy says left with 0.5 per frame. The history policy keeps its last choice with 0.97.
How do you build πθ over a window? A fully connected layer fails: the number of frames varies, and the weights grow with every frame. The lecture's answer is the time-series models from lecture 4: recurrent networks and transformers.
The failure. A long window lets the policy copy a shortcut: "keep doing the last action". The symptom is a policy that ignores new events, such as a brake light. To measure it, shuffle the current frame and see if the action changes. The fix is a shorter window or dropout on past frames.
Chapter 3
Prove that squared error picks the mean, and see why the mean of two good swerves is a crash
Four demos reach the rock. Two swerve left, by 1.0 m and 0.9 m. Two swerve right, by 1.1 m and 0.8 m. All four are good.
Behaviour with two or more good answers is multimodal, and each answer is a mode. The lecture lists three common sources: human demonstrators, optimal control and search algorithms. Each can break a tie either way.
Now train a network that outputs one number, with squared error. Call the demo swerves a1 to aN. Call the network's single answer â (a-hat). Training looks for the â with the smallest average squared gap:
a-hat equals the argmin over a of one over N times the sum over i of a-i minus a, squared, and that equals one over N times the sum of the a-i.
Try each candidate a. Square its gap to every demo a-i and average. The a with the smallest average is a-hat. Setting the slope to zero, 2 times the sum of (a minus a-i) equals 0, gives the mean of the a-i.
With the demos above: a-hat = (−1.0 − 0.9 + 1.1 + 0.8) / 4 = 0.0 m, straight at the rock. Its loss is 0.915. Swerving left by 0.95 m costs 1.82. The loss prefers the crash.
Blue dots are demo swerves. Purple is the policy's answer. Slide the share of demos that go left, and switch the policy type.
Illustrative toy. 40 demo swerves near −1 m and +1 m. The rock spans −0.5 m to +0.5 m.
The trap is worst when the two modes hold equal shares. At 90% left, the mean is −0.8 m and clears the rock. At 50%, the mean sits on it. A bigger network does not help: a perfect fit to squared error is the mean.
The fix is a policy that outputs a distribution with more than one peak. The lecture lists four: a Gaussian mixture with a fixed number of peaks, a conditional VAE, a diffusion model and a GAN. Chapters 5 to 8 cover the last three.
The failure, measured. Symptom: the robot hesitates or hits the middle of an obstacle. Measure: plot the demo actions at one state; two clumps mean trouble. Fix: a multimodal policy.
Chapter 4
Train a teacher that sees the true state, then train a student that sees only the sensors to copy it
A legged robot walks on ice. Its camera and joint sensors do not say "friction 0.1". Learning to walk straight from those sensors is hard. In simulation, though, you know the friction, the terrain and every push.
So split the job in two stages. First train a privileged teacher: a policy that sees the true state, with information the real robot will never have. Then train a student that sees only the robot's sensors, to copy the teacher.
| System | The teacher sees | The student sees | Teacher trained by |
|---|---|---|---|
| Driving (CARLA) | traffic lights, other cars' positions and speeds | camera images | copying an expert |
| Legged robot (sim) | terrain, friction, contacts, pushes | joint angles and IMU history | RL (PPO) |
| Drone | the full state | onboard sensing | it is an MPC controller |
Why is this easier than copying a person? The teacher is a program you control. You can ask it about any state, so DAgger costs nothing. It gives as many labels as you want, and you can augment the data freely. The student only learns perception; the teacher already solved control.
Call the teacher πβ and the true state at step t st. Call the student πθ and its sensor history ht. The student's loss over N steps is:
L of theta equals one over N times the sum over t of the squared distance between pi-theta of h-t and pi-beta of s-t.
At each step, ask the teacher pi-beta for its action from s-t. Ask the student pi-theta for its action from h-t. Square the gap, average over the N steps, and lower L with gradient descent.
With made-up numbers: the teacher says 0.8, −0.2, 0.5; the student says 0.6, −0.2, 0.9. The squared gaps are 0.04, 0 and 0.16. L = 0.20 / 3 ≈ 0.067.
At step 20 a side wind starts. Blue is the teacher, who is told the wind. Purple is the student, who guesses it from its last k positions. Change the wind and the window.
Illustrative toy. The student averages the unexplained drift over its window, with sensor noise. A short window is noisy; a long window lags.
The student need not copy actions. In RMA (Rapid Motor Adaptation), the teacher compresses the privileged information into a short vector, a latent. The student learns to predict that latent from its history. In ABS, the student predicts distance rays.
The failure. The teacher may use information the student can never infer, such as a hidden hole ahead. Symptom: the student's loss stays high in some states. Measure it per state. Fix: give the teacher only what the sensors can reveal.
Chapter 5
Train a classifier on state and action pairs, and use it to measure how far the policy is from the expert
Now take away the queryable expert. You have a fixed set of expert drives, no reward, and no one to ask. GAIL (Generative Adversarial Imitation Learning, Ho and Ermon, 2016) still learns a policy.
The idea: stop matching one action per state. Match the whole cloud of (state, action) pairs. If the policy's pairs and the expert's pairs look the same, the policy behaves like the expert over whole drives. The lecture writes one pair as z = (s, a).
How do you tell if two clouds look the same? Train a classifier to separate them. GAIL borrows this from a GAN. The classifier is the discriminator D. It reads one pair and outputs a number from 0 to 1: its belief that the pair came from the expert.
Call an expert pair (sE, aE) and a policy pair (sπ, aπ). D trains with the cross-entropy loss of a yes/no classifier:
L-D equals minus the average over expert pairs of log D of s-E, a-E, minus the average over policy pairs of log of one minus D of s-pi, a-pi.
Label expert pairs 1 and policy pairs 0. Penalise D by minus the log of the probability it gives to the right label. Average, and lower L-D with gradient descent.
With made-up numbers: D gives an expert pair 0.9 and a policy pair 0.3. L-D = −ln 0.9 − ln 0.7 = 0.105 + 0.357 = 0.46. A perfect D would score 0.
Blue dots are expert pairs, purple dots are policy pairs. Pink shading is where D says "expert". Train D, then step the policy. Repeat.
Illustrative toy. D is a logistic classifier on two features. A policy step moves each policy pair up the slope of D.
This is a cousin of inverse RL, which first recovers the expert's cost and then runs RL on it. GAIL skips the explicit cost. D plays its part.
The failure. D can win too easily. If D is near perfect, it says 0 to every policy pair and gives no direction. Symptom: D's accuracy near 100% for many rounds. Measure it each round. Fix: a smaller D, fewer D steps per round, or noise on its inputs.
Chapter 6
Turn D's score into a reward and train the policy with RL until D cannot tell the two apart
The lecture states GAIL in three steps, repeated:
Step 3 has no labels. No one says which action was right. So GAIL makes a reward from D and runs RL. A pair that D mistakes for expert pays well. Call the reward for a pair r(s, a):
r of s, a equals minus the log of one minus D of s, a.
Ask D how expert-like the pair (s, a) looks. Take one minus that, the chance it looks like the policy. The reward r is minus its log: small when D sees through the pair, large when D is fooled.
D = 0.3 gives r = −ln 0.7 = 0.36. D = 0.5 gives r = ln 2 = 0.69. D = 0.9 gives r = 2.30. When D outputs 0.5 everywhere, it cannot tell the two apart, and GAIL has converged.
Blue is the expert's pairs, purple is the policy. Pink is D, yellow is the reward. Each round refits D, then moves the policy up the reward.
Illustrative toy. Each pair is one number. D is logistic. The policy is a Gaussian whose centre takes one reward-weighted step per round, a stand-in for the TRPO or PPO step in the paper.
The policy improves by RL, not by regression. The original paper uses TRPO; policy gradients explains the family. GAIL needs no expert reward. D makes one.
Can GAIL model multimodal behaviour? Yes. The policy is a sampler, so it can put mass on "left" and on "right" as long as both fool D.
The price is a two-player game. Like a GAN, it can oscillate or drop a mode. Symptom: the policy's success swings from round to round. Measure D's output on policy pairs each round; it should settle near 0.5. Fix: smaller policy steps.
Chapter 7
Output a sequence of future actions at once, then average the overlapping predictions for a smooth motion
A robot arm picks up a cup. A policy that decides one action per step can switch modes in the middle of a grasp, from "approach left" to "approach right". The arm jitters. ACT (Action Chunking with Transformers, Zhao et al., 2023) has two answers.
First, action chunking: at each step, predict the next k actions, a chunk. One prediction commits to one plan for k steps. Second, ACT is a conditional VAE. In training, an encoder reads the expert's action sequence and the observation and makes a latent z. A decoder reads z and the observations and outputs the chunk. The latent z holds the choice of mode, so the decoder does not average modes.
If the robot predicts a chunk every step, each future step gets several predictions, one from each recent chunk. ACT blends them with a temporal ensemble. Call the predictions for step t At[i], with i = 0 the oldest. Each gets a weight wi = e−m·i, where m sets how fast the weights fall:
a-hat-t equals the sum over i of w-i times A-t of i, divided by the sum over i of w-i.
Collect every prediction A-t of i for step t. Weight the oldest by 1, the next by e to the minus m, and so on. Add the weighted predictions and divide by the total weight. A larger m trusts the oldest predictions more.
With made-up numbers: three chunks predict 0.30, 0.36 and 0.42 for step t, oldest first, and m = 0.5. The weights are 1, 0.61 and 0.37, total 1.97. a-hat-t = (0.30 + 0.218 + 0.155) / 1.97 ≈ 0.34.
Thin purple lines are chunks, one predicted per step. The thick line is what the robot does. Switch the mode, then raise the weight decay m.
Illustrative toy. Chunks of 8 steps. Each chunk is the target path plus its own small error. Jerk is the sum of the size of each jump between steps.
The failure. A chunk runs open loop. If the cup moves after a prediction, the old plan keeps going. Symptom: late reactions to pushes. Measure the delay from a push to the first change in action. Fix: shorter chunks or the ensemble, which mixes in new chunks every step.
Chapter 8
Start from random noise and remove predicted noise step by step, so each sample settles in one mode
GAIL handles modes but is hard to train. Can we get modes without the two-player game? Diffusion Policy (Chi et al., 2023) takes the diffusion model from lecture 4 and points it at actions instead of pixels.
Training uses the forward process. Take an expert action chunk. Add a little Gaussian noise, again and again, until it is pure noise. Train a network, the denoiser, to predict the noise the process added. Generation runs the reverse process: start from noise and remove the predicted noise, step by step, given the observation.
Call the action chunk after k noise steps Ak, the observation O, and the denoiser εθ. One reverse step is:
A to the k minus one equals alpha times the quantity A to the k, minus gamma times epsilon-theta of O, A to the k and k, plus sigma z.
Ask the denoiser how much noise is in A to the k. Subtract gamma times that guess. Add a little fresh noise sigma z, scale by alpha, and repeat until k reaches 0.
With made-up numbers for one action: A to the k = 0.40, the denoiser says 0.50, gamma 0.2, sigma z = 0.02, alpha 0.98. A to the k minus one = 0.98 × (0.40 − 0.10 + 0.02) ≈ 0.31.
The blue curve is the demo action distribution: left and right. Purple dots are samples. The dashed line is the squared-error answer. Press Sample.
Illustrative toy. The samples follow annealed Langevin steps on the true two-peak density, the link the lecture draws between diffusion and Langevin dynamics. A real denoiser learns that slope from data.
Why does this keep both swerves? Each sample starts from its own noise and slides into the nearest peak. Some land left, some right. None stays between, because the slope there points away. The rock lies in a valley of the density, where no sample rests.
| Generative model | Holds modes by | Catch |
|---|---|---|
| Gaussian mixture | a fixed number of peaks | you must guess the count |
| Conditional VAE (ACT) | a latent that picks the mode | needs chunking for smooth motion |
| GAN (GAIL) | an adversarial sampler | unstable training |
| Diffusion | denoising from noise | many network passes per action |
The learned distribution can sit at several levels. It can be one action given an observation, or a (state, action) pair as in GAIL. It can be a chunk as in ACT, or a whole trajectory as in Diffuser.
The failure. Too few denoising steps leave samples between the peaks. Measure the share of samples in the valley as you lower K. Fix: more steps, or a faster sampler.
Chapter 9
Keep the terms, the numbers, the code and the choice of tool on one page
Behavior cloning has two diseases. Drift needs data from the learner's own states: DAgger, or a privileged teacher you can query. Averaging needs a policy that holds several modes: GAIL, a conditional VAE such as ACT, or Diffusion Policy. History and chunks help the policy keep the mode it chose.
| Term | What it is | Number to remember |
|---|---|---|
| Drift | a slip leads to states with no demos | cloning: up to T²ε |
| DAgger | expert labels the learner's states; data only grows | u T ε: 1 against 50 at T = 100 |
| History policy | πθ(at | o1, …, ot) | 0.002 against 0.76 to hold a side |
| Averaging | squared error outputs the mean of the modes | −1, +1 → 0, into the rock |
| Privileged teacher | sees the true state; the student copies it from sensors | L = 0.067 in the worked example |
| GAIL discriminator | D(s, a): chance a pair is expert | LD = 0.46 for 0.9 and 0.3 |
| GAIL reward | −log(1 − D); trained by RL | ln 2 = 0.69 at convergence |
| ACT | CVAE that outputs chunks; temporal ensemble | wi = e−m i |
| Diffusion Policy | denoise a chunk from noise, given the observation | 0.40 → 0.31 in one step |
| You have | Reach for |
|---|---|
| an expert you can ask in any state | DAgger |
| a simulator with the true state | a privileged teacher, then a student |
| fixed demos, no expert, no reward | GAIL |
| demos with several good answers | Diffusion Policy or ACT |
| demonstrators with memory | a sequence model over a window |
pythonimport numpy as np def dagger(expert, train, rollout, demos, rounds=5): D = list(demos) # (obs, action) pairs pi = train(D) for _ in range(rounds): visited = rollout(pi) # the learner picks the states D += [(o, expert(o)) for o in visited] # the expert picks the labels pi = train(D) # retrain on everything return pi def gail_reward(D_prob): # D_prob: P(expert) per (s, a) pair return -np.log(1.0 - D_prob + 1e-8) # high when D is fooled def temporal_ensemble(preds, m=0.5): # preds for step t, oldest first w = np.exp(-m * np.arange(len(preds))) return float(np.dot(w, preds) / w.sum()) def denoise_step(A, eps_hat, alpha, gamma, sigma, rng): return alpha * (A - gamma * eps_hat + sigma * rng.standard_normal(A.shape))
Readings for this lecture: Ross, Gordon and Bagnell, DAgger (2011); Ho and Ermon, GAIL (2016); Zeng et al., Transporter Networks (2020); and Diffusion Policy. Also cited: Zhao et al., ACT (2023); Chen et al., Learning by Cheating (2019); Kumar et al., RMA (2021).
Course credit: Lecture 6 of CMU 16-831 Introduction to Robot Learning, Spring 2024, taught by Guanya Shi. The slides are on the course site. Every idea here is re-taught in our own words and toys.
Try to teach it back with Present or Teach. Then go back to the forked road and predict each method's score before you press the button.