Lecture 19 · Part 5

Control as Inference


Levine reframes the control problem as probabilistic inference in a graphical model with binary optimality variables, where the trajectory posterior weights paths by exponentiated reward. Exact inference gives a soft value iteration — softmax in place of max — but with an optimism bias under stochastic dynamics; variational inference fixes it and lands exactly on maximum-entropy RL, whose practical faces are soft Q-learning and soft actor-critic.

Why hard optimality can’t explain real behavior

The lecture opens with a question about modeling: does optimal control provide a reasonable model of human and animal behavior? A monkey moving a cursor to a target usually reaches it — but by a different route each time, sometimes lazily indirect. The mistakes aren’t uniform noise: with practice they “tend to occur in ways that matter less for the success of the task.” The monkey knows the manner of reaching the goal barely affects the reward, so it lets that part be sloppy.

The RL framework we’ve built cannot express this. For any objective that is linear in the state–action marginal, every fully observed MDP admits a deterministic optimal policy — so randomness can never be rational under hard optimality. Yet natural agents faced with the same situation twice don’t do the same thing, and “good behavior is still the most likely” without being the only behavior. We need a probabilistic model of near-optimal behavior, and Levine flags three reasons to want one: it explains sub-optimal demonstrations (crucial for the inverse RL lecture that follows), it lets us attack control with the whole toolbox of inference algorithms, and it explains why stochastic behavior helps exploration and transfer.

A graphical model in which inference is control

Take the usual MDP chain of states and actions with transitions p(st+1st,at)p(s_{t+1} \mid s_t, a_t) — that alone carries “no assumption of optimal behavior.” To encode why an agent picks one action over another, add one node per time step:

The choice looks arbitrary — Levine admits as much twice — but it earns its keep immediately. Bayes’ rule on the trajectory posterior gives

p(τO1:T)p(τ)texp(r(st,at))=p(τ)exp ⁣(tr(st,at))p(\tau \mid \mathcal{O}_{1:T}) \propto p(\tau) \prod_{t} \exp(r(s_t, a_t)) = p(\tau)\, \exp\!\left(\sum_t r(s_t, a_t)\right)

where p(τ)p(\tau) carries only the dynamics and initial state. With deterministic dynamics, p(τ)p(\tau) is an indicator of physical consistency, and the picture is exactly the sloppy monkey: the highest-reward trajectory is the most likely, equal- reward routes are chosen at random, and probability decays exponentially as reward drops — so the agent will wander on the way to the goal but will not fail to reach it.

Exact inference: backward messages and soft value functions

Three inference problems matter in this chain-structured model, and they’re solved the way you’d solve an HMM — by message passing. The backward message βt(st,at)=p(Ot:Tst,at)\beta_t(s_t, a_t) = p(\mathcal{O}_{t:T} \mid s_t, a_t) is the probability of being optimal from now to the end. Insert st+1s_{t+1} and integrate it out; conditional independence of future optimality variables given st+1s_{t+1} factorizes the result into the transition, the current exp(r)\exp(r) term, and the next message, giving the recursion (run backward from βT=p(OTsT,aT)\beta_T = p(\mathcal{O}_T \mid s_T, a_T)):

βt(st,at)=p(Otst,at)Est+1p(st+1st,at) ⁣[βt+1(st+1)],βt(st)=Eatp(atst) ⁣[βt(st,at)]\beta_t(s_t, a_t) = p(\mathcal{O}_t \mid s_t, a_t)\, \mathbb{E}_{s_{t+1} \sim p(s_{t+1} \mid s_t, a_t)}\!\left[\beta_{t+1}(s_{t+1})\right], \qquad \beta_t(s_t) = \mathbb{E}_{a_t \sim p(a_t \mid s_t)}\!\left[\beta_t(s_t, a_t)\right]

The action prior p(atst)p(a_t \mid s_t) — how likely actions are before you know whether the agent is optimal, emphatically not the policy — is assumed uniform, and this loses nothing: any non-uniform prior can be folded into the reward as r~=r+logp(atst)\tilde{r} = r + \log p(a_t \mid s_t) and the machinery rerun with a uniform prior.

Now define Vt(st)=logβt(st)V_t(s_t) = \log \beta_t(s_t) and Qt(st,at)=logβt(st,at)Q_t(s_t, a_t) = \log \beta_t(s_t, a_t) and the recursion becomes eerily familiar:

