Skip to main content
v2026.11,858 entries · CC-BY 4.0

Imitation Learning in Python: A Research Guide

Imitation learning trains policies from expert demonstrations. Learn how to install the imitation library, execute behavioral cloning, and interpret logs.

Written and maintained by CASRAI Editorial Board

Last updated

Imitation learning (also designated as Learning from Demonstration, or LfD) is a machine learning paradigm in which an autonomous agent learns sequential decision-making policies by observing expert demonstrations rather than exploring an environment through unguided trial and error. In computational artificial intelligence, robotics, and reinforcement learning research, imitation learning resolves a pervasive operational challenge: designing dense, un-hackable reward functions for complex, multi-step tasks is often mathematically intractable or prohibitively labor-intensive. By providing an agent with state-action trajectories generated by human operators, pre-existing algorithmic controllers, or verified heuristic solvers, researchers can bypass reward engineering and bootstrap policy acquisition with high sample efficiency.

Within quantitative methodology, imitation learning serves as an indispensable tool for training high-dimensional autonomous systems, simulating complex human behaviors, and constructing data-driven policies across robotics, economics, and healthcare simulation. Researchers operating within computational labs can leverage high-performance compute resources such as the National AI Research Resource (NAIRR) to parallelize trajectory collection and accelerate policy convergence across complex environments.

What it is used for in research

Imitation learning is deployed across multiple academic disciplines to address problems where defining explicit analytical objectives is impractical, but collecting demonstrations is feasible:

  • Embodied AI and Robot Manipulation: Training robotic arms, mobile manipulators, and humanoid platforms to execute dexterous skills—such as cable routing, surgical suturing, and warehouse bin-picking—directly from teleoperated demonstrations.
  • Autonomous Vehicle Trajectory Planning: Modeling human driving behaviors and interaction dynamics in multi-agent traffic simulators to produce realistic motion profiles that avoid unrealistic reward-seeking edge cases.
  • Surrogate Policy Optimization: In engineering optimization and computational mechanics, imitation policies act alongside surrogate models to approximate computationally expensive numerical solvers or finite-element feedback loops in real time.
  • Algorithmic Behavioral Science: Recovering latent utility functions and decision rules of human participants in economic experiments or cognitive psychology tasks through inverse reinforcement learning.

Methodologically, imitation learning divides into three core mathematical approaches:

  1. Behavioral Cloning (BC): Formulates policy learning as direct supervised learning. Given a dataset of recorded demonstration transitions (s, a), where s represents the observed state and a represents the expert action, a policy network πθ is optimized to minimize the prediction error between πθ(s) and a using negative log-likelihood or mean squared error. While computationally efficient, BC suffers from covariate shift (compounding distribution drift), where minor execution errors steer the agent into states unrepresented in the training corpus.
  2. Interactive Imitation Learning (DAgger): The Dataset Aggregation (DAgger) framework resolves compounding errors by allowing the novice policy to execute actions in the environment while an expert supervisor provides corrective labels for the states newly visited by the learner. These corrective tuples are iteratively aggregated into the demonstration buffer to retrain the policy.
  3. Inverse Reinforcement Learning (IRL) and Adversarial Imitation: Rather than directly cloning actions, IRL algorithms (such as Generative Adversarial Imitation Learning [GAIL] and Adversarial Inverse Reinforcement Learning [AIRL]) recover an underlying reward function or match the state-action occupancy measure between the agent and expert. This enables robust policy generalization even when environment transition dynamics differ from the demonstration setup.

Install or access (official source only)

The standard benchmark library for imitation learning research in Python is imitation, developed and maintained by the Center for Human-Compatible AI (CHAI) at the University of California, Berkeley. The library provides modular, unit-tested implementations of BC, DAgger, GAIL, AIRL, and reward modeling built directly on top of PyTorch, Farama Gymnasium, and Stable-Baselines3.

To ensure computational reproducibility across experimentation, research teams should verify software dependencies and document execution environments using CodeMeta metadata files.

