Search

Search papers and lessons

Inverse Reinforcement Learning

Level 4 · 12 min read

Inverse reinforcement learning infers the reward function that would explain an expert's behaviour, then trains a policy against that reward.

On this page

  • Colour = topic mix
  • Discrete math
  • Probability
  • Neural network basics
  • Calculus
  • Linear algebra
  • Reinforcement learning
  • Sign in to see your progress

TL;DR

Inverse reinforcement learning (IRL) works backwards from reinforcement learning. Instead of being given a reward and finding good behaviour, it watches good behaviour and asks which reward would make that behaviour the best choice. Once the reward is recovered, an ordinary reinforcement learner can be trained on it, and the result can generalise to situations the expert never showed.

The catch is that many different rewards explain the same behaviour, so the problem needs an extra rule to pick one.

Where you'll see it: teaching robots and self-driving systems from human demonstrations, and modelling how people trade off goals, such as how a driver balances speed against comfort.

Intuition

Start from a question you already know

Watch a friend cross a busy city. They always take the tree-lined street, even when the bus route is shorter. You want to know what they value. Maybe they like shade, maybe they dislike noise, maybe they just like walking past the bakery. Whatever the reason, their choices reveal a preference, and a good model of that preference would let you predict their next trip.

That is the IRL question. The expert's choices are the evidence. The reward is the hidden preference that the choices came from. Reinforcement learning answers "given the preference, what should I do?" Inverse reinforcement learning answers "given what was done, what preference explains it?"

Why not just copy the expert

Behavior Cloning already learns from demonstrations, by predicting the expert's action in each situation. It is simple, but it has two weaknesses. First, when the learner drifts into a situation the expert never visited, it has no guidance. Second, the copied behaviour is tied to the exact demonstrations, so it does not transfer to a new layout or a new goal.

IRL tries to recover the reason behind the behaviour. If the learner knows that the expert wants to stay in the shade, it can act sensibly in a street the expert never walked.

The simplest version: one choice

Suppose an expert is in a state with two actions, x and y, and always picks x. What reward explains this? Any reward where x is worth more than y does. The data says nothing beyond that. That already shows the core difficulty: the answer is a set, not a single reward. We will return to this in the math section.

The math

Setting

IRL starts from a Markov Decision Process with states S\mathcal{S}, actions A\mathcal{A}, transitions P(s′∣s,a)P(s' \mid s, a) and a discount factor γ∈[0,1)\gamma \in [0, 1), but without the reward RR. The discount factor says how much a reward kk steps away is worth today, namely γk\gamma^k of its face value. An expert policy πE\pi_E generated trajectories τ=(s0,a0,s1,a1,…)\tau = (s_0, a_0, s_1, a_1, \ldots). The learner sees samples of these trajectories and wants a reward function that explains them.

A common choice is a reward that is linear in known features:

Rw(s)=w⊤ϕ(s),R_w(s) = w^\top \phi(s),

where ϕ(s)\phi(s) is a vector of numbers describing the state, such as distance to the road edge or whether a cell is wet, and ww is the weight vector the learner must find. The features are chosen by the designer, and they are the main way prior knowledge enters.

The feature expectation

For a policy π\pi, define its feature expectation

μ(π)=Eπ[∑t=0∞γtϕ(st)].\mu(\pi) = \mathbb{E}_\pi\left[ \sum_{t=0}^{\infty} \gamma^t \phi(s_t) \right].

Each entry says how much of that feature the policy collects, discounted over time. The expected return under the reward is then w⊤μ(π)w^\top \mu(\pi), because the reward is linear. This is the key simplification: the policy's value depends on the weights only through its feature expectation.

Apprenticeship learning: match the features

Abbeel and Ng (2004) noticed that if the learner finds a policy π\pi whose feature expectation μ(π)\mu(\pi) matches the expert's μE\mu_E, then the learner's return under any linear reward is the same as the expert's. The learner can then perform as well as the expert on every reward of that form (with the weights bounded in size, say ∥w∥≤1\|w\| \le 1). Their method repeatedly picks a new reward direction that separates the expert from the current learner, trains a policy on that reward, and repeats until the gap closes.

The ambiguity problem

Matching features is not enough to pin down a reward. The zero reward, w=0w = 0, makes every policy equally good, so the expert is trivially optimal under it. Real demonstrations can also be explained by many non-zero rewards. Two common fixes are to demand that the expert be better than others by a margin, or to use a probabilistic model of the expert. The second is the most widely used.

Maximum-entropy IRL

Ziebart and colleagues (2008) model the expert as choosing trajectories with probability proportional to the exponential of their total reward:

Pw(τ)=exp⁡(∑tγtRw(st))Z(w),Z(w)=∑τ′exp⁡(∑tγtRw(st′)).P_w(\tau) = \frac{\exp\left( \sum_t \gamma^t R_w(s_t) \right)}{Z(w)}, \qquad Z(w) = \sum_{\tau'} \exp\left( \sum_t \gamma^t R_w(s'_t) \right).