Vt(st)=logexp(Qt(st,at))dat,Qt(st,at)=r(st,at)+logE ⁣[exp(Vt+1(st+1))]V_t(s_t) = \log \int \exp(Q_t(s_t, a_t))\, da_t, \qquad Q_t(s_t, a_t) = r(s_t, a_t) + \log \mathbb{E}\!\left[\exp(V_{t+1}(s_{t+1}))\right]

The first equation is a log-sum-exp — a soft max: as the QQ values grow, it approaches maxaQt\max_a Q_t. Value iteration’s hard max over actions has been softened, “which kind of makes sense — we want to soften our notion of optimality so that slightly sub-optimal things are still possible.” The second equation is a Bellman backup when the dynamics are deterministic (one term in the expectation, log and exp cancel), but under stochastic dynamics the log-expected-exp is dominated by the luckiest next state — an optimism problem we’ll return to.

The policy is the posterior p(atst,O1:T)p(a_t \mid s_t, \mathcal{O}_{1:T}). Past optimality variables are d-separated by sts_t, and applying Bayes’ rule to numerator and denominator simultaneously collapses everything into a ratio of messages:

π(atst)=βt(st,at)βt(st)=exp(Qt(st,at)Vt(st))=exp(At(st,at))\pi(a_t \mid s_t) = \frac{\beta_t(s_t, a_t)}{\beta_t(s_t)} = \exp(Q_t(s_t, a_t) - V_t(s_t)) = \exp(A_t(s_t, a_t))

Forward messages αt(st)=p(stO1:t1)\alpha_t(s_t) = p(s_t \mid \mathcal{O}_{1:t-1}) come from the mirror-image recursion, and their product with backward messages gives state marginals: p(stO1:T)βt(st)αt(st)p(s_t \mid \mathcal{O}_{1:T}) \propto \beta_t(s_t)\, \alpha_t(s_t). Picture two cones — one radiating backward from the goal (states from which you can still reach it), one forward from the start (states reachable with high reward) — with near-optimal behavior living in their intersection. Human reaching experiments actually show this cigar-shaped state distribution: tight at start and goal, widest in the middle. Forward messages aren’t needed for the policy, but they are the workhorse of inverse RL in the next lecture.

The optimism problem, and the variational fix

What we actually want is “given that you obtained high reward, what was your action probability, given that your transition probabilities did not change?” That is a variational inference problem: find q(s1:T,a1:T)q(s_{1:T}, a_{1:T}) close to the posterior p(s1:T,a1:TO1:T)p(s_{1:T}, a_{1:T} \mid \mathcal{O}_{1:T}) but constrained to the true dynamics. The trick is a peculiar variational family where most of qq is fixed to match pp:

q(s1:T,a1:T)=p(s1)tp(st+1st,at)q(atst)q(s_{1:T}, a_{1:T}) = p(s_1) \prod_t p(s_{t+1} \mid s_t, a_t)\, q(a_t \mid s_t)

Only the action conditional is learned. With x=O1:Tx = \mathcal{O}_{1:T} and z=(s1:T,a1:T)z = (s_{1:T}, a_{1:T}), this is exactly the q(z)p(zx)q(z) \approx p(z \mid x) setup from the variational inference lecture, so write the evidence lower bound:

Optimizing the bound by dynamic programming from the last step backward uses one recurring fact: an objective of the form Eq[something]Eq[logq]\mathbb{E}_q[\text{something}] - \mathbb{E}_q[\log q] is maximized by qexp(something)q \propto \exp(\text{something}). Each step therefore gives q(atst)=exp(Qt(st,at)Vt(st))q(a_t \mid s_t) = \exp(Q_t(s_t, a_t) - V_t(s_t)) with VV the log-integral-exp normalizer — and, crucially, the backup for QQ is now the regular Bellman backup Qt=r+E[Vt+1]Q_t = r + \mathbb{E}[V_{t+1}], “not optimistic anymore.” The result is soft value iteration: identical to value iteration except V(s)soft maxaQ(s,a)V(s) \leftarrow \operatorname{soft\,max}_a Q(s, a) replaces the hard max. Variants follow mechanically: a discount γ\gamma (equivalently a 1γ1-\gamma chance of death), an explicit temperature, and an infinite-horizon version that just runs the backup to convergence.