System prerequisites:

  • Python 3.8, 3.9, or 3.10
  • PyTorch 1.13 or later (CPU or CUDA-enabled GPU build)
  • Farama Gymnasium 0.29+

Install the core package from PyPI via the official terminal command:

pip install imitation

For experiments involving classic control benchmarks or continuous robotics physics engines, install the accompanying simulation wrappers and environments:

pip install "gymnasium[classic-control]" seals

To inspect or modify source code for custom algorithmic development, clone the official UC Berkeley CHAI repository and execute an editable installation:

git clone https://github.com/HumanCompatibleAI/imitation.git
cd imitation
pip install -e .

Step-by-step task

The following protocol demonstrates a complete, reproducible Python script for collecting expert demonstrations, formatting transitions, training a Behavioral Cloning policy, and evaluating task performance on a Gymnasium environment.

  1. Initialize the Vectorized Environment and Rollout Wrapper
    Configure a deterministic pseudo-random number generator and instantiate the benchmark environment using make_vec_env. Wrap the environment in RolloutInfoWrapper to record trajectory transition metadata required by the downstream sampling utilities:

    import numpy as np
    from stable_baselines3.common.evaluation import evaluate_policy
    from imitation.algorithms import bc
    from imitation.data import rollout
    from imitation.data.wrappers import RolloutInfoWrapper
    from imitation.policies.serialize import load_policy
    from imitation.util.util import make_vec_env
    
    # Configure reproducible random seeds
    rng = np.random.default_rng(42)
    
    # Build vectorized environment with metadata wrapper
    env = make_vec_env(
        "seals:seals/CartPole-v0",
        rng=rng,
        n_envs=1,
        post_wrappers=[lambda env, _: RolloutInfoWrapper(env)],
    )
  2. Load an Expert Policy and Sample Demonstration Trajectories
    Load a pre-trained expert model from the official model registry (or provide an existing policy artifact), and sample 50 complete demonstration episodes:

    # Load verified expert policy
    expert = load_policy(
        "ppo-huggingface",
        organization="HumanCompatibleAI",
        env_name="seals-CartPole-v0",
        venv=env,
    )
    
    # Collect 50 expert demonstration rollouts
    print("Sampling expert demonstration rollouts...")
    rollouts = rollout.rollout(
        expert,
        env,
        rollout.make_sample_until(min_timesteps=None, min_episodes=50),
        rng=rng,
    )
  3. Flatten Trajectories into Transition Buffers
    Imitation algorithms process individual state-action pairs rather than full episode structures. Flatten the list of TrajectoryWithRew objects into a single Transitions structure containing observation, action, and done arrays:

    # Flatten episodes into transition batches
    transitions = rollout.flatten_trajectories(rollouts)
    print(f"Extracted {len(transitions)} state-action transition pairs.")
    print(f"Data fields available: {list(transitions.__dict__.keys())}")
  4. Configure and Instantiate the Behavioral Cloning Trainer
    Pass the observation space, action space, demonstration transition buffer, and random seed into the bc.BC constructor:

    # Initialize Behavioral Cloning algorithm instance
    bc_trainer = bc.BC(
        observation_space=env.observation_space,
        action_space=env.action_space,
        demonstrations=transitions,
        rng=rng,
    )
  5. Evaluate Untrained Baseline and Execute Training
    Record the baseline performance of the un-optimized policy, train the model over the demonstration corpus, and measure final reward convergence:

    # Evaluate uninitialized network
    baseline_rew, _ = evaluate_policy(bc_trainer.policy, env, n_eval_episodes=10)
    print(f"Mean baseline reward before training: {baseline_rew:.2f}")
    
    # Train the BC policy over demonstration transitions
    print("Executing Behavioral Cloning training loop...")
    bc_trainer.train(n_epochs=1)
    
    # Evaluate trained imitation policy
    trained_rew, _ = evaluate_policy(bc_trainer.policy, env, n_eval_episodes=10)
    print(f"Mean reward after BC training: {trained_rew:.2f}")
  6. Execute Alternative CLI Workflow for Batch Experiments
    When running multi-seed parameter sweeps across institutional clusters, execute the experiment using the built-in Sacred command-line interface rather than custom Python wrappers:

    python -m imitation.scripts.train_imitation bc with 
        cartpole 
        demonstrations.n_expert_demos=50 
        bc.train_kwargs.n_batches=2000 
        expert.policy_type=ppo

