The Generative Adversarial Imitation Learning (GAIL) uses expert trajectories to recover a cost function and then learn a policy.

Learning a cost function from expert demonstrations is called Inverse Reinforcement Learning (IRL). The connection between GAIL and Generative Adversarial Networks (GANs) is that it uses a discriminator that tries to seperate expert trajectory from trajectories of the learned policy, which has the role of the generator here.


GAIL requires OpenMPI. If OpenMPI isn’t enabled, then GAIL isn’t imported into the stable_baselines module.



Images are not yet handled properly by the current implementation

If you want to train an imitation learning agent

Step 1: Generate expert data

You can either train a RL algorithm in a classic setting, use another controller (e.g. a PID controller) or human demonstrations.

We recommend you to take a look at pre-training section or directly look at stable_baselines/gail/dataset/ folder to learn more about the expected format for the dataset.

Here is an example of training a Soft Actor-Critic model to generate expert trajectories for GAIL:

from stable_baselines import SAC
from stable_baselines.gail import generate_expert_traj

# Generate expert trajectories (train expert)
model = SAC('MlpPolicy', 'Pendulum-v0', verbose=1)
# Train for 60000 timesteps and record 10 trajectories
# all the data will be saved in 'expert_pendulum.npz' file
generate_expert_traj(model, 'expert_pendulum', n_timesteps=60000, n_episodes=10)

Step 2: Run GAIL

In case you want to run Behavior Cloning (BC)

Use the .pretrain() method (cf guide).


Thanks to the open source:

  • @openai/imitation
  • @carpedm20/deep-rl-tensorflow

Can I use?

  • Recurrent policies: ❌
  • Multi processing: ✔️ (using MPI)
  • Gym spaces:
Space Action Observation
Discrete ✔️ ✔️
Box ✔️ ✔️
MultiDiscrete ✔️
MultiBinary ✔️


import gym

from stable_baselines import GAIL, SAC
from stable_baselines.gail import ExpertDataset, generate_expert_traj

# Generate expert trajectories (train expert)
model = SAC('MlpPolicy', 'Pendulum-v0', verbose=1)
generate_expert_traj(model, 'expert_pendulum', n_timesteps=100, n_episodes=10)

# Load the expert dataset
dataset = ExpertDataset(expert_path='expert_pendulum.npz', traj_limitation=10, verbose=1)

model = GAIL('MlpPolicy', 'Pendulum-v0', dataset, verbose=1)
# Note: in practice, you need to train for 1M steps to have a working policy

del model # remove to demonstrate saving and loading

model = GAIL.load("gail_pendulum")

env = gym.make('Pendulum-v0')
obs = env.reset()
while True:
  action, _states = model.predict(obs)
  obs, rewards, dones, info = env.step(action)


class stable_baselines.gail.GAIL(policy, env, expert_dataset=None, hidden_size_adversary=100, adversary_entcoeff=0.001, g_step=3, d_step=1, d_stepsize=0.0003, verbose=0, _init_setup_model=True, **kwargs)[source]

Generative Adversarial Imitation Learning (GAIL)


Images are not yet handled properly by the current implementation

  • policy – (ActorCriticPolicy or str) The policy model to use (MlpPolicy, CnnPolicy, CnnLstmPolicy, …)
  • env – (Gym environment or str) The environment to learn from (if registered in Gym, can be str)
  • expert_dataset – (ExpertDataset) the dataset manager
  • gamma – (float) the discount value
  • timesteps_per_batch – (int) the number of timesteps to run per batch (horizon)
  • max_kl – (float) the Kullback-Leibler loss threshold
  • cg_iters – (int) the number of iterations for the conjugate gradient calculation
  • lam – (float) GAE factor
  • entcoeff – (float) the weight for the entropy loss
  • cg_damping – (float) the compute gradient dampening factor
  • vf_stepsize – (float) the value function stepsize
  • vf_iters – (int) the value function’s number iterations for learning
  • hidden_size – ([int]) the hidden dimension for the MLP
  • g_step – (int) number of steps to train policy in each epoch
  • d_step – (int) number of steps to train discriminator in each epoch
  • d_stepsize – (float) the reward giver stepsize
  • verbose – (int) the verbosity level: 0 none, 1 training information, 2 tensorflow debug
  • _init_setup_model – (bool) Whether or not to build the network at the creation of the instance
  • full_tensorboard_log – (bool) enable additional logging when using tensorboard WARNING: this logging can take a lot of space quickly
action_probability(observation, state=None, mask=None, actions=None, logp=False)

If actions is None, then get the model’s action probability distribution from a given observation.

Depending on the action space the output is:
  • Discrete: probability for each possible action
  • Box: mean and standard deviation of the action output