Here Z(w)Z(w) is the normaliser, the sum over all possible trajectories, which makes the probabilities add to one. Higher reward means exponentially higher probability, but not certainty. The model is the one with the highest Entropy (the least committed) among those that match the average feature counts of the demonstrations.

Maximising the log-likelihood of the demonstrations {τj}\{\tau_j\} gives a clean gradient. Write f(τ)f(\tau) for the discounted feature sum along a trajectory and f~\tilde{f} for its average over demonstrations. Then

∇wlog⁡L(w)=f~−EPw[f(τ)].\nabla_w \log L(w) = \tilde{f} - \mathbb{E}_{P_w}\left[ f(\tau) \right].

The gradient is the gap between the expert's feature totals and the feature totals the current reward would produce. Gradient ascent on ww closes that gap, which is again feature matching. The expectation under PwP_w requires solving the MDP under the current reward, usually with a soft version of value iteration, which uses the Bellman Equation but replaces the hard maximum over actions with a smooth one, the log of a sum of exponentials (see Maximum Entropy Reinforcement Learning).

Algorithm
Maximum-entropy IRL, one iteration
  1. Start with weights ww, for example all zeros.
  2. Solve the MDP under RwR_w with soft value iteration. This gives a stochastic policy πw\pi_w.
  3. Compute the expected discounted feature counts Eπw[f]\mathbb{E}_{\pi_w}[f] by rolling out πw\pi_w or by a state-visitation calculation.
  4. Compute the gradient f~−Eπw[f]\tilde{f} - \mathbb{E}_{\pi_w}[f].
  5. Update w←w+η(f~−Eπw[f])w \leftarrow w + \eta \left( \tilde{f} - \mathbb{E}_{\pi_w}[f] \right) with step size η>0\eta \gt 0.
  6. Repeat until the gradient is small.

Step 2 is the expensive part. Every outer update needs an inner solve of the MDP, which is why IRL is slow compared with behaviour cloning.

Using the recovered reward

Once ww is found, the learner has a reward. It can train a policy with any reinforcement learning method, such as Policy Gradient methods. The policy is then no longer a copy of the expert, but an optimiser of the inferred goal. This is what lets it handle new states.

Worked examples

Example
Two routes, one choice

Setup. A commuter has two routes, route 1 and route 2. We use a single feature: route 1 has feature value ϕ1=1\phi_1 = 1 and route 2 has ϕ2=0\phi_2 = 0 on a "pleasant" feature, and the reward of a route is w⋅ϕw \cdot \phi for a single weight ww (the simplest case of the linear reward above). The maximum-entropy model gives P(route 1)=ewew+e0P(\text{route } 1) = \dfrac{e^{w}}{e^{w} + e^{0}}.

  1. Observed frequency: the commuter takes route 1 on 80 percent of days.
  2. Set the model equal to the observed frequency: ewew+1=0.8\dfrac{e^{w}}{e^{w} + 1} = 0.8.
  3. Solve: ew=0.8ew+0.8e^{w} = 0.8 e^{w} + 0.8, so 0.2ew=0.80.2 e^{w} = 0.8, so ew=4e^{w} = 4.
  4. Take the logarithm: w=ln⁡4≈1.386w = \ln 4 \approx 1.386.
  5. Check: e1.386/(e1.386+1)=4/5=0.8e^{1.386} / (e^{1.386} + 1) = 4 / 5 = 0.8. The model reproduces the observed frequency.