Algorithms: soft Q-learning, entropy-regularized policy gradients

The variational picture instantiates directly as deep RL algorithms.

Alternatively, attack the bound directly with policy gradients: J(θ)=tEπθ[r(st,at)]+H(πθ(atst))J(\theta) = \sum_t \mathbb{E}_{\pi_\theta}[r(s_t, a_t)] + \mathcal{H}(\pi_\theta(a_t \mid s_t)) is the standard objective plus an entropy bonus — the widely used entropy-regularized policy gradient, which combats premature collapse of policy entropy (on-policy exploration lives entirely in that stochasticity). Its gradient has a pleasant surprise: differentiating through the entropy contributes a constant 1-1 inside the parentheses, which the baseline property lets you drop — so the entire modification is subtracting logπ\log \pi from the reward, nothing more. Substituting logπ=QV\log \pi = Q - V and crunching further, the entropy-regularized policy gradient comes out looking almost exactly like the soft Q-learning update — differing only in an extra V-\nabla V term on one side and a soft max on the other — a striking structural connection between the two families.

The practical culmination is soft actor-critic: an off-policy actor-critic where the critic update is the ordinary policy Q-function backup with a logπ-\log \pi entropy term added to the target, and the actor update maximizes E[Q^ϕπ(s,a)]+H(πθ)\mathbb{E}[\hat{Q}^\pi_\phi(s, a)] + \mathcal{H}(\pi_\theta). In variational language, the critic step is message passing in the variational family and the actor step fits qq to the posterior; in practice it’s a simple, very effective algorithm — “one of the most widely used off-policy continuous control algorithms today.”

What soft optimality buys you

Levine closes with evidence that this is more than a modeling nicety. An ant robot facing two passages to a goal: a conventional algorithm commits early to whichever passage its Q-function happens to rank higher, and can commit wrong; a policy proportional to exp(Q)\exp(Q) keeps probability mass on both peaks until one is clearly better (Haarnoja et al., soft Q-learning with deep energy-based policies). Pre-trained with the underspecified reward “run fast in any direction,” a soft-Q ant runs in every direction — entropy demands it — and then fine-tunes into a specific corridor far faster than DDPG, which must first unlearn its one committed direction. Soft actor-critic trained a real robot arm to stack Lego blocks robustly under physical perturbation, and taught the Minitaur quadruped a real-world walking gait that generalized to slopes and stairs it never trained on.

The suggested readings trace the lineage: Todorov’s linearly solvable MDPs and human motor control work, Kappen’s “optimal control as graphical model inference,” Ziebart’s maximum-entropy inverse RL, the soft Q-learning and soft actor-critic papers, and Levine’s 2018 tutorial “Reinforcement Learning and Control as Probabilistic Inference.”

Check yourself

Derivation-style exercises in the lecture’s spirit — work them on paper.

Problem 19.1 trajectory posterior

Starting from the graphical model with p(Otst,at)=exp(r(st,at))p(\mathcal{O}_t \mid s_t, a_t) = \exp(r(s_t, a_t)), derive p(τO1:T)p(τ)exp(tr(st,at))p(\tau \mid \mathcal{O}_{1:T}) \propto p(\tau) \exp(\sum_t r(s_t, a_t)). Then specialize to deterministic dynamics: for two feasible trajectories τ1,τ2\tau_1, \tau_2 with total rewards R1>R2R_1 > R_2, what is the ratio of their posterior probabilities, and what does this predict about the monkey choosing among equal-reward routes?

Show solution

