Maximum Entropy Inverse Reinforcement Learning
Watch an expert, count what they visit, then pick the reward that makes those counts likely while assuming nothing else. Higher return means higher chance.
Why Does This Exist?
Writing rewards by hand is brittle. A small misspecification invites reward hacking, where the agent maxes the score while driving badly. Inverse RL flips the task: infer the reward from expert demos.
Many rewards fit the same demos, including the zero reward that makes everything optimal. That is reward ambiguity. Margin methods also break ties arbitrarily and assume a perfect expert. Maximum entropy IRL from Ziebart and coauthors in 2008 answers both. It matches expert feature counts while keeping trajectory spread as wide as possible. Noisy demos stay likely, equal return paths stay equal, and linear rewards give one global optimum.
This page covers the entropy fix. The base inverse problem lives in inverse RL. Copying actions without rewards lives in imitation learning. The adversarial shortcut lives in GAIL.
The one idea you need first
A feature map turns a state into counts like road type or speed. A path total sums those counts along the run. Reward weights price each feature with , so path return is . Everything below is matching these counts.
Think of It Like This
Guessing a soup recipe from many bowls
Fifty cooks trained by one chef serve fifty balanced bowls, no two with identical salt and acid. A rigid rule that demands one exact recipe calls the data contradictory. A loose rule that accepts any edible bowl also accepts plain water.
You act like a calm taster. You assume tastier bowls appear exponentially more often, with natural wobble around the best mix. You match the mean salt and acid across bowls, then pick the widest recipe spread that still hits those means. You add no truffle oil the data never demanded.
Where it stops: soup mixes at once in a bowl. Demos unfold step by step, where an early turn changes which states remain reachable.
How It Actually Works
Expert set holds paths. Mean expert count is:
Max entropy asks for the path spread with largest Shannon entropy that still matches . The unique answer is Boltzmann:
Normalizer sums over all paths. Log likelihood of demos is:
Gradient is expert minus learner:
In words: raise weights on features the expert visits more than the current model, lower the rest. Linear rewards make concave, so ascent reaches the global peak. Large grids compute the learner mean with soft backward values plus forward visit counts instead of listing every path.
Worked example
Three stops with two features: , , . Two routes reach the goal:
- Route 1 through : .
- Route 2 through : .
Weights start at . Ten demos split 8 to route 1 and 2 to route 2.
Returns:
Energies and . Normalizer:
Model chances:
Expert mean:
Learner mean:
Gradient:
Step with rate :
Expert favors feature one more than the model, so weight one rises and weight two falls. Repeating closes the gap. Those six numbers are what the diagram draws.
Code
This snippet uses only the standard library and prints the same partition, chances, means, gradient, and update.
import math
mu1 = [2.0, 1.0]mu2 = [1.0, 2.0]theta = [1.0, 0.5]r1 = theta[0] * mu1[0] + theta[1] * mu1[1]r2 = theta[0] * mu2[0] + theta[1] * mu2[1]print([round(r1, 4), round(r2, 4)]) # -> [2.5, 2.0]e1 = math.exp(r1)e2 = math.exp(r2)z = e1 + e2print(round(z, 4)) # -> 19.5716p1 = e1 / zp2 = e2 / zprint([round(p1, 4), round(p2, 4)]) # -> [0.6225, 0.3775]expert = [0.8 * mu1[0] + 0.2 * mu2[0], 0.8 * mu1[1] + 0.2 * mu2[1]]learner = [p1 * mu1[0] + p2 * mu2[0], p1 * mu1[1] + p2 * mu2[1]]print([round(v, 4) for v in expert]) # -> [1.8, 1.2]print([round(v, 4) for v in learner]) # -> [1.6225, 1.3775]grad = [expert[0] - learner[0], expert[1] - learner[1]]print([round(v, 4) for v in grad]) # -> [0.1775, -0.1775]print([round(theta[0] + grad[0], 4), round(theta[1] + grad[1], 4)]) # -> [1.1775, 0.3225]Each print matches the hand path: at , chances and , gradient and , new weights and .
Watch Out For
Summing every path on a large grid
Listing all paths to get works for two routes and explodes on long horizons. Path count grows exponentially with steps, memory follows, and naive sampling misses the narrow high reward corridors.
Use soft backward values plus forward visit counts on tabular tasks. On continuous control switch to guided sampling that focuses on high reward corridors, or skip with an adversarial matcher. Exact sums are for teaching grids only.
Reading matched counts as proof of true reward
Many weight vectors can match the same counts when features overlap. Matching counts guarantees matching return under linear rewards, not recovery of the chef private weights.
Report the fitted weights as one consistent explanation, test the implied policy on held out starts, and add features only when demos demand them. Extra features invent distinctions the expert never showed.
The Quick Version
- Reward ambiguity means many rewards fit the same demos, including zero everywhere.
- Max entropy picks the widest path spread that still matches expert feature means.
- Path chance is with in the worked pair.
- Gradient is expert mean minus learner mean, here .
- Update moves weights to and repeats until means agree.