Output and how to read it

During the optimization phase, imitation writes structured telemetry blocks to stdout and log files after designated batch intervals. A standard console telemetry record appears as follows:

---------------------------------
| batch_size        | 32        |
| bc/               |           |
|    batch          | 500       |
|    ent_loss       | -0.000237 |
|    entropy        | 0.237     |
|    epoch          | 0         |
|    l2_loss        | 0         |
|    l2_norm        | 93.5      |
|    loss           | 0.321     |
|    neglogp        | 0.321     |
|    prob_true_act  | 0.832     |
|    samples_so_far | 16032     |
---------------------------------

Researchers should interpret these metrics using the following diagnostic criteria:

  • batch_size: The number of transition samples processed in each stochastic gradient descent update step (default is 32).
  • bc/batch: The running index of the current optimization mini-batch within the training session.
  • bc/loss: The aggregate scalar loss being minimized by the PyTorch optimizer. In default BC, this is the sum of the negative log-probability and any active regularization penalties (such as L2 or entropy loss).
  • bc/neglogp: The negative log probability of the true expert action under the policy distribution: -log πθ(a* | s). A steady downward trajectory indicates the policy is converging on the demonstrator’s action distribution.
  • bc/prob_true_act: The mean probability assigned by the policy model to the actual expert action across the mini-batch. For a binary discrete action space, an untrained policy begins at approximately 0.50. A successful training run typically achieves values ≥0.80 to 0.95.
  • bc/entropy: The Shannon entropy of the predicted action distribution. High entropy reflects exploratory variance; low entropy reflects high deterministic commitment.
  • bc/ent_loss: The negative entropy multiplied by the configured entropy coefficient. Used when researchers explicitly penalize overconfidence.
  • bc/l2_norm: The Frobenius norm of the policy network weights. Monitored to detect gradient explosion or excessive parameter drift.
  • bc/samples_so_far: The cumulative number of demonstration state-action pairs ingested by the optimizer.
  • evaluate_policy: Returns a tuple consisting of the mean episode reward and reward standard deviation across the specified number of evaluation rollouts (e.g., 500.0 ± 0.0 on CartPole-v0). Comparing this figure against the expert’s initial score confirms whether policy convergence matches expert performance.

Troubleshooting

Researchers frequently encounter specific failure modes when scaling imitation learning algorithms from toy environments to complex benchmarks. Address common issues with the following verified solutions:

  • Covariate Shift and Catastrophic Task Failure:
    Symptom: The policy achieves high prob_true_act (>0.90) on the static demonstration dataset, but achieves near-zero reward when deployed in the live environment.
    Remedy: This indicates compounding errors where minor policy errors lead to unvisited states. Replace standard Behavioral Cloning with interactive dataset aggregation using imitation.algorithms.dagger.SimpleDAggerTrainer. Alternatively, inject synthetic noise into demonstration rollouts or collect explicit recovery demonstrations starting from perturbed initial states.
  • Missing RolloutInfoWrapper Error:
    Symptom: Executing rollout.rollout() raises ValueError: Trajectory is missing rollout info.
    Remedy: The rollout collector requires environment wrappers to inject trajectory metadata into info dictionaries. Ensure post_wrappers=[lambda env, _: RolloutInfoWrapper(env)] is passed to make_vec_env when constructing the vectorized environment.
  • Multimodal Action Averaging Collapse:
    Symptom: In continuous control tasks where demonstrators bypass an obstacle by steering either left or right, the trained agent drives straight into the obstacle.
    Remedy: Standard feedforward policies with unimodal Gaussian heads average contradictory expert actions (e.g., +1.0 and -1.0 average to 0.0). Replace the unimodal policy head with an expressively parameterized policy, such as a Gaussian Mixture Model (GMM), Energy-Based Model, or Diffusion Policy head available in specialized robotics suites.
  • API Step Signature Incompatibility:
    Symptom: ValueError: too many values to unpack (expected 4, got 5) or environment step crashes.
    Remedy: imitation operates natively on Farama Gymnasium environments returning 5-tuple step signatures (obs, reward, terminated, truncated, info). If using legacy OpenAI Gym environments, wrap them with the Farama shimmy compatibility package.
  • Out-of-Memory (OOM) on Large Trajectory Buffers:
    Symptom: Python process terminated by SIGKILL when calling rollout.flatten_trajectories() on high-resolution image observations.
    Remedy: Trajectory flattening loads all transitions into host RAM. For vision-based datasets, store demonstrations as memory-mapped HDF5 files or stream mini-batches directly using PyTorch IterableDataset loaders rather than loading uncompressed numpy arrays in memory.

