Generative Adversarial Imitation Learning (GAIL)
GAIL copies an expert without writing a reward function. A discriminator learns to spot learner moves, and its suspicion becomes the training reward.
Why Does This Exist?
Writing rewards by hand is brittle. A driving reward that says stay centered can produce a car that never changes lanes. Imitation learning avoids hand rewards by copying demos, but plain copying drifts: small errors put the learner where the expert never went, and errors compound.
Classical inverse RL tried to infer the reward first, then solve a full RL problem inside every update. In high dimensional control that inner loop is far too slow. Ho and Ermon showed in 2016 that the regularized inverse problem is dual to matching state action visitation directly. GAIL uses that duality and trains like a GAN, so no inner MDP solve is needed. Use maximum entropy IRL when you want the reward itself explained; use GAIL when you want the policy with less compute.
The one idea you need first
The occupancy measure is the discounted frequency of visiting under . If two policies visit states the same way, they earn the same return under any reward. GAIL matches those frequencies instead of recovering a reward.
Think of It Like This
A forger and a sharp curator
A forger paints in the style of a master. The curator knows the real collection and scores each canvas for authenticity. The forger never sees a chemistry report, only the score. High score means keep that brushwork, low score means change it.
Over rounds the forger gets harder to catch. At the limit the curator guesses at chance. That limit is the GAIL equilibrium at . The forger is , the curator is , and the score turns into reward.
Where it stops: paintings do not fight back. Actions change the next state, so GAIL must match whole trajectories, not single stills.
How It Actually Works
GAIL plays a minimax game between policy and discriminator in :
In words: averages over expert demos, averages over learner rollouts, is entropy to keep exploring, weights it. The discriminator wants on expert pairs and on learner pairs. The policy wants learner pairs that score near .
Discriminator step
Binary cross entropy on one expert and one learner pair is . For fixed the best discriminator is the occupancy ratio . When visitations match, that ratio is everywhere.
Policy step with surrogate reward
Freeze , then reward each learner step by:
In words: confident expert-like moves get large reward, obvious learner moves get near zero. Update with PPO or TRPO on those rewards.
Worked example
Take for a near expert move and for a noisy move:
The expert-like move pays over 15 times more, so gradients point toward expert behavior.
Now one discriminator batch with and . Loss is . Gradients are on the expert output and on the learner output, pushing the two apart.
Code
Same arithmetic in code, with checks:
import math
def surrogate_reward(d: float) -> float: """GAIL reward r = -log(1 - D), clipped for safety.""" c = min(1.0 - 1e-7, max(1e-7, d)) return -math.log(1.0 - c)
def bce_loss(d_expert: float, d_learner: float) -> tuple[float, float, float]: """Return loss, dL/dDE, dL/dDL for one pair.""" loss = -math.log(d_expert) - math.log(1.0 - d_learner) return loss, -1.0 / d_expert, 1.0 / (1.0 - d_learner)
print(round(surrogate_reward(0.80), 4)) # -> 1.6094print(round(surrogate_reward(0.10), 4)) # -> 0.1054print([round(x, 4) for x in bce_loss(0.70, 0.40)]) # -> [0.8675, -1.4286, 1.6667]Outputs match the hand math: , , and loss with the two gradients.
Watch Out For
Letting the discriminator get too strong
Early learner rollouts look nothing like demos, so classification is easy. The discriminator hits near perfect accuracy, outputs near on learner moves, and reward variance collapses. The symptom is loss near zero while the policy stops improving.
Train the discriminator one small step per policy round, never to convergence. Add spectral norm or a gradient penalty, and keep entropy bonus on so the policy keeps visiting new pairs.
Reading the surrogate as the true reward
GAIL reward measures fooling ability, not task progress. A policy can match occupancy quirks while missing intent, especially with few demos. The symptom is rising surrogate reward with flat or falling task score.
Track task return separately, grow demo coverage, and compare against inverse RL baselines when you need a transferable reward rather than a copier.
The Quick Version
- GAIL skips inner loop inverse RL by matching state action visitation directly.
- A discriminator learns expert versus learner , with optimum at occupancy ratio and at match.
- The policy trains on , so pays and pays .
- One batch at and gives loss with gradients minus and plus .
- Keep discriminator weak and entropy on, or gradients vanish early.