Skip to main content
Open In Colab

Behavioral Cloning with CarRacing-v3

Behavioral cloning (BC) is the simplest form of imitation learning: collect expert demonstrations, then train a policy via supervised learning to map observations to actions. It is the starting point for understanding why imitation learning works, and why it fails. In this section you will:
  1. Train an expert policy using PPO
  2. Collect expert driving demonstrations
  3. Train a BC policy via supervised learning on the expert’s data
  4. Observe distribution shift, the core failure mode of BC
  5. Fix it with DAgger (Dataset Aggregation)
Environment: CarRacing-v3, a continuous-control driving task where the agent observes a 96×96 RGB top-down view of a procedurally generated race track and outputs steering, throttle, and brake.

Setup

Step 1: Create the environment

Step 2: Train an expert policy

We train an expert using PPO. In a real setting you might use a pre-trained checkpoint or human teleoperation. Here PPO acts as our “expert driver.” Note: Training for 200k timesteps takes ~5-10 minutes on CPU.

Reading PPO training output

PPO logs a block of metrics after each rollout-and-update iteration. With verbose=1 you would see something like this:
PPO uses a diagonal Gaussian action distribution for continuous control: the CNN outputs the mean of each action dimension, and the standard deviation (std) is a separate learned parameter (state-independent log_std). Actions are sampled from Normal(mean, std) at training time and set to mean at evaluation time when deterministic=True. There is no diffusion head, modern diffusion-based action heads (Chi et al. 2023) are an alternative used in some manipulation BC systems, but they are not required for continuous control and SB3 PPO does not use them. Each metric and the trend you should expect during a healthy run: The next cell plots these metrics over the course of training using the data captured by the callback above.
Output from cell 5

Step 3: Collect expert demonstrations

Roll out the expert to collect (observation, action) pairs, this is our training data for behavioral cloning.
Output from cell 7

Step 4: Train a behavioral cloning policy

BC is supervised learning: a CNN maps observations to actions, trained with MSE loss against the expert’s recorded actions.
Output from cell 9

Step 5: Evaluate and observe distribution shift

Deploy the BC policy and compare against the expert. What you will observe: the BC policy performs noticeably worse than the expert. On straight sections it may track the road, but on sharp turns it drifts off. Once off-track, it enters states the expert never demonstrated, predictions become unreliable, and the car spirals further off course. This is compounding error from distribution shift: at training time, the policy only saw states along the expert’s trajectory. At test time, any small deviation puts the agent in unfamiliar territory.
Output from cell 10

Step 6: Fix it with DAgger

DAgger (Dataset Aggregation) addresses distribution shift by iteratively collecting new data from the learner’s trajectory, labeled by the expert. The algorithm:
  1. Train an initial BC policy on expert demonstrations
  2. Roll out the learner’s policy in the environment
  3. Ask the expert to label the states the learner visited (what would you have done here?)
  4. Add this new data to the training set
  5. Retrain and repeat

Compare all three policies

Output from cell 12

Summary

The distribution shift problem you observed here, and DAgger’s fix of relabeling learner-visited states, reappears throughout robot learning. Later in the course, you will revisit these ideas in the context of world models and VLA architectures, where the same fundamental challenge is addressed at larger scale.

Further reading