By the definition of conditional probability, p(τO1:T)=p(τ,O1:T)/p(O1:T)p(\tau \mid \mathcal{O}_{1:T}) = p(\tau, \mathcal{O}_{1:T}) / p(\mathcal{O}_{1:T}). The numerator factorizes over the model’s CPDs: p(τ)tp(Otst,at)=p(τ)texp(r(st,at))p(\tau) \prod_t p(\mathcal{O}_t \mid s_t, a_t) = p(\tau) \prod_t \exp(r(s_t, a_t)), and a product of exponentials is the exponential of the sum, giving p(τ)exp(tr)p(\tau)\exp(\sum_t r); the denominator is a constant in τ\tau, hence the proportionality. With deterministic dynamics p(τ)p(\tau) is an indicator of physical consistency, so for two feasible trajectories the ratio is p(τ1)/p(τ2)=exp(R1R2)p(\tau_1 \mid \cdot)/p(\tau_2 \mid \cdot) = \exp(R_1 - R_2) — probability decays exponentially in the reward gap. If R1=R2R_1 = R_2 the ratio is 1: equal-reward routes are chosen uniformly at random, which is exactly the observed variety in how the monkey reaches the target while still (almost) never missing it, since missing costs a large reward gap.

Problem 19.2 softmax backup

Define Vt(st)=logβt(st)V_t(s_t) = \log \beta_t(s_t) and Qt(st,at)=logβt(st,at)Q_t(s_t, a_t) = \log \beta_t(s_t, a_t). (a) Derive Vt(st)=logexp(Qt(st,at))datV_t(s_t) = \log \int \exp(Q_t(s_t, a_t))\, da_t from the message recursion with a uniform action prior. (b) For discrete actions with values Q=(10,9,2)Q = (10, 9, 2), compute the soft max and compare it to the hard max; repeat for 10×Q10 \times Q. What does this say about the effect of reward magnitude?

Show solution

(a) The state message is βt(st)=Eatp(atst)[βt(st,at)]\beta_t(s_t) = \mathbb{E}_{a_t \sim p(a_t \mid s_t)}[\beta_t(s_t, a_t)]; with a uniform prior this is a constant times βt(st,at)dat\int \beta_t(s_t, a_t)\, da_t. Taking logs and dropping the additive constant (it shifts all values equally and cancels in the policy exp(QV)\exp(Q - V)), Vt=logexp(Qt)datV_t = \log \int \exp(Q_t)\, da_t. (b) log(e10+e9+e2)=10+log(1+e1+e8)10.31\log(e^{10} + e^9 + e^2) = 10 + \log(1 + e^{-1} + e^{-8}) \approx 10.31, versus a hard max of 10 — the soft max exceeds the max slightly, dominated by the largest term. For (100,90,20)(100, 90, 20): 100+log(1+e10+e80)100.00005100 + \log(1 + e^{-10} + e^{-80}) \approx 100.00005, essentially the hard max. Scaling rewards up (equivalently, lowering the temperature) makes the soft max converge to the hard max, which is why soft optimality “reduces to classical hard optimality as the reward magnitude increases.”

Problem 19.3 optimism bias

An action leads to a jackpot state with Vt+1=10V_{t+1} = 10 with probability 0.0010.001 and to a state with Vt+1=0V_{t+1} = 0 otherwise. Compare the exact-inference backup logE[exp(Vt+1)]\log \mathbb{E}[\exp(V_{t+1})] with the standard backup E[Vt+1]\mathbb{E}[V_{t+1}]. Which inference question does the first answer, and how does the variational formulation change the backup?

Show solution

Standard: E[Vt+1]=0.001×10=0.01\mathbb{E}[V_{t+1}] = 0.001 \times 10 = 0.01 — nearly worthless. Exact inference: log(0.001e10+0.999e0)=log(22.03+0.999)log(23.03)3.14\log(0.001\, e^{10} + 0.999\, e^0) = \log(22.03 + 0.999) \approx \log(23.03) \approx 3.14 — hundreds of times larger, dominated entirely by the rare lucky outcome. The log-exp backup answers “given that you obtained high reward, what happened?” — and conditioned on success, the posterior transition probability of the jackpot state is inflated: the model changes the dynamics to agree with the evidence, which real dynamics are not allowed to do. The variational formulation fixes the dynamics inside qq to the true p(st+1st,at)p(s_{t+1} \mid s_t, a_t), and the resulting dynamic program replaces the backup with the ordinary Qt=r+E[Vt+1]Q_t = r + \mathbb{E}[V_{t+1}], scoring the action at its honest 0.010.01 while keeping the soft max over actions (which is the part we actually wanted).

Problem 19.4 ELBO cancellation

