# Module 6.3: Imitation Learning and Behavior Cloning

- Course: [Course 6: Modern Robot Learning & Embodied AI](https://roboticsconcepts.dev/courses/modern-robot-learning-embodied-ai.md)
- HTML Page: https://roboticsconcepts.dev/courses/modern-robot-learning-embodied-ai/imitation-learning-and-behavior-cloning/
- Key Concepts: Teleoperation datasets & covariate shift, Action chunking (ACT), Multimodal diffusion policies

Writing a reward function for folding a towel or pouring coffee without spilling is surprisingly hard. Often the simplest way to teach a robot a finicky skill is just to show it — guiding the arm yourself with a joystick, leader-follower rig, or VR controller, and asking a neural network to imitate your hands.

## Behavior cloning and the covariate shift trap

The most direct form of imitation learning is Behavior Cloning (BC): record pairs of (observation, expert action) during human teleoperation and train a network with supervised regression to predict the human's next move. On paper it looks just like standard image classification or curve fitting.

On a real robot, naive single-step BC runs into a nasty feedback loop called compounding covariate shift. Every physical rollout has tiny execution noise. A 2-millimeter drift puts the gripper slightly outside the clean path the human demonstrated. Because the network never saw that slightly off-center state during training, its next prediction is a bit worse — which pushes it even farther off course until the gripper drifts completely off the table.

## Fixing jitter and mode-averaging: Action Chunking & Diffusion

Two practical ideas turned imitation learning from a fragile lab demo into a workhorse for bimanual manipulation:

- Action Chunking (ACT): instead of predicting just one tiny action for the next 20 ms step, predict a short sequence (chunk) of the next 16–50 steps at once. Executing chunks enforces temporal consistency, cuts out high-frequency hesitation, and slashes the number of sequential decisions where errors can compound.
- Diffusion Policies: suppose a human demonstrator sometimes goes left around a mug and sometimes goes right. A plain MSE regression model averages the two choices and drives straight through the middle of the mug. Diffusion policies treat action generation as iterative denoising from random noise, letting the model represent multi-modal choices cleanly and commit to one valid path.

## Hands-On Lab

In Module 6.3, explore the compounding error and multimodal averaging trade-offs directly in the companion notebook — you'll step through a 2D corridor navigation demo where naive MSE averages two valid paths into an obstacle while chunked/multimodal policies pick a clean route.

## Collecting demonstrations that actually generalize

Even with action chunking and diffusion, imitation learning is only as broad as the demonstrations you feed it. If all fifty demos start with the mug in the exact same square centimeter, the policy will freeze or flail the first time someone sets the mug down crooked.

Good teleop datasets deliberately vary object starting poses, lighting, and clutter — and increasingly mix in recovery demos where the human gently bumps an object out of place and corrects it, teaching the policy what to do when a state is slightly messy.

## Quadratic compounding error vs. action chunking

```text
E[J(π_BC)] ≤ O(ε · T²)   →   chunking horizon H reduces decision steps from T to T/H
```

Single-step errors ε grow quadratically over episode length T due to covariate shift; predicting H-step chunks shrinks effective horizon and smooths motion.

## Check Your Intuition

**Question:** In half your teleoperation demos, you reach around the left side of a water bottle to grab a snack; in the other half, you reach around the right side. What happens if you train a plain MSE regression behavior-cloning policy on that dataset?

- [ ] It randomly alternates between left and right cleanly on every episode
- [✓] It averages the two trajectories and steers straight into the water bottle
- [ ] It refuses to move until you add a sparse RL reward

**Explanation:** Standard MSE regression outputs the conditional mean of the demonstrations. The mean of going left and going right goes straight down the center into the obstacle.

## Where You'll See This in Modern Robotics

Modern visuomotor policies like ACT, Diffusion Policy, and Flow-Matching VLA action heads all predict multi-step action chunks rather than single frames, typically blending overlapping chunks with temporal ensembling.

## Companion Python Notebook: 03 · Behavior Cloning, Covariate Shift & Action Chunking

Demonstrate compounding drift on single-step imitation and fix multimodal averaging & jitter with action chunking.

- Open in molab (WASM): https://molab.marimo.io/github/alchemicduncan/robotics-concepts-dev/blob/main/notebooks/course-6/03_imitation_learning_behavior_cloning.py/wasm
- Open in Google Colab: https://colab.research.google.com/github/alchemicduncan/robotics-concepts-dev/blob/main/notebooks/colab/course-6/03_imitation_learning_behavior_cloning.ipynb
- Download .py: https://roboticsconcepts.dev/downloads/notebooks/course-6/03_imitation_learning_behavior_cloning.py
- Download .ipynb: https://roboticsconcepts.dev/downloads/notebooks/colab/course-6/03_imitation_learning_behavior_cloning.ipynb
- Run locally: `uvx marimo edit --sandbox notebooks/course-6/03_imitation_learning_behavior_cloning.py`
