Twenty expert paths cross a grid. Inverse RL finds a reward that makes them likely: here the log-likelihood of a demo climbs from −16.1 to about −5.0, and the reward still works when a wall appears.
Press Step. Purple rollouts come from the soft-optimal policy for the current reward. Then the reward rises where the teal demos go more often than the policy, and falls where they go less. Press Run to repeat, then switch the map to see other rewards that explain the same paths. Then we build it piece by piece: the likelihood, its gradient, importance weights, the GAN view and GAIL.
You need GAIL in one paragraph from Reward Learning and soft value iteration from Control as Inference. We build the rest here.
A supplement to UC Berkeley CS 185/285, Deep Reinforcement Learning, Sergey Levine. Sources: Lecture 13 (inverse RL and adversarial imitation), Lecture 14 (part 1), Section 7 and Lecture 22. The words, toys and numbers are our own.
Illustrative toy: a 5×5 grid, 5 moves, 10 steps, one reward number per cell. The 20 demos are samples from a near-optimal policy for a goal and puddle reward. Each step solves soft value iteration exactly, draws 20 rollouts, and moves each cell by 0.5 × (expert visits − rollout visits).
Chapter 0
See why a learned reward survives a changed world, and why many rewards explain the same paths
A person reaches for a cup with the left hand, because the right hand holds a phone. A robot that copies actions learns "use the left hand." A robot that copies intent learns "get the cup," and uses whichever hand is free.
Behavior cloning (BC) copies actions: it maps each state to the expert's action and never reasons about outcomes. Inverse reinforcement learning (IRL) infers the reward the expert seems to maximise, then runs RL on it. When the world changes, RL plans new actions for the same goal.
| Question | Behavior cloning | Inverse RL |
|---|---|---|
| What it learns | A map from state to action | A reward, then a policy from RL |
| A wall appears | Walks into the wall | Plans around it |
| Cost | One supervised fit | RL inside a loop |
Our demos are 20 paths in a 5×5 grid, from the bottom-left start to the top-right goal. Each takes 8 moves, steps around a 2×2 puddle, and then waits 2 steps at the goal. Which reward made them? Paint one and check.
Tap a cell to cycle its reward: 0, then +1 (warm), then −1 (blue). The teal lines are the demos; the purple line is where a greedy agent goes on your reward.
Illustrative toy. A demo "is a best path" when its total reward over the 10 steps equals the best total any action sequence can get, found by dynamic programming. The greedy agent breaks ties by staying put.
Under "goal only", a demo arrives at step 8 and waits 2 steps, so it collects +1 three times: a total of 3, the best possible. Under "zero everywhere" every path totals 0, so every path is best, and the greedy agent never moves. The demos fit the useless reward perfectly.
So IRL by itself is underspecified: the demos do not pick one reward. The failure is a reward that fits the demos but trains an agent that idles. Measure it by running RL on the reward and comparing its visits with the demos. The fix is a model that asks for demos to be likely, not merely optimal: Chapter 1.
Chapter 1
Score a reward by how likely it makes the demos, and meet the one term that is hard to compute
Control as inference gives a model of a near-optimal expert: a path is more likely the more reward it collects, in proportion to e to the reward. IRL uses that model backwards. Call the reward r-psi, a function with parameters ψ: 25 numbers here, one per cell, or the weights of a neural network.
p of tau given O and psi equals p of tau, times e to the r-psi of tau, divided by Z.
Raise e to each path's total reward. Divide by Z, the same number added over all paths, so the chances sum to 1.
In our toy, 5 moves for 10 steps give 510 = 9,765,625 action sequences. With a reward of 0 everywhere, each has e0 = 1, so Z = 9,765,625 and each path has a chance of 1 in 9,765,625.
The best reward makes the demos most likely. Take the log of the chance of each demo, average over the N demos, and drop log p(τ), which does not depend on ψ.
L of psi equals one over N times the sum over i of r-psi of tau-i, minus log Z.
Add up the reward of each demo and average. Subtract log Z, so a reward cannot score well by loving every path.
Take the goal and puddle reward at scale 1. Every demo totals 1.6, and log Z = 13.77, so L = 1.6 − 13.77 = −12.17. At scale 5 the demos total 8.0, log Z = 13.10, and L = −5.10: the demos are now much more likely.
Teal is the demos' average reward, purple is log Z, warm is L, their difference. Drag the scale, then add two wandering demos.
Illustrative toy. The reward is c times the goal and puddle shape. Log Z comes from soft value iteration, exact because we know the grid's moves. The two wanderers are 10-step paths that never reach the goal.
With clean demos, L levels off at −5.09 = −log 162. The model ends spread evenly over the 162 best action sequences: 18 routes that miss the puddle, times 9 ways to spend the last 2 steps at the goal. Add 2 wanderers and L peaks at c = 3.66: a noisy expert gets a gentler reward.
Why is log Z hard? It adds over every path: 10 million here, and an integral over paths with unknown dynamics for a real robot. Drop it and the failure is a reward that grows without limit, because the first term rises forever with c. Watch log Z, and estimate it from samples: Chapters 2 and 3.
Chapter 2
Differentiate log Z, find the soft-optimal policy inside it, and turn the gradient into visit counts
To climb L we need its gradient. The first term is easy: the average of the gradient of each demo's reward. For log Z, the gradient of a log is the gradient divided by the value. So the gradient of log Z is the sum over paths of p(τ) er ∇r, divided by Z.
That weight, p(τ) er / Z, is the model's own chance of each path from Chapter 1. It is the path distribution of the soft-optimal policy, the policy that max-entropy RL learns for the current reward.
The gradient of L with respect to psi equals the expected gradient of r-psi of tau over the expert's paths, minus the expected gradient of r-psi of tau over the soft-optimal paths.
Push the reward up along the demos. Push it down along the paths the current soft-optimal policy takes. When the two sets look alike, the pushes cancel.
With one reward per cell, the gradient of a path's reward with respect to cell s is how often the path visits s. The next equation uses that.
Call the average visits to cell s by the demos μE(s), and by the soft-optimal policy μψ(s).
The partial of L with respect to r of s equals mu-E of s minus mu-psi of s.
For each cell, count how often the demos visit it and how often the policy visits it. The difference is that cell's gradient.
At step 0 the reward is 0, so the policy wanders. At the goal, μE = 3.0 and μψ = 0.002, so the gradient is 3.00 and a step of size 0.5 adds 1.50. At the start corner, μE = 0 and μψ = 2.75, so its reward drops by 1.37.
Left: demo visits. Middle: soft-optimal policy visits. Right: their difference, the gradient. Pick a point in training.
Illustrative toy. Here the policy visits are exact expectations from soft value iteration, and each step adds 0.5 × the gradient. The hero uses 20 sampled rollouts instead, as the course does.
This is the course's first algorithm. With a linear reward on features it becomes the feature matching of the CMU IRL lesson.
The failure is cost: a whole RL run for every reward step. The symptom is a loop that spends all its time in step 1. Measure the RL steps per reward step. The fix is Chapter 3.
Chapter 3
Improve the policy a little per round, and reweight its samples so the gradient stays honest
Instead of training the policy to convergence, improve it a little each round. Now the samples come from the current policy π, not from the soft-optimal one, so the second term is biased. Importance weights fix that: each sample counts in proportion to how much more likely the target makes it than π does.
w-j equals e to the sum over t of r-psi of s-t and a-t, divided by the product over t of pi of a-t given s-t.
The full ratio is p(τ) er over p(τ) times the policy's action chances. The start and the dynamics appear on top and bottom, so they cancel. Only the reward and the policy remain.
With made-up numbers: a 2-step path earns rewards 0.5 and 1.0, and the policy chose its actions with chances 0.4 and 0.5. Then w = e1.5 / (0.4 × 0.5) = 4.48 / 0.2 = 22.4.
The gradient of L is about one over N times the sum over i of the gradient of r-psi of tau-i, minus one over the sum of the w-j, times the sum over j of w-j times the gradient of r-psi of tau-j.
Turn the weights into shares that add to 1. Average the policy samples by those shares, not equally.
With made-up numbers: three samples have weights 22.4, 11.2 and 5.6, so the shares are 0.571, 0.286 and 0.143. They visit a cell 1, 0 and 2 times. The weighted count is 0.571 + 0 + 0.286 = 0.86; the plain average is 1.00.
Yellow bars are the 200 samples' shares, largest first. Drag how many reward steps pass without a policy update.
Illustrative toy. The policy is soft-optimal for the reward after 2 training steps; 200 rollouts come from it with a fixed seed. Error is the summed gap, over 25 cells, to the exact visits of the soft-optimal policy for the newer reward.
After 8 stale steps, the plain average is off by 3.13 visits and the weighted one by 0.68. But the weights also concentrate: the effective sample size, (Σw)² / Σw², falls from 200 to 100. That is the failure to watch. If it nears 1, one sample decides the gradient; the fix is to update the policy more often.
Guided cost learning (Finn et al., 2016) is this loop with a neural reward:
Chapter 4
Read the loop as a game, then build the discriminator from the reward so the weights disappear
Look at the loop of Chapter 3. The reward step makes demos more likely and policy samples less likely. The policy step makes the samples harder to tell from the demos. That is a game: the policy is the generator and the reward is the discriminator, as in a GAN.
A GAN's discriminator D(x) is a classifier that gives the chance that x is real. Call the real data's density p* and the generator's pθ.
D-star of x equals p-star of x, divided by p-theta of x plus p-star of x.
At each x, D-star is the real data's share of the total density.
With made-up densities p* = 0.3 and pθ = 0.1, D* = 0.3 / 0.4 = 0.75. When the generator matches the data, D* = 0.5.
Finn, Christiano and colleagues put the reward inside D. In IRL, the policy should end at p(τ) er / Z, so use that in place of p*. The policy's path chance is p(τ) ∏πθ, and p(τ) cancels from top and bottom.
D-psi of tau equals one over Z times e to the r-psi of tau, divided by the product over t of pi-theta of a-t given s-t, plus one over Z times e to the r-psi of tau.
Compare the reward model's chance of the path with the policy's chance. D is the reward's share. Train ψ and Z with the usual GAN loss: log D on demos plus log(1 − D) on policy samples.
With made-up numbers: the reward model gives a path 0.02 and the policy gives it 0.005. Then D = 0.02 / 0.025 = 0.8. Once the policy also gives 0.02, D = 0.5.
Pink bars are D for four paths, under the learned reward. Drag the policy from uniform toward soft-optimal.
Illustrative toy. The reward is the one IRL learns in 60 exact steps. The policy at setting λ is soft-optimal for λ times that reward, so λ = 0 is uniform and λ = 1 matches the reward model. Z is exact.
No importance weights appear. The ratio inside D does their job, and Z absorbs the normalising sum. The failure to watch is the usual GAN imbalance: D near 1 on every demo and near 0 on every sample from the first round. Measure the mean D on each side; fix it with fewer reward steps per policy step.
Chapter 5
Swap the reward-shaped discriminator for any classifier, see what you lose, and find the reward hiding in D
Reward Learning met GAIL (Ho and Ermon, 2016): D is a standard binary classifier, and the policy uses log D as its reward. Here is the policy step.
The gradient of L with respect to theta is about one over M times the sum over j of the gradient of log pi-theta of tau-j, times log D-psi of tau-j.
This is the policy gradient with log D in place of the return. Paths that fool D get pushed up more.
With made-up numbers: D = 0.8 gives log D = −0.22, and D = 0.2 gives −1.61. So the second path gets about 7 times the push down.
| Discriminator | Gain | Loss |
|---|---|---|
| Plain classifier (GAIL) | Simpler to set up, fewer moving parts | At convergence D = 0.5 everywhere, so the "reward" cannot be reused |
| Reward-shaped (IRL as a GAN) | A reward rψ survives training | Needs the policy's action chances inside D |
The course calls the two "actually the same thing." Take the log-odds of the IRL discriminator from Chapter 4.
Log D-psi minus log of one minus D-psi equals r-psi of tau, minus log Z, minus the sum over t of log pi-theta of a-t given s-t.
Divide D by 1 − D: the policy terms cancel except one. Use this log-odds as the policy's reward and average over its own paths. You get reward plus entropy, minus a constant: max-entropy RL on rψ.
From Chapter 4: D = 0.8 gives log(0.8 / 0.2) = log 4 = 1.39. The right side is log(0.02 / 0.005) = log 4 = 1.39 too.
Pick a world and what to plan with. Lines are 16 rollouts; green ends at the goal, red does not.
Illustrative toy. IRL reward: the 60-step learned reward, re-solved by soft value iteration in the new world. GAIL's final D: 0.5 on every state, as at convergence, so log D is flat. Copied actions: the demos' most common action per state, random where they never went. Success is exact.
Add the wall. The learned reward re-plans around it and reaches the goal 99.5% of the time; copied actions walk into the wall, 0%. GAIL's converged classifier says 0.5 everywhere, so its policy wanders: 0%. That is the failure of reusing a GAIL reward. Measure it by checking whether D varies over states. The fix is a reward-shaped D, or training GAIL again in the new world.
Chapter 6
Keep the equations, the numbers, a checklist and the core loop on one page, and see where to go next
Maximum entropy IRL models the expert as soft-optimal: a path's chance grows as e to its reward. The best reward maximises the demos' log-likelihood. Its gradient is expert visits minus soft-optimal visits. Guided cost learning replaces the inner RL run with a lazy policy and importance weights. Put the reward inside a GAN discriminator and the weights vanish into Z. GAIL drops the reward shape for a plain classifier: simpler, but nothing reusable remains.
| Idea | Form | Chapter |
|---|---|---|
| Expert model | p(τ|O,ψ) = p(τ) erψ(τ) / Z | 1 |
| Objective | L = mean of rψ(demos) − log Z | 1 |
| Gradient | Eexpert[∇r] − Esoft-optimal[∇r] | 2 |
| Importance weight | w = eΣr / ∏π(a|s) | 3 |
| IRL discriminator | D = (er/Z) / (∏π + er/Z) | 4 |
| GAIL policy reward | log D from a plain classifier | 5 |
| Same thing | log-odds of D = r − log Z − Σ log π | 5 |
| What | Number | Why it matters |
|---|---|---|
| Paths in the grid | 510 = 9,765,625 | Z adds over all of them |
| Demo log-likelihood | −16.09 at reward 0; about −4.98 after 60 steps | What IRL climbs |
| Clean demos, any scale | L levels at −log 162 = −5.09 | Perfect demos push the reward scale up without end |
| Two wanderers added | Best scale 3.66, L = −6.43 | Noise gives a finite reward |
| First gradient at the goal | 3.0 − 0.002 = 3.00 | Expert visits minus policy visits |
| 8 stale steps, 200 samples | Error 3.13 plain, 0.68 weighted; ESS 100 | Weights remove bias but cost samples |
| Wall added | IRL 99.5%, copied actions 0%, GAIL's final D 0% | Only a reward transfers |
pythonimport numpy as np def irl_step(r, demo_visits, policy, sample, lr=0.5, M=20): # r: (S,) one reward per state; demo_visits: (S,) mean visits per demo paths = [sample(policy) for _ in range(M)] # each: (states, actions, logp) logw = np.array([r[s].sum() - logp for s, a, logp in paths]) w = np.exp(logw - logw.max()); w /= w.sum() # self-normalised: Z cancels pol_visits = np.zeros_like(r) for wj, (s, a, logp) in zip(w, paths): np.add.at(pol_visits, s, wj) # weighted visit counts ess = 1.0 / (w ** 2).sum() # watch this return r + lr * (demo_visits - pol_visits), ess # outer loop: r, ess = irl_step(...); then improve policy a little on r (any max-ent RL)
Now press Present or Teach and explain from memory: the likelihood, why log Z is hard, the gradient as visit counts, the weights, the GAN discriminator and what GAIL gives up. Then go back to the hero, train the reward, and switch the maps.
This lesson supplements UC Berkeley CS 185/285, Deep Reinforcement Learning, Sergey Levine, at rail.eecs.berkeley.edu/deeprlcourse. It draws on Lecture 13 (pages 17 to 34), Lecture 14 (pages 2 to 12), Section 7 (pages 1 to 2) and Lecture 22 (pages 30 to 33). The papers are Finn, Levine and Abbeel, Guided Cost Learning (2016); Finn, Christiano, Abbeel and Levine, A Connection Between GANs, Inverse RL, and Energy-Based Models (2016); and Ho and Ermon, Generative Adversarial Imitation Learning (2016). The words, toys and numbers here are our own.