Free alternatives (table)

While the imitation package is the standard benchmark suite for algorithm researchers comparing algorithmic baselines, several complementary open-source frameworks support distinct research domains, such as physical robot hardware control or offline dataset management:

Framework Developing Institution Supported Algorithms Primary Research Target License & Repository
imitation Center for Human-Compatible AI (UC Berkeley) BC, DAgger, GAIL, AIRL, SQIL, MCE-IRL Algorithmic RL benchmarking and Gymnasium environments MIT License (GitHub: HumanCompatibleAI/imitation)
robomimic Stanford University (Stanford Vision & Learning Lab) BC, BC-RNN, HBC, Diffusion Policy, IRIS Robot manipulation, teleoperation datasets, and RoboSuite benchmarks MIT License (GitHub: ARISE-Initiative/robomimic)
LeRobot Hugging Face ACT, Diffusion Policy, VQ-BeT Real-world physical robotics, low-cost arms, and dataset hub sharing Apache 2.0 (GitHub: huggingface/lerobot)
Stable-Baselines3 Farama Foundation PPO, SAC, TD3, A2C (RL algorithms) Underlying RL policy optimization and vectorized environment backbones MIT License (GitHub: DLR-RM/stable-baselines3)
rlberry Inria / Scieur et al. Model-based RL, policy search, basic demonstration loaders Theoretical reinforcement learning and educational exploration MIT License (GitHub: rlberry-py/rlberry)

FAQ

What is the difference between Behavioral Cloning and Inverse Reinforcement Learning?

Behavioral Cloning is a direct supervised learning formulation that maps environmental states to expert actions without estimating why the action was selected. In contrast, Inverse Reinforcement Learning assumes the demonstrator acts according to an unobserved reward function; IRL seeks to recover that underlying mathematical objective function, which can subsequently be used to train an optimal agent via reinforcement learning across varying environment dynamics.

Can imitation learning train policies without access to reward functions?

Yes. Pure Behavioral Cloning (BC) and interactive imitation algorithms like DAgger train entirely on observation-action pairs without requiring scalar reward values from the environment. Similarly, Generative Adversarial Imitation Learning (GAIL) substitutes environmental rewards with an adversarial discriminator score that quantifies similarity to expert trajectories.

How many demonstration trajectories are needed to achieve policy convergence?

Demonstration volume depends heavily on state-space dimensionality and task horizon. In low-dimensional classic control tasks (e.g., CartPole or Pendulum), 10 to 50 trajectories are often sufficient to match expert return. In high-dimensional vision-based robotic manipulation, standard benchmarks typically require hundreds of teleoperated demonstrations (or thousands of synthetic rollouts) combined with aggressive visual data augmentation or pretrained visual encoders to prevent overfitting.

How should researchers ensure experimental reproducibility when publishing imitation learning benchmarks?

To uphold standards in reproducibility vs replicability, researchers must report exact random seed schedules, environment step limits, demonstration dataset hashes, and hyperparameter logs. Open-source software outputs should be archived under formal software metadata schemas and submitted to scholarly software venues like the Journal of Open Source Software (JOSS).

Sources (official docs)

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Imitation Learning in Python: A Research Guide

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →