Control as Inference
RL as probabilistic inference: optimality variables, the evidence lower bound and MPO.
What if "act optimally" were an observation? Attach a binary optimality variable to every step, with probability , condition on all of them being 1, and ask a probabilistic graphical model for the posterior over actions. Out come soft value functions and Boltzmann policies, and, when the dynamics are stochastic, an agent that believes it can make its own luck. This lesson derives exact inference and its optimism, the variational fix that recovers maximum-entropy RL as an evidence lower bound, the EM-style algorithms built on it, from MPO to advantage-weighted regression, and the critique that inference about optimality is not exploration.
What you’ll learn
- Define the optimality-variable model and derive its backward messages, soft values and posterior policy by exact inference.
- Show that exact inference with stochastic dynamics backs up , a risk-seeking value, and explain it through the posterior dynamics.
- Derive maximum-entropy RL as the evidence lower bound of a variational family whose dynamics are held fixed.
- Derive MPO's E-step (a KL-constrained, nonparametric improvement with a convex dual) and M-step, and relate them to REPS, AWR, V-MPO and KL-regularized fine-tuning of language models.
- Explain why inference about optimal actions does not provide deep exploration (O'Donoghue, Osband & Ionescu, 2020).
Optimality as evidence
Follow the inference literature and take a finite horizon with undiscounted rewards (a discount is the same model with an absorbing "end" state entered with probability at every step). Control as inference adds a binary optimality variable to each step, with likelihood
where is a temperature. For this to be a probability the rewards must be at most 0; subtract the largest reward, or read as an unnormalized potential, and nothing below changes. With a uniform prior over actions (any other prior can be folded into the reward), conditioning on gives a posterior over trajectories:
With deterministic dynamics the bracket only says which trajectories are feasible, so the most probable trajectory is the reward-maximizing plan: planning is maximum a posteriori inference. The idea has several roots: Todorov's (2008) duality between optimal control and estimation, Toussaint's (2009) trajectory optimization by approximate inference, Kappen, Gómez and Opper's (2012) optimal control as graphical-model inference, Rawlik, Toussaint and Vijayakumar's (2012) approximate inference control, and Ziebart's (2010) principle of maximum causal entropy, which also founded maximum-entropy inverse RL (Maximum-Entropy Inverse RL). Levine (2018) gives the unified account this lesson follows.
Exact inference, and an agent that makes its own luck
As in a hidden Markov model, the posterior policy needs only backward messages, and , computed from down to 1 with . In log space they are value functions.
Condition on the next state; the optimality of step and of the rest of the trajectory are independent given it:
Define and . The recursion becomes a backup, up to a constant per step from the uniform prior, which does not affect the policy:
By Bayes' rule the posterior policy is the ratio of messages: a Boltzmann policy over the soft values, as in maximum-entropy RL.
With deterministic dynamics, and the backup is the soft Bellman equation. With stochastic dynamics it is not: by Jensen's inequality , a soft maximum over next states. One lucky outcome can dominate the value of a risky action.
The reason is in the posterior dynamics. Conditioned on optimality, the next state is distributed as : given that the trajectory turned out optimal, the good outcomes must have happened. The objective exact inference optimizes contains terms for changing the dynamics, so the inferred agent behaves as if it could bend the world toward good outcomes at a KL cost (Levine, 2018). Under the posterior's logic, a lottery ticket is a sound purchase. In risk-sensitive terms, is an exponential-utility certainty equivalent with a risk-seeking sign.
A gamble pays 0.9 with probability 0.9 and 10.9 with probability 0.1 (mean 1.9). With , what value does the exact-inference backup assign to it?
Show the answer
8.6
, far above the mean of 1.9 and close to the jackpot. The posterior puts probability on winning. A larger temperature tempers the optimism: at the value is and the posterior probability of the jackpot about 0.23.
Variational inference recovers maximum-entropy RL
The fix is to stop the agent from choosing its luck. Approximate the posterior with a structured variational family that keeps the true start distribution and dynamics and lets only the policy vary, , and maximize the evidence lower bound.
Write the evidence as an expectation under and apply Jensen's inequality:
Expand both log-densities. The start distribution and the dynamics appear in both and cancel, because uses the true ones; the uniform action prior leaves a constant:
Multiplied by , the bound is the maximum-entropy objective, plus a constant. The gap is , the distance from the optimistic posterior.
Optimizing this bound over by dynamic programming gives the soft Bellman equation with an expectation over next states, with , exactly the backup of Maximum-Entropy RL & Soft Actor–Critic. Freezing the dynamics in the variational family restores risk neutrality toward the environment's randomness; with deterministic dynamics, exact and variational inference coincide. The bound also accommodates any policy class, such as Gaussians or networks that see only partial observations.
Why does restricting the variational family to the true dynamics remove the optimism of exact inference?
- Because it changes the reward function to a risk-averse one
- Because the posterior over trajectories reweights next states by , and a with the true dynamics cannot follow that reweighting, so only actions are optimized and next states are averaged with their true probabilities
- Because the evidence lower bound is always smaller than the true value
- Because it lowers the temperature to zero
Show the answer
B. Because the posterior over trajectories reweights next states by , and a with the true dynamics cannot follow that reweighting, so only actions are optimized and next states are averaged with their true probabilities
Exact inference matches the posterior, which tilts transitions toward good outcomes. The restricted family cannot tilt them: the dynamics terms cancel in the bound, and the best maximizes expected reward plus entropy under the real dynamics, a risk-neutral soft Bellman equation.
EM for policies: MPO and its relatives
Replace the uniform action prior by the current policy and the bound becomes a KL-regularized objective, , with a prior on the parameters. MPO, maximum a posteriori policy optimisation (Abdolmaleki, Springenberg, Tassa, Munos, Heess & Riedmiller, 2018), maximizes it by coordinate ascent, as in expectation–maximization: an E-step improves with the policy fixed, an M-step fits the policy to . Both use the old policy's critic learned off-policy from replay, and both are written as hard KL constraints, because a penalty's scale relative to the rewards is hard to choose.
Over replayed states , improve the action distribution while staying within of the old policy on average:
With a Lagrange multiplier for the constraint, each state's problem is the Gibbs variational principle with the old policy as reference: a tilted version of the old policy.
The temperature is not a hyperparameter but the minimizer of the convex dual, estimated with actions sampled from :
import math
import torch
def mpo_e_step(q_values, eps=0.1, iters=200):
"""q_values: (B, K) critic values of K actions sampled from pi_old in each of B states.
Returns the nonparametric E-step weights q(a_k | s_b) and the temperature eta."""
log_eta = torch.zeros((), requires_grad=True)
opt = torch.optim.Adam([log_eta], lr=0.05)
K = q_values.shape[1]
for _ in range(iters): # minimize the convex dual g(eta)
eta = log_eta.exp()
log_mean_exp = torch.logsumexp(q_values / eta, dim=1) - math.log(K)
g = eta * eps + eta * log_mean_exp.mean()
opt.zero_grad()
g.backward()
opt.step()
eta = log_eta.exp().detach()
return torch.softmax(q_values / eta, dim=1), eta
# M-step: maximize sum over b, k of weights[b, k] * log pi_theta(a_k | s_b),
# subject to an average KL(pi_old || pi_theta) below a second bound.Because the actions were sampled from , the tilted distribution restricted to them is just a softmax of over each state's samples. A larger gives a smaller and sharper weights.
The M-step is supervised learning: weighted maximum likelihood, , subject to below a second bound. The two KL divergences point in opposite directions: mode-seeking in the E-step, moment-matching in the M-step, which the authors found keeps the parametric policy's entropy from collapsing, and the M-step constraint greatly increased stability.
The same tilt-then-project structure runs through a family of methods. Relative entropy policy search (REPS; Peters, Mülling & Altun, 2010) solves a similar dual over joint state–action distributions. Reward-weighted regression (Peters & Schaal, 2007) weights actions by exponentiated returns. Advantage-weighted regression (AWR; Peng, Kumar, Zhang & Levine, 2019) fits to replayed actions weighted by , the projection of for a behavior policy ; the baseline proved crucial, and the weights are clipped to avoid exploding gradients. It reappears in offline RL (Conservative Offline RL: BCQ, CQL & IQL). V-MPO (Song et al., 2019) is the on-policy version: advantages from a learned state-value function, only the top half of each batch's advantages in the E-step, a learned temperature and a Lagrange multiplier for the M-step's KL bound; it reported surpassing previously published multi-task scores on Atari-57 and DMLab-30 without importance weighting, entropy regularization or population-based hyperparameter tuning.
Language-model fine-tuning has the same skeleton. The KL-regularized objective is maximized by , an E-step with the reference policy as prior (Direct Preference Optimization & Its Relatives), and Korbak, Perez and Buckley (2022) argue that KL-regularized RL for language models is best understood as variational inference toward that posterior.
Inference about optimality is not exploration
The word "posterior" invites a misreading. The posterior in control as inference is over trajectories given optimality, computed as if the MDP were known. It says nothing about what the agent does not yet know about the MDP, and that epistemic uncertainty is what exploration must resolve. O'Donoghue, Osband and Ionescu (2020) made the gap concrete with a one-state problem with actions: action 1 pays 1, actions 3 to pay , and action 2 pays or , each with prior probability ½.
The Bayes-optimal agent tries action 2 once: if it paid it keeps it, otherwise it switches to action 1. A soft Q-learning agent acts by a Boltzmann policy over expected rewards, in which action 2 is worth 0 and competes with distractors worth nearly 1, so it almost never tries it. In their experiment (, uniform prior) the Bayesian regret was 1.5 for the Bayes-optimal policy, 2 for Thompson sampling and at most 2.2 for K-learning, while soft Q-learning's regret grew linearly in even at the best temperature. K-learning keeps the soft Bellman equation and Boltzmann policy but adds a count-based bonus and a schedule for the temperature, which makes its values optimistic about uncertain actions, and it comes with a Bayesian regret bound.
Key takeaways
- With , the posterior over trajectories is the dynamics reweighted by ; its backward messages are soft values, and the posterior policy is .
- Exact inference with stochastic dynamics backs up : the posterior assumes the lucky outcomes happened, and the agent is risk-seeking (a 10% jackpot of 10.9 against 0.9 is worth about 8.6 at , not 1.9).
- Variational inference with the true dynamics held fixed turns the evidence lower bound into the maximum-entropy objective, and the backup into the risk-neutral soft Bellman equation.
- MPO runs EM on the KL-regularized bound: a closed-form E-step with from a convex dual, then weighted maximum likelihood under a KL constraint; REPS, AWR, V-MPO and the optimum of KL-regularized LM fine-tuning share this tilt-then-project form.
- The posterior given optimality contains no uncertainty about the MDP, so inference-based methods do not explore deeply; optimism or posterior sampling (K-learning, Thompson sampling) must be added.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
In the optimality-variable model with stochastic dynamics, exact inference backs up . What kind of agent does this produce?
- A risk-neutral agent, identical to standard maximum-entropy RL
- A risk-averse agent, since the logarithm is concave
- A risk-seeking agent: the backup is a soft maximum over next states, so a rare high-value outcome can dominate
- An agent that ignores the dynamics entirely
Show the answer
C. A risk-seeking agent: the backup is a soft maximum over next states, so a rare high-value outcome can dominate
By Jensen's inequality the log-expected-exponential is at least the expectation, and it approaches the maximum as . It is an exponential-utility certainty equivalent with a risk-seeking sign, the result of a posterior that assumes the lucky outcomes happened.
With , what is times the evidence lower bound, up to a constant?
- , the standard expected return
- , the maximum-entropy objective
- , a trust-region objective
- , the exact log-evidence
Show the answer
B. , the maximum-entropy objective
The dynamics terms of and cancel, leaving minus a constant from the uniform prior. Multiplying by gives reward plus times entropy.
What does MPO's E-step produce, and where does its temperature come from?
- A tilted version of the old policy, , with the Lagrange multiplier of the average-KL constraint, found by minimizing a convex dual
- The greedy policy , with a learning rate
- A new parametric policy trained by the policy gradient, with an entropy bonus chosen by hand
- A uniform policy over actions whose value exceeds
Show the answer
A. A tilted version of the old policy, , with the Lagrange multiplier of the average-KL constraint, found by minimizing a convex dual
Maximizing expected under a KL budget is the Gibbs variational principle with the old policy as reference. The budget fixes through the dual ; the M-step then fits the parametric policy to by weighted maximum likelihood.
O'Donoghue, Osband and Ionescu (2020) showed that soft Q-learning can perform poorly on a one-state problem with one uncertain action. What is the core reason?
- Its temperature is always too high
- It computes a posterior over trajectories given optimality, as if the MDP were known; its Boltzmann policy over expected rewards almost never tries an uncertain action whose expected value is low, so it does not resolve its own uncertainty
- Soft Bellman equations do not converge in bandit problems
- It is risk-seeking, so it tries the uncertain action too often
Show the answer
B. It computes a posterior over trajectories given optimality, as if the MDP were known; its Boltzmann policy over expected rewards almost never tries an uncertain action whose expected value is low, so it does not resolve its own uncertainty
Exploration requires reasoning about what the agent does not know and about the value of finding out. The inference framework's "posterior" contains no epistemic uncertainty. Thompson sampling and K-learning, which do, achieve small Bayesian regret on the same problem, while soft Q-learning's grows with the number of distractor actions.
Further reading
- S. Levine, "Reinforcement learning and control as probabilistic inference: Tutorial and review," arXiv:1805.00909 (2018).
- B. D. Ziebart, Modeling Purposeful Adaptive Behavior with the Principle of Maximum Causal Entropy, PhD thesis, Carnegie Mellon University (2010).
- A. Abdolmaleki, J. T. Springenberg, Y. Tassa, R. Munos, N. Heess & M. Riedmiller, "Maximum a posteriori policy optimisation," ICLR (2018).
- B. O'Donoghue, I. Osband & C. Ionescu, "Making sense of reinforcement learning and probabilistic inference," ICLR (2020).
- H. F. Song, A. Abdolmaleki, J. T. Springenberg, A. Clark, H. Soyer, J. W. Rae, S. Noury, A. Ahuja et al., "V-MPO: On-policy maximum a posteriori policy optimization for discrete and continuous control," ICLR (2020).