Inverse Reinforcement Learning
Work backward from expert moves to the hidden reward they imply. You match feature counts, then run normal RL on the recovered goal.
Why Does This Exist?
Writing rewards by hand is brittle. For driving you want safety, progress, comfort, and legality, all traded off per second. Weight them wrong and the car either freezes or barges ahead. Hand tuning that balance across weather and cities never ends.
Inverse RL asks a person to drive instead. You watch demos and infer the reward they must have been optimizing. Then you run standard RL on that recovered reward, so the agent can handle streets the expert never showed.
This page covers the inversion loop and the ambiguity fix. Copying moves directly is imitation learning, with its simplest form in behavioral cloning. The entropy principled version has its own page in maximum entropy IRL.
The one idea you need first
An MDP is for states, actions, dynamics, discount, and reward. Here is missing and demos fill the gap. Feature counts summarize where time was spent, discounted by per step. Match those counts and values match too under linear rewards.
Think of It Like This
Guessing the judging rubric
You watch a cooking judge taste ten dishes and pick winners without seeing the score sheet. One winner is salty and fast, another is mild and slow. You list traits per dish, then find weights that make the winners score highest.
Many rubrics fit. All zeros calls every dish tied, which technically explains the picks and teaches nothing. So you pick the least opinionated rubric that still puts winners on top. That extra modesty is the maximum entropy idea, and it keeps you from inventing preferences the judge never showed.
Where the analogy stops: judges score finished plates, while agents steer through sequences where early turns change later options. Counts must be discounted and dynamics aware, not just tallied.
How It Actually Works
Assume linear reward . Symbols: is the feature vector for a state, holds the weights you seek. Expert demos give empirical counts . Your current policy gives . The MaxEnt gradient is their gap:
You step , solve RL for the new , remeasure counts, and repeat. Trajectory odds follow , so high reward paths draw most probability while every path keeps some share. That soft choice is what resolves the all zeros tie.
Worked example
Three states with , , . Start , , step .
Expert runs :
Learner under runs :
Gradient:
Update:
First weight rises by while the second holds. Next RL round values feature one more, so the learner favors over and edges toward the demo path. Those four vectors are what the diagram plots.
Code
This snippet recomputes the count match with plain lists.
"""Recompute IRL feature counts and the theta step."""
gamma = 0.9alpha = 0.5theta = [2.0, -1.0]phi = {"s1": [1.0, 0.0], "s2": [0.0, 1.0], "s3": [1.0, 1.0]}
mu_e = [phi["s1"][0] + gamma * phi["s3"][0], phi["s1"][1] + gamma * phi["s3"][1]]mu_pi = [phi["s1"][0] + gamma * phi["s2"][0], phi["s1"][1] + gamma * phi["s2"][1]]grad = [mu_e[0] - mu_pi[0], mu_e[1] - mu_pi[1]]theta_new = [theta[0] + alpha * grad[0], theta[1] + alpha * grad[1]]
print([round(v, 2) for v in mu_e])# -> [1.9, 0.9]print([round(v, 2) for v in mu_pi])# -> [1.0, 0.9]print([round(v, 2) for v in grad])# -> [0.9, 0.0]print([round(v, 2) for v in theta_new])# -> [2.45, -1.0]Watch Out For
Claiming you found the true reward
Many rewards explain the same demos, including zero everywhere. The symptom is treating recovered weights as ground truth and shipping them to new tasks where they steer oddly.
Report the reward as one consistent explanation, not the truth. Prefer MaxEnt or margin guards, test the resulting policy rather than the weights, and expect retraining when features or dynamics shift.
Ignoring the inner RL cost
Each outer step solves RL again to get . The symptom is a prototype that works on three states but stalls on larger worlds because the inner loop dominates runtime.
Budget that loop first. Use warm starts, short rollouts early, or adversarial shortcuts that skip full planning. If demos are clean and coverage is narrow, direct cloning often wins on cost.
The Quick Version
- Inverse RL recovers reward weights from expert feature counts .
- Matching counts matches values under linear .
- Here expert minus learner gives gradient .
- Update moves from to , favoring the demo path.
- Rewards stay ambiguous, so use MaxEnt guards and judge the learned policy, not the weights.