Inverse RL & Reward Learning
Inferring the reward that explains observed behavior.
A taxi driver never states how she trades speed against distance and turns, yet her routes reveal it. Inverse reinforcement learning infers a reward from behavior. This lesson poses the problem precisely, derives Ng and Russell's characterization of the rewards that make a policy optimal, faces the ambiguity it exposes, builds the two ideas that tame it, feature matching and maximum entropy, and follows reward learning to comparisons, corrections and RLHF.
What you’ll learn
- State the inverse RL problem, and derive Ng and Russell's linear conditions under which a given policy is optimal.
- Explain reward ambiguity (degenerate rewards, scaling, potential-based shaping) and why behavior pins down at most an equivalence class of rewards.
- Explain why matching feature expectations guarantees near-expert performance, and implement maximum-entropy IRL in a tabular MDP.
- Describe reward learning from comparisons, corrections and other feedback as Boltzmann-rational choice, and its relation to RLHF.
The inverse problem
Forward RL takes an MDP and finds an optimal policy. Inverse RL (Russell, 1998; Ng & Russell, 2000) runs the other way: given the MDP without its reward and observations of an expert's behavior, either its policy or sampled demonstrations, find a reward function under which that behavior is optimal or nearly so. Three motivations recur:
- Transfer. A reward is often a more compact and more portable description of a task than a policy: it can be re-optimized on a new map, with new dynamics or for new start states.
- Understanding behavior. In economics, transportation and human–robot interaction the reward is the object of interest itself: what do drivers trade off, where is a pedestrian heading?
- Specification. When a reward is hard to write down, as with a pleasant driving style, demonstrating the task specifies it indirectly.
Ng and Russell studied the cleanest case: finitely many states, actions with transition matrices , a reward that depends on the state only, , and a fully known expert policy. Renaming the actions in each state, we may assume the expert always takes .
The policy's value solves its Bellman equation, and is invertible for (Policy Evaluation as Linear Algebra):
By the policy improvement theorem, is optimal exactly when it is greedy with respect to its own values: in every state, is at least as good as any . The immediate reward is the same for every action, and , so this reduces to a comparison of expected next values:
Substitute . Strict inequalities in every state mean no other action ties with anywhere, which is uniqueness.
Read the theorem as geometry. The rewards that make optimal form a polyhedral convex cone: linear inequalities in , closed under positive scaling and under addition. Ng and Russell immediately noted its embarrassment: satisfies every inequality, since under the zero reward every policy is optimal. So does every positive multiple of a solution, and the cone usually contains rewards with very different meanings.
One behavior, many rewards
This reward ambiguity has a clean structure. Three transformations never change which policies are optimal:
- Positive scaling, with .
- Adding a constant to every reward, in a continuing discounted task. (With terminal states it is not harmless: a constant changes how attractive it is to end the episode.)
- Potential-based shaping (Ng, Harada & Russell, 1999; see Reward Shaping): for any , the reward . Telescoping the shaping terms along any trajectory shifts every action value by the same state-dependent amount, , so the ranking of actions in each state is unchanged, whatever the dynamics.
Beyond these, a demonstration only shows what the expert did where it went. It says little about states it never visited, and nothing about how much better its choices were than the alternatives. The widget below makes the second point concrete: the same reward, observed under two different dynamics, reveals different things.
An inverse RL method returns a reward . You add to it for some arbitrary . Which statement is true?
- The new reward makes different policies optimal unless is constant
- The new reward explains the demonstrations exactly as well, in every environment with the same states, so no amount of demonstrations can determine
- The new reward explains the demonstrations better, because it has more parameters
- The two rewards are identical
Show the answer
B. The new reward explains the demonstrations exactly as well, in every environment with the same states, so no amount of demonstrations can determine
Potential-based shaping changes every action value in state by the same amount, , for every policy and every transition function, so the optimal policies, and with them any behavior generated from them, never change. Rewards learned from demonstrations of optimal behavior are at best determined up to such transformations, and up to much more when the expert is only observed in some states.
IRL algorithms therefore add a selection principle. Ng and Russell's first proposal was a margin: among rewards that make the expert optimal, with , maximize the total gap between the expert's action and the best alternative, , minus a penalty that favors simple rewards. That is a linear program. The two principles that shaped the field came next.
Match features, not rewards
Suppose the reward is linear in known features, , with unknown weights. A policy's feature expectations determine its value for every such reward, , by linearity of expectation. (Bold denotes feature expectations; the start distribution is still .) Hence, by Cauchy–Schwarz,
A learner that matches the expert's feature expectations is as good as the expert under every reward in the class, without ever identifying the true one. That is the idea of apprenticeship learning (Abbeel & Ng, 2004), developed in its own Fixed Point lesson.
Feature matching still leaves many policies, and many trajectory distributions, that match. Maximum-entropy IRL (Ziebart, Maas, Bagnell & Dey, 2008) picks the least committal one: among distributions over trajectories whose expected feature counts equal the expert's, take the one with maximum entropy. For deterministic dynamics that distribution is with : better trajectories are exponentially more likely, but none is impossible, which also makes the model tolerant of an imperfect expert. Fitting by maximum likelihood has a famously simple gradient, the expert's feature counts minus the model's expected feature counts, computed by a backward pass (soft value iteration) and a forward pass (expected state visits).
import numpy as np
def maxent_irl(P, phi, demos, T, lr=0.1, iters=200):
"""P: (A, S, S) with P[a, s, s2] = P(s2 | s, a); phi: (S, k) state features;
demos: list of integer arrays, each the T states of one demonstration."""
A, S, _ = P.shape
f_expert = np.mean([phi[traj].sum(axis=0) for traj in demos], axis=0)
p0 = np.bincount([traj[0] for traj in demos], minlength=S) / len(demos)
w = np.zeros(phi.shape[1])
for _ in range(iters):
r = phi @ w # state rewards
V, policy = np.zeros(S), []
for t in range(T - 1, -1, -1): # backward pass: soft value iteration
Q = r[None, :] + P @ V # Q[a, s] = r(s) + E[V(s2)]
V = np.logaddexp.reduce(Q, axis=0) # soft maximum over actions
policy.append(np.exp(Q - V[None, :])) # pi_t(a | s)
policy = policy[::-1]
D, f_model = p0.copy(), np.zeros_like(w)
for t in range(T): # forward pass: expected visits
f_model += D @ phi
D = np.einsum('s,as,ast->t', D, policy[t], P)
w += lr * (f_expert - f_model) # log-likelihood gradient
return wTabular maximum-entropy IRL over a finite horizon , undiscounted as in Ziebart et al. The soft backup averages over next states, which is the causal version of the model; for deterministic dynamics it coincides with the 2008 trajectory model. The Fixed Point lesson derives every line.
Other evidence about rewards
Demonstrations are one kind of evidence among many. People compare two behaviors, push a robot away from a vase, switch it off, or say what they want. Jeon, Milli and Dragan (2020) proposed one lens for all of them, reward-rational (implicit) choice. Each piece of feedback is a choice from a set of options (two trajectories to compare, all demonstrations the person could have given, stopping the robot or letting it continue); a grounding function maps each option to the behavior it stands for; and the person chooses Boltzmann-rationally, with probability proportional to for a rationality coefficient . Learning the reward is then maximum likelihood or Bayesian inference under one model, whatever the feedback.
With , trajectory A's return exceeds B's by 1. Under the Bradley–Terry model, how often does the labeler prefer A?
Show the answer
0.73
. A labeler who prefers the better trajectory only 73% of the time still carries information: the rate of agreement grows with the size of the difference, which is how comparisons reveal not only the order of rewards but their differences, provided the noise model is right.
Uses, limits and what comes next
Some representative uses:
- Route choice. Ziebart et al. (2008) modeled Pittsburgh's road network as a deterministic MDP with over 300,000 states (road segments) and learned how 25 taxi drivers traded off road features from over 100,000 miles of GPS traces. The model predicted routes and inferred destinations from partly completed trips.
- Driving and motion planning. Abbeel, Dolgov, Ng and Thrun (2008) learned the trade-offs of a robot car's parking-lot planner, such as forward versus reverse driving and staying on the road, from demonstrations; Kuderer, Gulati and Burgard (2015) learned individual drivers' styles with feature-based IRL, to make an autonomous car drive the way its passenger prefers.
- Forecasting people. Kitani, Ziebart, Bagnell and Hebert (2012) extended maximum-entropy inverse optimal control to forecast pedestrians' paths and destinations from video.
Key takeaways
- Inverse RL infers a reward under which observed behavior is (near-)optimal. In finite MDPs with state rewards, is optimal exactly when for all : a convex cone of rewards that always contains .
- Positive scaling, constant shifts (in continuing tasks) and potential-based shaping never change optimal policies, and demonstrations reveal nothing about states the expert never visits: behavior pins down at most an equivalence class of rewards.
- Values are linear in feature expectations, so matching the expert's feature expectations guarantees expert-level performance for every reward in the class; maximum-entropy IRL selects the least committal model consistent with that match.
- Comparisons, corrections, stops and demonstrations can all be modeled as Boltzmann-rational choices that reveal the reward; for comparisons this is the Bradley–Terry model behind RLHF's reward models.
- Inverse RL depends on its model of the demonstrator and usually solves RL in its inner loop; with a wrong model of rationality, more data do not fix the inferred reward.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
In Ng and Russell's condition , what does each component of the vector express?
- That the expert's action earns a larger immediate reward than in that state
- That in that state the expected value of the next state, under the values of following , is at least as large after as after
- That the reward is nonnegative in that state
- That is the most frequently demonstrated action in that state
Show the answer
B. That in that state the expected value of the next state, under the values of following , is at least as large after as after
is , and row of compares the expected next value after with that after . Since the reward depends only on the state, that comparison is exactly the policy improvement condition.
Which reward always belongs to the set of rewards under which a given policy is optimal?
- The true reward of the task
- The reward that pays 1 for the expert's action and 0 otherwise
- The reward of maximum margin
- The zero reward
Show the answer
D. The zero reward
Under every policy is optimal, so the zero reward satisfies every inequality of the cone. It is the starkest form of reward ambiguity, and the reason IRL needs a selection principle such as a margin or maximum entropy.
A learner's feature expectations are within of the expert's in Euclidean norm. What is guaranteed for every reward with ?
- Nothing, unless the learner also recovers
- The learner's value is within of the expert's
- The learner's value is within of the expert's
- The learner's policy equals the expert's
Show the answer
B. The learner's value is within of the expert's
Values are linear in feature expectations, , so Cauchy–Schwarz gives . The discount is already inside .
An RLHF reward model is fitted by maximizing the Bradley–Terry likelihood of human comparisons. In the reward-rational choice view, what is the person's choice set for one label?
- The two responses being compared
- Every response the model could ever produce
- The possible values of the reward
- The parameters of the language model
Show the answer
A. The two responses being compared
Each label is a choice between the two shown responses, made with probability proportional to of each. Demonstrations are the case where the choice set is all feasible trajectories, which turns the same model into maximum-entropy IRL.
Why can an IRL method infer a badly wrong reward even from unlimited, noise-free data?
- Because gradient methods cannot fit the maximum-entropy model exactly
- Because unlimited data always overfit the features
- Because it relies on a model of how the demonstrator chooses, and if the demonstrator is biased or plans differently, the data are best explained by a wrong reward
- Because rewards are only defined for optimal agents
Show the answer
C. Because it relies on a model of how the demonstrator chooses, and if the demonstrator is biased or plans differently, the data are best explained by a wrong reward
Behavior depends on the reward and on how the agent turns rewards into actions. A misspecified model of the second absorbs its errors into the first, and more data only make the wrong answer more certain. This is the core of Armstrong and Mindermann's argument.
Go deeper
- Maximum-Entropy Inverse RL · A probabilistic model of near-optimal behavior, and its feature-matching gradient.
- Apprenticeship Learning & Feature Matching · Matching an expert’s feature expectations, with and without a game.
- Which Rewards Explain Behavior? · Reward ambiguity, shaping invariance and what demonstrations can identify.
Further reading
- A. Y. Ng & S. Russell, "Algorithms for inverse reinforcement learning," ICML (2000).
- P. Abbeel & A. Y. Ng, "Apprenticeship learning via inverse reinforcement learning," ICML (2004).
- B. D. Ziebart, A. Maas, J. A. Bagnell & A. K. Dey, "Maximum entropy inverse reinforcement learning," AAAI (2008).
- H. J. Jeon, S. Milli & A. D. Dragan, "Reward-rational (implicit) choice: A unifying formalism for reward learning," NeurIPS (2020).