Simple Algorithm
Reinforcement Learning · Fixed Point · 28 min

Maximum-Entropy Inverse RL

A probabilistic model of near-optimal behavior, and its feature-matching gradient.

Maximum-entropy inverse RL turns an ill-posed inverse problem into a well-posed statistical one: of all the ways to match the expert's feature counts, assume nothing more. This lesson derives the resulting exponential-family model and its gradient, repairs its flaw under stochastic dynamics with maximum causal entropy, builds the backward–forward algorithm, and follows the idea to neural rewards, sampled partition functions and an exact equivalence with GANs.

What you’ll learn

  • Derive the maximum-entropy trajectory distribution under feature-matching constraints, show that fitting it is concave maximum likelihood, and compute its gradient.
  • Explain why the 2008 trajectory model misbehaves under stochastic dynamics, and state the soft Bellman recursion of maximum causal entropy.
  • Write the backward (soft value iteration) and forward (visitation) passes and state their cost.
  • Explain deep maximum-entropy IRL, guided cost learning's importance-sampled partition function, and the equivalence between sample-based maximum-entropy IRL and a GAN whose discriminator knows the generator's density.

Maximum entropy over trajectories

DerivationThe maximum-entropy distribution and its dual
  1. max⁡p −∑τp(τ)log⁡p(τ)s.t.∑τp(τ) f(τ)=f~,∑τp(τ)=1\max_p\ -\sum_\tau p(\tau)\log p(\tau)\quad\text{s.t.}\quad\sum_\tau p(\tau)\,\mathbf f(\tau) = \tilde{\mathbf f},\quad\sum_\tau p(\tau) = 1

    Maximize the entropy subject to matching the features and normalization, with multipliers θ\boldsymbol\theta and λ0\lambda_0:

  2. pθ(τ)=exp⁡(θ⊤f(τ))Z(θ),Z(θ)=∑τexp⁡(θ⊤f(τ))p_{\boldsymbol\theta}(\tau) = \frac{\exp\big(\boldsymbol\theta^\top\mathbf f(\tau)\big)}{Z(\boldsymbol\theta)},\qquad Z(\boldsymbol\theta) = \sum_\tau\exp\big(\boldsymbol\theta^\top\mathbf f(\tau)\big)

    Setting the derivative of the Lagrangian with respect to p(τ)p(\tau) to zero, −log⁡p(τ)−1+θ⊤f(τ)+λ0=0-\log p(\tau) - 1 + \boldsymbol\theta^\top\mathbf f(\tau) + \lambda_0 = 0, gives an exponential family over trajectories:

  3. min⁡θ log⁡Z(θ)−θ⊤f~⟺max⁡θ L(θ)=1m∑j=1mlog⁡pθ(τj)\min_{\boldsymbol\theta}\ \log Z(\boldsymbol\theta) - \boldsymbol\theta^\top\tilde{\mathbf f}\quad\Longleftrightarrow\quad\max_{\boldsymbol\theta}\ \mathcal L(\boldsymbol\theta) = \frac1m\sum_{j=1}^m\log p_{\boldsymbol\theta}(\tau_j)

    Substituting back, the entropy is log⁡Z(θ)−θ⊤Epθ[f]\log Z(\boldsymbol\theta) - \boldsymbol\theta^\top\E_{p_{\boldsymbol\theta}}[\mathbf f] and the Lagrangian collapses to the dual function log⁡Z(θ)−θ⊤f~\log Z(\boldsymbol\theta) - \boldsymbol\theta^\top\tilde{\mathbf f}. Minimizing it is the same as maximizing the average log-likelihood of the demonstrations:

  4. ∇L(θ)=f~−Eτ∼pθ[f(τ)],∇2L(θ)=−Cov⁡τ∼pθ[f(τ)]⪯0\nabla\mathcal L(\boldsymbol\theta) = \tilde{\mathbf f} - \E_{\tau\sim p_{\boldsymbol\theta}}\big[\mathbf f(\tau)\big],\qquad \nabla^2\mathcal L(\boldsymbol\theta) = -\Cov_{\tau\sim p_{\boldsymbol\theta}}\big[\mathbf f(\tau)\big]\preceq 0

    The gradient of log⁡Z\log Z is the model's expected feature count, and its Hessian is the feature covariance, which is positive semidefinite. So the log-likelihood is concave, its gradient is expert features minus model features, and at the optimum the two match:

Predict before you look

Three routes take 10, 10 and 12 minutes. The only feature is travel time, with weight θ=−1\theta = -1 per minute. Under the maximum-entropy model, what is the probability of the 12-minute route?

Show the answer

0.06