Interpretation. The positive weight says the commuter values the pleasant feature. The number 1.3861.386 is not the reward in any absolute sense, only the difference between the two routes, which is all the data can reveal.

Example
The degenerate case

Setup. A state has actions x and y. The expert takes x every time.

  1. Suppose R(x)=1R(x) = 1 and R(y)=0R(y) = 0. The expert is optimal, and the demonstrations are consistent with it.
  2. Suppose R(x)=0R(x) = 0 and R(y)=0R(y) = 0. Both actions are equally good, so the expert's choice of xx is still optimal under the tie-breaking rule, and the demonstrations are consistent with this too.
  3. Suppose R(x)=−1R(x) = -1 and R(y)=−2R(y) = -2. The expert is still optimal, because xx beats yy.
  4. In this one-state case, every reward with R(x)≥R(y)R(x) \ge R(y) explains the data. That includes the useless reward that is zero everywhere.

Interpretation. Demonstrations alone say the expert prefers xx to yy, and nothing about how much. A learner that copies this reward would have no sense of scale, which matters when it must trade one goal against another. Probabilistic models and margin constraints exist to break exactly this tie.

Example
A realistic case: one gradient step for a driving policy

Setup. (Illustrative numbers, not from a real dataset.) A driving policy is described by two features: ϕ1\phi_1, the fraction of time spent within half a metre of the lane edge, and ϕ2\phi_2, the fraction of time spent in the fast lane. Human demonstrations give an average f~=(0.2,0.6)\tilde{f} = (0.2, 0.6). The current policy, under the current reward, gives an expected feature count Eπw[f]=(0.5,0.4)\mathbb{E}_{\pi_w}[f] = (0.5, 0.4). Take step size η=0.5\eta = 0.5 and start from w=(0,0)w = (0, 0).

  1. Gap: f~−Eπw[f]=(0.2−0.5,0.6−0.4)=(−0.3,0.2)\tilde{f} - \mathbb{E}_{\pi_w}[f] = (0.2 - 0.5, 0.6 - 0.4) = (-0.3, 0.2).
  2. Update: w←w+0.5×(−0.3,0.2)=(−0.15,0.10)w \leftarrow w + 0.5 \times (-0.3, 0.2) = (-0.15, 0.10).
  3. Interpretation of the first weight: it is negative, so the new reward penalises time near the lane edge. That is correct, because the current policy hugs the edge far more than the experts do.
  4. Interpretation of the second weight: it is positive, so the new reward favours the fast lane, which the experts use more than the current policy.
  5. Next step: re-solve the MDP under the new reward, which shifts the policy toward less edge time and more fast-lane time, and repeat until the gap is near zero.

Interpretation. One step is not a model. It shows the mechanism. The learner repeatedly moves its reward until its own behaviour looks like the expert's on the features the designer chose, and the quality of the final reward depends on whether those features capture what the expert cares about.

Tradeoffs and failure modes

