11.2 Inverse RL and GAIL
13.1 covered behavior cloning — directly imitating expert actions. BC has a fundamental limit: it never learns why the expert does what it does. This section takes a different route — inverse RL: infer a reward function from expert trajectories, then train a standard RL algorithm on that reward. This sidesteps BC's distribution shift, and it also yields a reward signal that transfers better.
Maximum-Entropy Inverse RL
Inverse RL assumes the expert is good because it is optimizing some hidden reward function. Instead of imitating actions directly, we first recover this reward function, then hand it to ordinary RL to solve.
The basic setup of inverse RL
Given expert trajectories Dexpert={τ1,…,τM}, each τ=(s0,a0,…,sT), the goal is to learn a reward function rψ(s,a) such that:
the expert policy is optimal under rψ
This condition is severely underdetermined — every constant reward rψ≡c satisfies it. We need extra regularization, or a maximum-entropy principle, to pin down rψ uniquely.
The MaxEnt IRL objective
Ziebart et al. 2008 proposed maximum-entropy inverse RL. Assume the expert policy follows a maximum-entropy distribution (it matches feature expectations while staying as random as possible otherwise):
π(a∣s)∝exp(Qrψsoft(s,a))
Then the likelihood of an expert trajectory is:
p(τ∣rψ)=Z(rψ)1exp(t∑rψ(st,at))
We maximize the log-likelihood of the expert data:
ψmaxL(ψ)=τ∈Dexpert∑[t∑rψ(st,at)]−∣Dexpert∣logZ(rψ)
The first term is the cumulative reward of the expert trajectories; the second term, logZ, is the log partition function (the log of the sum of exponentiated rewards over all possible trajectories). The gradient is:
∇ψL=Eτ∼expert[t∑∇ψrψ(st,at)]−Eτ∼p(⋅∣rψ)[t∑∇ψrψ(st,at)]
In plain terms: push up the reward on (s,a) pairs the expert visits, and push down the reward on (s,a) pairs the current policy (the policy rolled out under rψ) visits. When the two feature expectations match, the gradient is zero.
Where MaxEnt IRL gets hard
logZ(rψ) has no closed form over continuous state-action spaces. There are three main approximations:
- Model-based: use a learned environment model to do forward rollouts and estimate Z
- Sampling-based soft Q iteration: approximate with soft Bellman backups (Guided Cost Learning, Finn et al. 2016)
- Adversarial (GAIL): express rψ implicitly via a discriminator (next section)
def maxent_irl_step(reward_net, expert_states_actions, env_sampler, soft_q_planner):
# 1. Do soft Q planning under the current reward to get a sampling distribution
current_rewards = reward_net(states_actions_tensor)
sampled_trajectories = soft_q_planner.rollout(reward_net)
# 2. Compute the difference in feature expectations
expert_feat = feature_expectation(expert_states_actions, reward_net)
sampled_feat = feature_expectation(sampled_trajectories, reward_net)
# 3. Gradient ascent update on the reward
grad = expert_feat - sampled_feat
reward_net.update(grad)MaxEnt IRL is expensive: every outer update requires solving a full soft Q problem in the inner loop. That makes it hard to scale to high-dimensional problems like visual input. GAIL avoids the explicit Z computation with adversarial training.
Generative Adversarial Imitation Learning
Generative Adversarial Imitation Learning (Ho & Ermon 2016) borrows the idea behind GANs: it recasts inverse RL as a game between a discriminator Dϕ and a generator πθ.
The GAIL objective
The discriminator distinguishes "expert data" from "policy data":
ϕmaxE(s,a)∼Dexpert[logDϕ(s,a)]+E(s,a)∼πθ[log(1−Dϕ(s,a))]
The policy learns by "fooling the discriminator":
θminE(s,a)∼πθ[logDϕ(s,a)]−λH(πθ)
The second term is entropy regularization, which keeps the policy from collapsing prematurely. Here −logDϕ(s,a) acts as an implicit reward — equivalent to the adversarial derivation of rψ(s,a)=logDϕ(s,a)−log(1−Dϕ(s,a)) from MaxEnt IRL.
class GAIL:
def __init__(self, expert_data, policy, discriminator):
self.expert_buffer = expert_data # expert (s, a) pairs
self.policy = policy # any RL algorithm (PPO/TRPO/SAC)
self.disc = discriminator # binary classifier network
def update(self, n_policy_steps=5, n_disc_steps=1):
# === 1. Train the discriminator ===
for _ in range(n_disc_steps):
# Sample policy data
policy_states, policy_actions = self.policy.sample_rollout()
# Binary cross-entropy
expert_logits = self.disc(self.expert_buffer.sample())
policy_logits = self.disc(policy_states, policy_actions)
d_loss = (
F.binary_cross_entropy_with_logits(expert_logits, ones) +
F.binary_cross_entropy_with_logits(policy_logits, zeros)
)
self.disc_optim.zero_grad(); d_loss.backward(); self.disc_optim.step()
# === 2. Train the policy: use -log D as the reward ===
for _ in range(n_policy_steps):
states, actions, next_states, _ = self.policy.rollout()
with torch.no_grad():
rewards = -F.logsigmoid(self.disc(states, actions)) # r = -log(1 - D)
# Feed into any RL algorithm (assume PPO here)
self.policy.ppo_update(states, actions, rewards, next_states)The equivalence between GAIL and MaxEnt IRL
Formally, GAIL is the dual of MaxEnt IRL when the reward function is unconstrained (an arbitrary neural network). The optimal discriminator Dϕ∗ has the closed form:
Dϕ∗(s,a)=pexpert(s,a)+pπθ(s,a)pexpert(s,a)
Substituting this back in, the optimal reward is exactly r∗(s,a)=logD∗−log(1−D∗)=logpπθpexpert — the log-likelihood ratio. This matches the reward that MaxEnt IRL derives, but GAIL never has to compute the partition function Z explicitly.
Comparing the three imitation-learning approaches
| Dimension | BC | MaxEnt IRL | GAIL |
|---|---|---|---|
| Addresses distribution shift | ❌ | ✅ | ✅ |
| Needs an environment model | ❌ | ✅ (or a soft-Q approximation) | ❌ |
| Explicit reward function | — | ✅ (interpretable) | ❌ (implicit) |
| Compute cost | Low | High (inner-loop RL) | Medium (adversarial training) |
| Scales to high dimensions | Easy | Hard | Medium |
| LLM counterpart | SFT | — | Implicit in DPO (see 14.6) |
GAIL's training instability
This is the classic GAN failure mode: when the discriminator is too strong, the generator's gradient vanishes; when it's too weak, there's no signal to learn from. Common tricks in practice:
- Gradient penalty on the discriminator (Wasserstein GAIL)
- Update the discriminator more slowly than the policy (1 discriminator step per 5 policy steps)
- Tune the entropy coefficient λ to 0.1–1.0 to keep the policy from collapsing
GAIL reaches near-expert performance on MuJoCo, but it needs millions of steps of environment interaction — sample efficiency is still the bottleneck. This has driven research into offline imitation learning (e.g., DemoDICE, DWBC), which combines expert data with suboptimal data and needs no online interaction.
Section summary
Inverse RL (IRL) infers a reward function from expert behavior, and maximum-entropy IRL resolves IRL's ill-posedness. GAIL uses the GAN framework to sidestep explicit reward inference, giving imitation learning a large boost in scalability. GAIL went on to inspire later work on adversarial RL and on reward-model training in RLHF.
The next section, 13.3 Meta-RL: MAML, RL², PEARL, In-Context RL, turns to a different question — how does an agent adapt quickly to new tasks when the environment keeps changing?