p=e−12/(2e−10+e−12)=1/(2e2+1)≈0.063p = e^{-12}/(2e^{-10} + e^{-12}) = 1/(2e^2 + 1)\approx0.063. The two equally fast routes share the rest equally, about 0.47 each. A model that chose locally, junction by junction, could give equally good routes different probabilities depending on how many branches they pass; Ziebart et al. called this label bias and avoided it by normalizing over whole trajectories.

Two properties made the model popular. Trajectories with equal reward get equal probability and better ones get exponentially more, so the model ranks behavior by reward and nothing else. And every feasible trajectory has positive probability, so suboptimal or inconsistent demonstrations never make the problem infeasible: they lower the fitted ∥θ∥\norm{\boldsymbol\theta} instead, the model's way of saying the demonstrator is noisy.

Stochastic dynamics and maximum causal entropy

With stochastic transitions a trajectory is no longer fixed by the actions. Ziebart et al. (2008) approximated pθ(τ)∝exp⁡(θ⊤f(τ))∏tP(st+1∣st,at)p_{\boldsymbol\theta}(\tau)\propto\exp\big(\boldsymbol\theta^\top\mathbf f(\tau)\big)\prod_t P(s_{t+1}\mid s_t,a_t). That lets the outcome of a transition influence the probability of the action before it: an action looks better when its lucky outcomes are good, as if the agent could choose its luck.

Predict before you look

Under the 2008 trajectory model, what is P(gamble)=cosh⁡2/(1+cosh⁡2)P(\text{gamble}) = \cosh2/(1+\cosh2)?

Show the answer

0.79

cosh⁡2≈3.762\cosh2\approx3.762, so P≈3.762/4.762≈0.79P\approx3.762/4.762\approx0.79. The trajectory model aggregates outcomes by log⁡E[eR]\log\E[e^{R}], which exceeds E[R]\E[R] by Jensen's inequality, so a demonstrator who is indifferent between the two actions looks risk-seeking. Fitting such a model to real data would attribute to people a taste for gambling that is an artifact of the model.

Two remarks. The likelihood of a demonstration under the causal model is ∏tπθ(at∣st)\prod_t\pi_{\boldsymbol\theta}(a_t\mid s_t): the transition probabilities do not depend on θ\boldsymbol\theta and drop out, so the dynamics are needed only to compute πθ\pi_{\boldsymbol\theta}, not to score data. And the recursion is exactly the soft Bellman equation of maximum-entropy RL (Maximum-Entropy RL & Soft Actor–Critic): maximum-entropy IRL is the inverse of maximum-entropy RL, which is why it reappears inside GAIL, AIRL and IQ-Learn.

The backward–forward algorithm

Each gradient step needs the model's expected feature counts. Over a horizon TT they come from two dynamic programs, one backward in time and one forward.