SituationWhat goes wrongBetter choice
Demonstrations are few, or the states rarely visited by the expert matterThe recovered reward is wrong outside the data, so the policy fails thereCollect more demonstrations, or add prior structure to the reward
The features leave out something the expert cares aboutThe learner matches features but behaves wrongly, because the missing factor is absorbed into other featuresAdd features, or learn the features too
The environment model is unknown and cannot be simulatedThe inner MDP solve is impossibleAdversarial imitation such as [[gail-paper
Only the next action is needed and the states look the same as the demonstrationsIRL's extra work is wastedBehavior Cloning, which is far cheaper
The expert is not optimal, for example it is tired or inconsistentForcing an optimal reward explanation misleads the learnerModels of noisy or bounded rationality

The main tradeoff is between IRL's generalisation and its cost. Recovering a reward lets the learner act in new states, but every reward update costs a full inner solve.

History

Andrew Ng and Stuart Russell (2000) framed the problem of recovering a reward from optimal behaviour, showing that it is ill-posed without further assumptions. Pieter Abbeel and Andrew Ng (2004) turned it into apprenticeship learning, based on matching feature expectations, and showed that the learner's performance can match the expert's on the reward family. Brian Ziebart and colleagues (2008) introduced the maximum-entropy formulation, which gave a principled, probabilistic answer to the ambiguity problem and made it practical to fit to real trajectory data, including driver route choices.

The field then moved in two directions. One kept the reward as the goal and improved the inner solvers. The other, led by Jonathan Ho and Stefano Ermon (2016), showed that the reward need not be recovered explicitly. Their method trains a discriminator (a classifier) to tell expert trajectories from learner trajectories, using a generative adversarial setup, and uses the discriminator's output as a learning signal. That approach, GAIL, is now a standard tool.

Recap

Reinforcement learning maps a reward to a good policy. IRL maps a good policy back to a reward. Because many rewards explain the same behaviour, the method needs a tie-breaker, usually a maximum-entropy model that matches feature counts. Its gradient is the gap between expert and learner feature counts, so each update is a feature-matching step. The recovered reward can train a new policy that generalises beyond the demonstrations, at a cost in compute. The natural next step is to see how the matching idea is turned into an adversarial game in GAIL.

Check yourself

QuestionWhy can the zero reward always be used to explain an expert's behaviour in IRL, and what does that tell you about the problem?Show answer

Under the zero reward every action has the same return, so every policy is optimal, including the expert. Therefore the demonstrations are consistent with it. This shows IRL is ill-posed without extra assumptions: the data identifies a set of rewards, not a single one. Probabilistic models such as maximum entropy, or margin constraints, pick a specific member of that set.

QuestionIn the two-route example, the commuter takes route 1 on 75 percent of days and the model is P=ew/(ew+1)P = e^{w}/(e^{w}+1). What is ww? Give two decimal places.Show answer

Set ew/(ew+1)=0.75e^{w}/(e^{w}+1) = 0.75. Then ew=0.75ew+0.75e^{w} = 0.75 e^{w} + 0.75, so 0.25ew=0.750.25 e^{w} = 0.75 and ew=3e^{w} = 3. Therefore w=ln⁡3≈1.10w = \ln 3 \approx 1.10.

QuestionIn maximum-entropy IRL, the gradient is the expert's average feature count minus the model's expected feature count. Explain what happens when the two are equal.Show answer

The gradient is zero, so the update stops. At that point the model's expected features match the demonstrations, which is the condition the maximum-likelihood fit requires. The learned reward reproduces the expert's average feature usage, although other rewards might also do so.

QuestionA team has 50 demonstrations, a simulator, and needs a policy that works when the robot is pushed into states the demonstrations never covered. Would you pick behaviour cloning or IRL, and why?Show answer

IRL is the better fit. Behaviour cloning has no guidance in states the demonstrations did not visit, while an inferred reward can be optimised in new states. The simulator makes the inner MDP solves feasible. The main risk is that the 50 demonstrations must cover enough feature variety to pin the reward down, so the team should check that.

QuestionWhy does IRL need an inner solve of the MDP at every update, and what does that cost compared with behaviour cloning?Show answer

The expected feature counts under the current reward depend on the policy that the reward induces, so the learner must solve the MDP for each candidate reward. Behaviour cloning is a single supervised fit with no inner loop, so it is much cheaper, but it lacks the generalisation that a recovered reward gives.

Sources