Write out logp(O1:T)Ezq[logp(x,z)logq(z)]\log p(\mathcal{O}_{1:T}) \ge \mathbb{E}_{z \sim q}[\log p(x, z) - \log q(z)] for x=O1:Tx = \mathcal{O}_{1:T}, z=(s1:T,a1:T)z = (s_{1:T}, a_{1:T}), and q=p(s1)tp(st+1st,at)q(atst)q = p(s_1) \prod_t p(s_{t+1} \mid s_t, a_t)\, q(a_t \mid s_t). Show exactly which terms cancel and why the specific choice of variational family is what makes the bound collapse to reward plus entropy.

Show solution

logp(x,z)=logp(s1)+tlogp(st+1st,at)+tlogp(Otst,at)\log p(x, z) = \log p(s_1) + \sum_t \log p(s_{t+1} \mid s_t, a_t) + \sum_t \log p(\mathcal{O}_t \mid s_t, a_t) and logq(z)=logp(s1)+tlogp(st+1st,at)+tlogq(atst)\log q(z) = \log p(s_1) + \sum_t \log p(s_{t+1} \mid s_t, a_t) + \sum_t \log q(a_t \mid s_t). Because qq was defined with the same initial-state distribution and the same transitions as pp, the logp(s1)\log p(s_1) terms and every transition term appear identically in both and cancel in the difference. What remains is Eq[tlogp(Otst,at)logq(atst)]\mathbb{E}_q[\sum_t \log p(\mathcal{O}_t \mid s_t, a_t) - \log q(a_t \mid s_t)], and substituting logp(Otst,at)=r(st,at)\log p(\mathcal{O}_t \mid s_t, a_t) = r(s_t, a_t) and using linearity gives tE(st,at)q[r(st,at)+H(q(atst))]\sum_t \mathbb{E}_{(s_t, a_t) \sim q}[r(s_t, a_t) + \mathcal{H}(q(a_t \mid s_t))]. Had we let the variational family learn its own dynamics, the transition terms would not cancel, the bound would contain dynamics ratios, and — worse — the optimizer could exploit them, reintroducing exactly the optimism the constraint was built to remove. Fixing the dynamics is simultaneously what simplifies the algebra and what makes the answer the true expected reward under real transitions.

Problem 19.5 action prior folding

Levine claims a non-uniform action prior p(atst)p(a_t \mid s_t) can be absorbed into the reward, so assuming a uniform prior loses no generality. Prove it: show that running the backward pass with prior p(atst)p(a_t \mid s_t) and reward rr gives the same value function as running it with a uniform prior and reward r~(st,at)=r(st,at)+logp(atst)\tilde{r}(s_t, a_t) = r(s_t, a_t) + \log p(a_t \mid s_t).

Show solution

With a non-uniform prior, the state message is βt(st)=p(atst)βt(st,at)dat\beta_t(s_t) = \int p(a_t \mid s_t)\, \beta_t(s_t, a_t)\, da_t, so in log space Vt(st)=logexp ⁣(Qt(st,at)+logp(atst))datV_t(s_t) = \log \int \exp\!\big(Q_t(s_t, a_t) + \log p(a_t \mid s_t)\big) da_t where Qt=r+logE[exp(Vt+1)]Q_t = r + \log \mathbb{E}[\exp(V_{t+1})]. Define Q~t(st,at)=r(st,at)+logp(atst)+logE[exp(Vt+1)]\tilde{Q}_t(s_t, a_t) = r(s_t, a_t) + \log p(a_t \mid s_t) + \log \mathbb{E}[\exp(V_{t+1})] — i.e., the QQ you would compute from reward r~=r+logp(atst)\tilde{r} = r + \log p(a_t \mid s_t). Then Qt+logp(atst)=Q~tQ_t + \log p(a_t \mid s_t) = \tilde{Q}_t exactly, so Vt(st)=logexp(Q~t)datV_t(s_t) = \log \int \exp(\tilde{Q}_t)\, da_t: the same log-sum-exp a uniform-prior backward pass would produce from r~\tilde{r} (up to the constant a uniform prior contributes, which cancels in the policy). Since the VV‘s agree at every step by backward induction (the base case TT is the same identity), the two runs produce identical value functions and identical policies π=exp(Q~V)\pi = \exp(\tilde{Q} - V). Hence any prior preference — e.g., a lazy monkey favoring small actions — is just a reward shaping term, and the uniform-prior assumption is fully general.