text
Backward pass (soft value iteration), V_T(s) = 0; for t = T−1, ..., 0:
    Q_t(s, a)  = θᵀφ(s, a) + Σ_s' P(s' | s, a) · V_{t+1}(s')
    V_t(s)     = log Σ_a exp Q_t(s, a)
    π_t(a | s) = exp(Q_t(s, a) − V_t(s))
Forward pass (expected visits), D_0(s) = Pr(S_0 = s); for t = 0, ..., T−1:
    D_{t+1}(s') = Σ_{s, a} D_t(s) · π_t(a | s) · P(s' | s, a)
Gradient:
    f̃ − Σ_t Σ_{s, a} D_t(s) · π_t(a | s) · φ(s, a)

Each pass costs O(T ∣S∣ ∣A∣ b)\order{T\,\abs{\mathcal S}\,\abs{\mathcal A}\,b} for at most bb successor states per state–action pair, and both are repeated at every gradient step: the inner loop is a full soft planning problem. Ziebart et al.'s 2008 algorithm is the same computation for deterministic dynamics, written with partition functions Zs=eV(s)Z_s = e^{V(s)}.

Three practical notes.

  • Work in log space. VV is a log-sum-exp; exponentiating QQ directly overflows as soon as rewards are large.
  • Mind the horizon. With an infinite horizon and absorbing goal states, ZZ can diverge even when every reward is negative, as Ziebart et al. note; a finite horizon or a discount avoids it.
  • The data are estimates too. f~\tilde{\mathbf f} averages mm demonstrations. With bounded features, Hoeffding's inequality and a union bound over the kk features control each coordinate's error at rate O(log⁡(k/δ)/m)\order{\sqrt{\log(k/\delta)/m}} times the range of the feature counts, which is the statistical side of the feature-matching guarantee.

Neural rewards, sampled partition functions and GANs

Deep maximum-entropy IRL (Wulfmeier, Ondruska & Posner, 2015) replaces the linear reward with a network rψ(s)r_\psi(s), for example a convolutional network over a map. With one indicator feature per state, the gradient with respect to each state's reward is the demonstrations' average number of visits νD(s)\nu_{\data}(s) minus the model's expected number νψ(s)\nu_\psi(s), from the forward pass; the chain rule carries it into the network:

∇ψL=∑s(νD(s)−νψ(s)) ∇ψrψ(s)\nabla_\psi\mathcal L = \sum_{s}\big(\nu_{\data}(s) - \nu_\psi(s)\big)\,\nabla_\psi r_\psi(s)
The reward is now flexible, but the expectation still needs exact planning, so the state space must stay small enough for the backward and forward passes.

Guided cost learning (Finn, Levine & Abbeel, 2016) drops the planning. With a learned cost cθ(τ)c_\theta(\tau) and the model pθ(τ)=e−cθ(τ)/Zp_\theta(\tau) = e^{-c_\theta(\tau)}/Z, it estimates ZZ by importance sampling from trajectories τj\tau_j of a sampling policy qq:

LIOC(θ)=1N∑τi∈Ddemocθ(τi)+log⁡1M∑τj∼qe−cθ(τj)q(τj)\mathcal L_{\mathrm{IOC}}(\theta) = \frac1N\sum_{\tau_i\in\data_{\text{demo}}}c_\theta(\tau_i) + \log\frac1M\sum_{\tau_j\sim q}\frac{e^{-c_\theta(\tau_j)}}{q(\tau_j)}
The sampler is a policy trained to minimize the current cost while keeping its entropy, which moves it toward the ideal importance distribution ∝e−cθ\propto e^{-c_\theta}. Samples from several past samplers are weighted under their mixture; Finn et al. show that dropping the importance weights, as some earlier work did, gives an inconsistent estimate.
DerivationSample-based maximum-entropy IRL is a GAN (Finn, Christiano, Abbeel & Levine, 2016)
  1. Dθ(τ)=1Ze−cθ(τ)1Ze−cθ(τ)+q(τ)D_\theta(\tau) = \frac{\tfrac1Z e^{-c_\theta(\tau)}}{\tfrac1Z e^{-c_\theta(\tau)} + q(\tau)}

    Give the discriminator the generator's density q(τ)q(\tau) and a learned normalizer ZZ, and let it output the probability that a trajectory is a demonstration:

  2. Finn et al. show that training this discriminator by logistic regression, demonstrations against generated trajectories, optimizes the maximum-entropy IRL cost objective, with the partition function estimated by importance sampling from a mixture of demonstrations and samples.

  3. Eτ∼q[log⁡(1−Dθ(τ))−log⁡Dθ(τ)]=Eτ∼q[log⁡q(τ)+log⁡Z+cθ(τ)]=log⁡Z+Eq[cθ(τ)]−H(q)\E_{\tau\sim q}\Big[\log\big(1 - D_\theta(\tau)\big) - \log D_\theta(\tau)\Big] = \E_{\tau\sim q}\big[\log q(\tau) + \log Z + c_\theta(\tau)\big] = \log Z + \E_q\big[c_\theta(\tau)\big] - \mathcal H(q)

    The generator's GAN loss, computed directly from the form of DθD_\theta, is the maximum-entropy RL objective for the current cost: the expected cost minus the generator's entropy, plus a constant.

So the sample-based form of maximum-entropy IRL is a GAN whose discriminator knows the generator's density. AIRL applies the same construction to single state–action pairs, with the policy's probability π(a∣s)\pi(a\mid s) in place of q(τ)q(\tau), which lowers the variance that whole trajectories bring (Adversarial Imitation: GAIL & Distribution Matching).

Key takeaways

  • The maximum-entropy distribution matching expected feature counts is pθ(τ)∝exp⁡(θ⊤f(τ))p_{\boldsymbol\theta}(\tau)\propto\exp\big(\boldsymbol\theta^\top\mathbf f(\tau)\big); its dual is maximum likelihood, concave with Hessian −Cov⁡[f]-\Cov[\mathbf f], and its gradient is f~−Eθ[f]\tilde{\mathbf f} - \E_{\boldsymbol\theta}[\mathbf f].
  • Under stochastic dynamics the trajectory model lets outcomes influence the preceding action, so indifferent demonstrators look risk-seeking; maximum causal entropy gives πθ∝eQθ\pi_{\boldsymbol\theta}\propto e^{Q_{\boldsymbol\theta}} with the soft Bellman recursion Q=θ⊤ϕ+E[V′]Q = \boldsymbol\theta^\top\boldsymbol\phi + \E[V'], V=log⁡∑aeQV = \log\sum_a e^{Q}.
  • Each gradient step solves a soft planning problem backward and propagates expected visits forward, at cost O(T∣S∣∣A∣b)\order{T\abs{\mathcal S}\abs{\mathcal A}b}; computations belong in log space.
  • Deep maximum-entropy IRL backpropagates visit differences into a neural reward; guided cost learning estimates the partition function by importance sampling from an adaptively trained policy, which is exactly a GAN whose discriminator knows the generator's density.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

At the maximum-likelihood θ\boldsymbol\theta of maximum-entropy IRL, what holds?

  1. The model assigns probability 1 to the demonstrated trajectories
  2. The model's expected feature counts equal the demonstrations' average feature counts
  3. The demonstrated trajectories have the highest reward under θ\boldsymbol\theta
  4. θ=0\boldsymbol\theta = 0, because entropy is maximal there
Show the answer

B. The model's expected feature counts equal the demonstrations' average feature counts

The gradient is f~−Epθ[f]\tilde{\mathbf f} - \E_{p_{\boldsymbol\theta}}[\mathbf f], so it vanishes exactly when the feature expectations match, which is the constraint of the maximum-entropy problem. By the feature-matching argument, the model is then as good as the demonstrator under every reward linear in the features.

Question 2

Why is the maximum-entropy IRL log-likelihood concave in θ\boldsymbol\theta (for rewards linear in the features)?

  1. Because the entropy is concave
  2. Because the dynamics are deterministic
  3. Because the feature counts are nonnegative
  4. Because its Hessian is −Cov⁡pθ[f(τ)]-\Cov_{p_{\boldsymbol\theta}}[\mathbf f(\tau)], and covariance matrices are positive semidefinite
Show the answer

D. Because its Hessian is −Cov⁡pθ[f(τ)]-\Cov_{p_{\boldsymbol\theta}}[\mathbf f(\tau)], and covariance matrices are positive semidefinite

The log-likelihood is linear in θ\boldsymbol\theta minus log⁡Z(θ)\log Z(\boldsymbol\theta), and log⁡Z\log Z is the cumulant function of an exponential family, whose Hessian is the covariance of the sufficient statistics. Concavity is what makes simple gradient ascent find the global optimum.

Question 3

Why did Ziebart, Bagnell and Dey replace the 2008 trajectory model with maximum causal entropy for stochastic dynamics?

  1. Because the trajectory model lets the outcomes of transitions raise or lower the probability of the actions before them, so the modeled agent behaves as if it could choose its luck
  2. Because the trajectory model cannot represent deterministic dynamics
  3. Because maximum causal entropy needs no model of the dynamics
  4. Because the trajectory model's likelihood is not concave
Show the answer

A. Because the trajectory model lets the outcomes of transitions raise or lower the probability of the actions before them, so the modeled agent behaves as if it could choose its luck

Weighting each full trajectory by erewarde^{\text{reward}} aggregates random outcomes optimistically (log⁡EeR≥ER\log\E e^{R}\ge\E R), so in the gamble example an indifferent demonstrator appears to prefer gambling 79% of the time. The causal model conditions each action only on the past and backs up the expected soft value.

Question 4

In the GAN view of sample-based maximum-entropy IRL, with Dθ(τ)=1Ze−cθ/(1Ze−cθ+q)D_\theta(\tau) = \tfrac1Z e^{-c_\theta}/(\tfrac1Z e^{-c_\theta} + q), what does the generator minimize?

  1. The probability that the discriminator labels its samples correctly, and nothing else
  2. The cost cθc_\theta alone, with no entropy term
  3. log⁡Z+Eq[cθ(τ)]−H(q)\log Z + \E_q[c_\theta(\tau)] - \mathcal H(q): the maximum-entropy RL objective for the current cost
  4. The KL divergence from the demonstrations to its samples, DKL(pdemo ∥ q)\KL(p_{\text{demo}}\,\|\,q)
Show the answer

C. log⁡Z+Eq[cθ(τ)]−H(q)\log Z + \E_q[c_\theta(\tau)] - \mathcal H(q): the maximum-entropy RL objective for the current cost

Plugging the special form of DθD_\theta into the generator's loss Eq[log⁡(1−D)−log⁡D]\E_q[\log(1-D) - \log D] gives Eq[log⁡q+log⁡Z+cθ]\E_q[\log q + \log Z + c_\theta]. The generator therefore does entropy-regularized RL on the current cost, and the discriminator update is the cost update of maximum-entropy IRL.

Further reading

  • B. D. Ziebart, A. Maas, J. A. Bagnell & A. K. Dey, "Maximum entropy inverse reinforcement learning," AAAI (2008).
  • B. D. Ziebart, J. A. Bagnell & A. K. Dey, "Modeling interaction via the principle of maximum causal entropy," ICML (2010).
  • M. Wulfmeier, P. Ondruska & I. Posner, "Maximum entropy deep inverse reinforcement learning," arXiv:1507.04888 (2015).
  • C. Finn, S. Levine & P. Abbeel, "Guided cost learning: Deep inverse optimal control via policy optimization," ICML (2016).
  • C. Finn, P. Christiano, P. Abbeel & S. Levine, "A connection between generative adversarial networks, inverse reinforcement learning, and energy-based models," arXiv:1611.03852 (2016).