However if actions is not None, this function will return the probability that the given actions are taken with the given parameters (observation, state, …) on this model. For discrete action spaces, it returns the probability mass; for continuous action spaces, the probability density. This is since the probability mass will always be zero in continuous spaces, see for a good explanation

  • observation – (np.ndarray) the input observation
  • state – (np.ndarray) The last states (can be None, used in recurrent policies)
  • mask – (np.ndarray) The last masks (can be None, used in recurrent policies)
  • actions – (np.ndarray) (OPTIONAL) For calculating the likelihood that the given actions are chosen by the model for each of the given parameters. Must have the same number of actions and observations. (set to None to return the complete action probability distribution)
  • logp – (bool) (OPTIONAL) When specified with actions, returns probability in log-space. This has no effect if actions is None.

(np.ndarray) the model’s (log) action probability


returns the current environment (can be None if not defined)

Returns:(Gym Environment) The current environment

Get tensorflow Variables of model’s parameters

This includes all variables necessary for continuing training (saving / loading).

Returns:(list) List of tensorflow Variables

Get current model parameters as dictionary of variable name -> ndarray.

Returns:(OrderedDict) Dictionary of variable name -> ndarray of model’s parameters.
get_vec_normalize_env() → Optional[stable_baselines.common.vec_env.vec_normalize.VecNormalize]

Return the VecNormalize wrapper of the training env if it exists.

Returns:Optional[VecNormalize] The VecNormalize env.
learn(total_timesteps, callback=None, log_interval=100, tb_log_name='GAIL', reset_num_timesteps=True)[source]

Return a trained model.

  • total_timesteps – (int) The total number of samples to train on
  • callback – (Union[callable, [callable], BaseCallback]) function called at every steps with state of the algorithm. It takes the local and global variables. If it returns False, training is aborted. When the callback inherits from BaseCallback, you will have access to additional stages of the training (training start/end), please read the documentation for more details.
  • log_interval – (int) The number of timesteps before logging.
  • tb_log_name – (str) the name of the run for tensorboard log
  • reset_num_timesteps – (bool) whether or not to reset the current timestep number (used in logging)

(BaseRLModel) the trained model

classmethod load(load_path, env=None, custom_objects=None, **kwargs)

Load the model from file

  • load_path – (str or file-like) the saved parameter location
  • env – (Gym Environment) the new environment to run the loaded model on (can be None if you only need prediction from a trained model)
  • custom_objects – (dict) Dictionary of objects to replace upon loading. If a variable is present in this dictionary as a key, it will not be deserialized and the corresponding item will be used instead. Similar to custom_objects in keras.models.load_model. Useful when you have an object in file that can not be deserialized.
  • kwargs – extra arguments to change the model when loading
load_parameters(load_path_or_dict, exact_match=True)

Load model parameters from a file or a dictionary

Dictionary keys should be tensorflow variable names, which can be obtained with get_parameters function. If exact_match is True, dictionary should contain keys for all model’s parameters, otherwise RunTimeError is raised. If False, only variables included in the dictionary will be updated.

This does not load agent’s hyper-parameters.


This function does not update trainer/optimizer variables (e.g. momentum). As such training after using this function may lead to less-than-optimal results.

  • load_path_or_dict – (str or file-like or dict) Save parameter location or dict of parameters as -> ndarrays to be loaded.
  • exact_match – (bool) If True, expects load dictionary to contain keys for all variables in the model. If False, loads parameters only for variables mentioned in the dictionary. Defaults to True.
predict(observation, state=None, mask=None, deterministic=False)

Get the model’s action from an observation

  • observation – (np.ndarray) the input observation
  • state – (np.ndarray) The last states (can be None, used in recurrent policies)
  • mask – (np.ndarray) The last masks (can be None, used in recurrent policies)
  • deterministic – (bool) Whether or not to return deterministic actions.

(np.ndarray, np.ndarray) the model’s action and the next state (used in recurrent policies)

pretrain(dataset, n_epochs=10, learning_rate=0.0001, adam_epsilon=1e-08, val_interval=None)

Pretrain a model using behavior cloning: supervised learning given an expert dataset.

NOTE: only Box and Discrete spaces are supported for now.

  • dataset – (ExpertDataset) Dataset manager
  • n_epochs – (int) Number of iterations on the training set
  • learning_rate – (float) Learning rate
  • adam_epsilon – (float) the epsilon value for the adam optimizer
  • val_interval – (int) Report training and validation losses every n epochs. By default, every 10th of the maximum number of epochs.

(BaseRLModel) the pretrained model

save(save_path, cloudpickle=False)

Save the current parameters to file

  • save_path – (str or file-like) The save location
  • cloudpickle – (bool) Use older cloudpickle format instead of zip-archives.

Checks the validity of the environment, and if it is coherent, set it as the current environment.

Parameters:env – (Gym Environment) The environment for learning a policy
set_random_seed(seed: Optional[int]) → None
Parameters:seed – (Optional[int]) Seed for the pseudo-random generators. If None, do not change the seeds.

Create all the functions and tensorflow graphs necessary to train the model