Skip to content
Ahmed Haroon
← Reinforcement Learning

Control as Inference

From Control to Inference

Ordinary optimal control chooses actions that maximize cumulative reward:

a1:T∗=arg⁡max⁡a1:T∑t=1Tr(st,at),a_{1:T}^* =\arg\max_{a_{1:T}} \sum_{t=1}^{T}r(s_t,a_t),

subject to the environment dynamics. This produces a hard distinction between optimal and non-optimal behavior.

But several behaviors may all be good, demonstrations may contain mistakes, and a controller may benefit from remaining stochastic. Control as inference replaces hard optimality with a probabilistic event.

Introduce a binary optimality variable OtO_t at every time step and define

p(Ot=1∣st,at)∝exp⁡(r(st,at)α),\boxed{ p(O_t=1\mid s_t,a_t) \propto \exp\left(\frac{r(s_t,a_t)}{\alpha}\right) },

where α>0\alpha>0 is a temperature. High-reward state-action pairs make optimality more likely, without forcing lower-reward actions to have zero probability.

Before conditioning, let the trajectory prior be

p(τ)=p(s1)∏t=1Tp0(at∣st)∏t=1T−1p(st+1∣st,at),p(\tau) =p(s_1) \prod_{t=1}^{T}p_0(a_t\mid s_t) \prod_{t=1}^{T-1}p(s_{t+1}\mid s_t,a_t),

where p0(a∣s)p_0(a\mid s) is an action prior. The joint distribution is

p(τ,O1:T=1)=p(τ)∏t=1Texp⁡(r(st,at)α).p(\tau,O_{1:T}=1) =p(\tau) \prod_{t=1}^{T} \exp\left(\frac{r(s_t,a_t)}{\alpha}\right).

Therefore,

p(τ∣O1:T=1)∝p(τ)exp⁡(1α∑t=1Tr(st,at)).\boxed{ p(\tau\mid O_{1:T}=1) \propto p(\tau) \exp\left( \frac1\alpha\sum_{t=1}^{T}r(s_t,a_t) \right) }.

The prior keeps trajectories physically plausible. The exponential-return factor assigns greater posterior probability to higher-return trajectories.

For equally likely trajectories τ1\tau_1 and τ2\tau_2,

p(τ1∣O=1)p(τ2∣O=1)=exp⁡(R(τ1)−R(τ2)α).\frac{p(\tau_1\mid O=1)}{p(\tau_2\mid O=1)} =\exp\left( \frac{R(\tau_1)-R(\tau_2)}{\alpha} \right).

A return advantage of ΔR\Delta R multiplies posterior odds by eΔR/αe^{\Delta R/\alpha}.

Backward Inference

Define the state-action backward message

βt(st,at)=p(Ot:T=1∣st,at).\beta_t(s_t,a_t) =p(O_{t:T}=1\mid s_t,a_t).

It asks how likely future behavior is to be optimal after taking ata_t in sts_t. Also define

βt(st)=p(Ot:T=1∣st).\beta_t(s_t)=p(O_{t:T}=1\mid s_t).

The messages satisfy

βt(s,a)=exp⁡(r(s,a)α)Es′∼p(⋅∣s,a)[βt+1(s′)],\boxed{ \beta_t(s,a) =\exp\left(\frac{r(s,a)}{\alpha}\right) \mathbb E_{s'\sim p(\cdot\mid s,a)} [\beta_{t+1}(s')] },

and

βt(s)=Ea∼p0(⋅∣s)[βt(s,a)].\boxed{ \beta_t(s) =\mathbb E_{a\sim p_0(\cdot\mid s)} [\beta_t(s,a)] }.

Take scaled logarithms so the messages have reward units:

Qt(s,a)=αlog⁡βt(s,a),Vt(s)=αlog⁡βt(s).Q_t(s,a)=\alpha\log\beta_t(s,a), \qquad V_t(s)=\alpha\log\beta_t(s).

This produces the exact inference recursions

Qt(s,a)=r(s,a)+αlog⁡Es′∼p(⋅∣s,a)[exp⁡(Vt+1(s′)α)],\boxed{ Q_t(s,a) =r(s,a) +\alpha\log \mathbb E_{s'\sim p(\cdot\mid s,a)} \left[ \exp\left(\frac{V_{t+1}(s')}{\alpha}\right) \right] }, Vt(s)=αlog⁡∫p0(a∣s)exp⁡(Qt(s,a)α)da.\boxed{ V_t(s) =\alpha\log \int p_0(a\mid s) \exp\left(\frac{Q_t(s,a)}{\alpha}\right)da }.
See derivation

Starting from the state-action message,

βt(s,a)=er(s,a)/αEs′[βt+1(s′)],\beta_t(s,a) =e^{r(s,a)/\alpha} \mathbb E_{s'}[\beta_{t+1}(s')],

take logs and multiply by α\alpha:

Qt(s,a)=αlog⁡βt(s,a)=r(s,a)+αlog⁡Es′[βt+1(s′)]=r(s,a)+αlog⁡Es′[eVt+1(s′)/α].\begin{aligned} Q_t(s,a) &=\alpha\log\beta_t(s,a)\\ &=r(s,a) +\alpha\log\mathbb E_{s'} [\beta_{t+1}(s')]\\ &=r(s,a) +\alpha\log\mathbb E_{s'} \left[e^{V_{t+1}(s')/\alpha}\right]. \end{aligned}

For the state message,

Vt(s)=αlog⁡Ea∼p0[βt(s,a)]=αlog⁡∫p0(a∣s)eQt(s,a)/αda.\begin{aligned} V_t(s) &=\alpha\log \mathbb E_{a\sim p_0}[\beta_t(s,a)]\\ &=\alpha\log \int p_0(a\mid s)e^{Q_t(s,a)/\alpha}da. \end{aligned}

For discrete actions with a uniform prior absorbed into the base measure,

Vt(s)=αlog⁡∑aexp⁡(Qt(s,a)α).V_t(s) =\alpha\log\sum_a \exp\left(\frac{Q_t(s,a)}{\alpha}\right).

This log-sum-exp is a smooth maximum. If M=max⁡iQiM=\max_iQ_i, then

M≤αlog⁡∑ieQi/α≤M+αlog⁡n.\boxed{ M \leq \alpha\log\sum_i e^{Q_i/\alpha} \leq M+\alpha\log n }.

Therefore,

lim⁡α→0+αlog⁡∑ieQi/α=max⁡iQi.\lim_{\alpha\to0^+} \alpha\log\sum_i e^{Q_i/\alpha} =\max_iQ_i.

For deterministic dynamics s′=f(s,a)s'=f(s,a), the Q-backup becomes

Qt(s,a)=r(s,a)+Vt+1(f(s,a)),Q_t(s,a)=r(s,a)+V_{t+1}(f(s,a)),

so backward inference is soft dynamic programming.

The Posterior Policy

The inferred policy is the posterior action distribution conditioned on future optimality:

π∗(at∣st)=p(at∣st,Ot:T=1).\pi^*(a_t\mid s_t) =p(a_t\mid s_t,O_{t:T}=1).

Bayes' rule gives

π∗(a∣s)=p0(a∣s)βt(s,a)βt(s).\pi^*(a\mid s) =p_0(a\mid s) \frac{\beta_t(s,a)}{\beta_t(s)}.

Using the log-messages,

π∗(a∣s)=p0(a∣s)exp⁡(Qt(s,a)−Vt(s)α).\boxed{ \pi^*(a\mid s) =p_0(a\mid s) \exp\left( \frac{Q_t(s,a)-V_t(s)}{\alpha} \right) }.

If At(s,a)=Qt(s,a)−Vt(s)A_t(s,a)=Q_t(s,a)-V_t(s), then

π∗(a∣s)=p0(a∣s)eAt(s,a)/α.\pi^*(a\mid s) =p_0(a\mid s)e^{A_t(s,a)/\alpha}.

The policy is normalized because

eVt(s)/α=∫p0(a∣s)eQt(s,a)/αda.e^{V_t(s)/\alpha} =\int p_0(a\mid s)e^{Q_t(s,a)/\alpha}da.

For two actions,

π∗(a1∣s)π∗(a2∣s)=p0(a1∣s)p0(a2∣s)exp⁡(Q(s,a1)−Q(s,a2)α).\frac{\pi^*(a_1\mid s)}{\pi^*(a_2\mid s)} =\frac{p_0(a_1\mid s)}{p_0(a_2\mid s)} \exp\left( \frac{Q(s,a_1)-Q(s,a_2)}{\alpha} \right).

Small α\alpha concentrates probability on maximizing actions. Large α\alpha makes the policy approach its prior:

lim⁡α→0+π∗(a∣s)=greedy policy,\lim_{\alpha\to0^+}\pi^*(a\mid s) =\text{greedy policy}, lim⁡α→∞π∗(a∣s)=p0(a∣s).\lim_{\alpha\to\infty}\pi^*(a\mid s) =p_0(a\mid s).

The action prior has three equivalent interpretations:

  • a default behavior before conditioning on optimality;
  • an implicit reward αlog⁡p0(a∣s)\alpha\log p_0(a\mid s);
  • a reference distribution that the learned policy is penalized for leaving.

Forward Messages and Occupancy

Backward messages say whether a state can lead to high future reward. Forward messages say whether it is reachable from the initial distribution while remaining optimal so far.

Define the unnormalized forward message

f~t(st)=p(st,O1:t−1=1).\widetilde f_t(s_t) =p(s_t,O_{1:t-1}=1).

It follows the recursion

f~t+1(s′)=∫f~t(s)p0(a∣s)er(s,a)/αp(s′∣s,a) da ds.\boxed{ \widetilde f_{t+1}(s') =\int \widetilde f_t(s) p_0(a\mid s) e^{r(s,a)/\alpha} p(s'\mid s,a) \,da\,ds }.

The state posterior conditioned on optimality over the entire trajectory is

p(st∣O1:T=1)∝f~t(st)βt(st).\boxed{ p(s_t\mid O_{1:T}=1) \propto \widetilde f_t(s_t)\beta_t(s_t) }.

This is a forward-backward intersection:

f~t(st)⏟reachable from the start×βt(st)⏟can lead to high reward.\underbrace{\widetilde f_t(s_t)}_{\text{reachable from the start}} \times \underbrace{\beta_t(s_t)}_{\text{can lead to high reward}}.

A state may be promising but unreachable, or reachable but unable to lead to a good outcome. The conditioned occupancy emphasizes states satisfying both conditions.

The Optimism Problem

Exact inference conditions every random variable on optimality, including stochastic next states. The posterior transition becomes

p(s′∣s,a,Ot:T=1)=p(s′∣s,a)βt+1(s′)Esˉ∼p(⋅∣s,a)[βt+1(sˉ)].\boxed{ p(s'\mid s,a,O_{t:T}=1) =\frac{ p(s'\mid s,a)\beta_{t+1}(s') }{ \mathbb E_{\bar s\sim p(\cdot\mid s,a)} [\beta_{t+1}(\bar s)] } }.

Favorable next states receive greater posterior probability. Unless βt+1\beta_{t+1} is constant,

p(s′∣s,a,Ot:T=1)≠p(s′∣s,a).p(s'\mid s,a,O_{t:T}=1) \neq p(s'\mid s,a).

The same optimism appears in the exact-inference backup:

Qexact(s,a)=r(s,a)+αlog⁡Es′[eV(s′)/α].Q_{\mathrm{exact}}(s,a) =r(s,a)+\alpha\log \mathbb E_{s'}[e^{V(s')/\alpha}].

By Jensen's inequality,

αlog⁡E[eV/α]≥E[V].\boxed{ \alpha\log\mathbb E[e^{V/\alpha}] \geq\mathbb E[V] }.

For small α\alpha, even a rare favorable outcome can dominate the log-sum-exp.

Suppose an action leads to values 1010 and 00 with equal probability and α=1\alpha=1. Ordinary expected value is

E[V]=5,\mathbb E[V]=5,

while exact inference gives

log⁡(12e10+12e0)≈9.31.\log\left(\frac12e^{10}+\frac12e^0\right) \approx9.31.

This posterior statement is valid:

given a high-reward trajectory, a favorable transition probably occurred.\text{given a high-reward trajectory, a favorable transition probably occurred}.

But this control assumption is invalid:

choosing the action makes the favorable transition more likely.\text{choosing the action makes the favorable transition more likely}.

A controller may change its action distribution. It cannot change the environment dynamics. We therefore need an inference approximation that keeps transitions fixed.

Control as Variational Inference

Choose a variational trajectory distribution parameterized only by a policy:

qπ(τ)=p(s1)∏t=1Tπ(at∣st)∏t=1T−1p(st+1∣st,at).\boxed{ q_\pi(\tau) =p(s_1) \prod_{t=1}^{T}\pi(a_t\mid s_t) \prod_{t=1}^{T-1}p(s_{t+1}\mid s_t,a_t) }.

It retains the real initial-state and transition distributions. Only the action prior changes:

p0(at∣st)⟶π(at∣st).p_0(a_t\mid s_t) \longrightarrow \pi(a_t\mid s_t).

Maximizing the ELBO gives

αL(qπ)=Eτ∼qπ[∑t=1T(r(st,at)−αlog⁡π(at∣st)p0(at∣st))].\boxed{ \alpha\mathcal L(q_\pi) =\mathbb E_{\tau\sim q_\pi} \left[ \sum_{t=1}^{T} \left( r(s_t,a_t) -\alpha\log \frac{\pi(a_t\mid s_t)}{p_0(a_t\mid s_t)} \right) \right] }.
See derivation

The variational lower bound is

L(qπ)=Eqπ[log⁡p(τ,O1:T=1)−log⁡qπ(τ)].\mathcal L(q_\pi) =\mathbb E_{q_\pi} \left[ \log p(\tau,O_{1:T}=1)-\log q_\pi(\tau) \right].

Expand the joint model:

log⁡p(τ,O=1)=log⁡p(s1)+∑t=1T−1log⁡p(st+1∣st,at)+∑t=1Tlog⁡p0(at∣st)+1α∑t=1Tr(st,at).\begin{aligned} \log p(\tau,O=1) &=\log p(s_1) +\sum_{t=1}^{T-1}\log p(s_{t+1}\mid s_t,a_t)\\ &\quad+\sum_{t=1}^{T}\log p_0(a_t\mid s_t) +\frac1\alpha\sum_{t=1}^{T}r(s_t,a_t). \end{aligned}

The variational log-density is

log⁡qπ(τ)=log⁡p(s1)+∑t=1T−1log⁡p(st+1∣st,at)+∑t=1Tlog⁡π(at∣st).\log q_\pi(\tau) =\log p(s_1) +\sum_{t=1}^{T-1}\log p(s_{t+1}\mid s_t,a_t) +\sum_{t=1}^{T}\log\pi(a_t\mid s_t).

Thus

L(qπ)=Eqπ[∑t(r(st,at)α+log⁡p0(at∣st)−log⁡π(at∣st))].\mathcal L(q_\pi) =\mathbb E_{q_\pi} \left[ \sum_t \left( \frac{r(s_t,a_t)}\alpha +\log p_0(a_t\mid s_t) -\log\pi(a_t\mid s_t) \right) \right].

Multiplying by α\alpha yields the stated objective.

At a fixed state,

Ea∼π[log⁡π(a∣s)p0(a∣s)]=DKL(π(⋅∣s)∥p0(⋅∣s)).\mathbb E_{a\sim\pi} \left[ \log\frac{\pi(a\mid s)}{p_0(a\mid s)} \right] =D_{\mathrm{KL}} \left( \pi(\cdot\mid s)\|p_0(\cdot\mid s) \right).

Therefore variational control maximizes reward while penalizing deviation from a default behavior:

J(π)=Eτ∼π[∑t(r(st,at)−αDKL(π(⋅∣st)∥p0(⋅∣st)))].\boxed{ J(\pi) =\mathbb E_{\tau\sim\pi} \left[ \sum_t \left( r(s_t,a_t) -\alpha D_{\mathrm{KL}} (\pi(\cdot\mid s_t)\|p_0(\cdot\mid s_t)) \right) \right] }.

Maximum-Entropy Reinforcement Learning

For a uniform action prior,

−DKL(π∥p0)=H(π)−constant.-D_{\mathrm{KL}}(\pi\|p_0) =\mathcal H(\pi)-\text{constant}.

The variational objective becomes

J(π)=Eτ∼π[∑t=0∞γt(r(st,at)+αH(π(⋅∣st)))].\boxed{ J(\pi) =\mathbb E_{\tau\sim\pi} \left[ \sum_{t=0}^{\infty}\gamma^t \left( r(s_t,a_t) +\alpha\mathcal H(\pi(\cdot\mid s_t)) \right) \right] }.

Equivalently, using sampled actions,

J(π)=Eτ∼π[∑t=0∞γt(r(st,at)−αlog⁡π(at∣st))].J(\pi) =\mathbb E_{\tau\sim\pi} \left[ \sum_{t=0}^{\infty}\gamma^t \left( r(s_t,a_t)-\alpha\log\pi(a_t\mid s_t) \right) \right].

Entropy is not rewarded independently of return. Low-value actions remain exponentially unlikely. The objective mainly spreads probability among competitive actions, avoids premature collapse, and preserves multiple good solutions.

Soft policy evaluation

For a fixed policy,

Qπ(s,a)=r(s,a)+γEs′∼p(⋅∣s,a)[Vπ(s′)],\boxed{ Q^\pi(s,a) =r(s,a)+\gamma \mathbb E_{s'\sim p(\cdot\mid s,a)}[V^\pi(s')] }, Vπ(s)=Ea∼π(⋅∣s)[Qπ(s,a)−αlog⁡π(a∣s)p0(a∣s)].\boxed{ V^\pi(s) =\mathbb E_{a\sim\pi(\cdot\mid s)} \left[ Q^\pi(s,a) -\alpha\log \frac{\pi(a\mid s)}{p_0(a\mid s)} \right] }.

The transition expectation is ordinary. The action value includes reward and future soft value; the state value also includes the policy's entropy or KL cost.

Soft policy improvement

Given QπoldQ^{\pi_{\mathrm{old}}}, improve the policy by solving

\max_{\pi'} \left{ \mathbb E_{a\sim\pi'}[Q^{\pi_{\mathrm{old}}}(s,a)] -\alpha D_{\mathrm{KL}}(\pi'\|p_0) \right}.

The solution is

πnew(a∣s)=p0(a∣s)exp⁡(Qπold(s,a)/α)∫p0(aˉ∣s)exp⁡(Qπold(s,aˉ)/α)daˉ.\boxed{ \pi_{\mathrm{new}}(a\mid s) =\frac{ p_0(a\mid s) \exp(Q^{\pi_{\mathrm{old}}}(s,a)/\alpha) }{ \int p_0(\bar a\mid s) \exp(Q^{\pi_{\mathrm{old}}}(s,\bar a)/\alpha)d\bar a } }.
See derivation

Define

ZQ(s)=∫p0(a∣s)eQ(s,a)/αdaZ_Q(s)=\int p_0(a\mid s)e^{Q(s,a)/\alpha}da

and

πQ(a∣s)=p0(a∣s)eQ(s,a)/αZQ(s).\pi_Q(a\mid s) =\frac{p_0(a\mid s)e^{Q(s,a)/\alpha}}{Z_Q(s)}.

Then

Q(s,a)+αlog⁡p0(a∣s)=αlog⁡πQ(a∣s)+αlog⁡ZQ(s).Q(s,a)+\alpha\log p_0(a\mid s) =\alpha\log\pi_Q(a\mid s)+\alpha\log Z_Q(s).

Substitute into the improvement objective:

Eπ′[Q]−αDKL(π′∥p0)=αlog⁡ZQ(s)−αDKL(π′∥πQ).\begin{aligned} &\mathbb E_{\pi'}[Q] -\alpha D_{\mathrm{KL}}(\pi'\|p_0)\\ &=\alpha\log Z_Q(s) -\alpha D_{\mathrm{KL}}(\pi'\|\pi_Q). \end{aligned}
KL divergence is minimized at zero when

Therefore πQ\pi_Q is the improving policy.

Soft Bellman optimality

At the optimal policy,

Q∗(s,a)=r(s,a)+γEs′[V∗(s′)],\boxed{ Q^*(s,a) =r(s,a)+\gamma\mathbb E_{s'}[V^*(s')] }, V∗(s)=αlog⁡∫p0(a∣s)eQ∗(s,a)/αda,\boxed{ V^*(s) =\alpha\log \int p_0(a\mid s)e^{Q^*(s,a)/\alpha}da },

and

π∗(a∣s)=p0(a∣s)exp⁡(Q∗(s,a)−V∗(s)α).\boxed{ \pi^*(a\mid s) =p_0(a\mid s) \exp\left( \frac{Q^*(s,a)-V^*(s)}{\alpha} \right) }.

The difference from exact inference is crucial:

Qexact=r+αlog⁡Es′[eV(s′)/α],Q_{\mathrm{exact}} =r+\alpha\log\mathbb E_{s'}[e^{V(s')/\alpha}], Qvariational=r+γEs′[V(s′)].Q_{\mathrm{variational}} =r+\gamma\mathbb E_{s'}[V(s')].

Exact inference softens and reweights stochastic transitions. Variational maximum-entropy control keeps transitions fixed and softens only action selection.

Soft Policy Iteration\textbf{Soft Policy Iteration}Initialize a policy π0.Repeat:evaluate Qπk usingQπk(s,a)←r(s,a)+γEs′Ea′∼πk[Qπk(s′,a′)−αlog⁡πk(a′∣s′)p0(a′∣s′)],improve withπk+1(a∣s)∝p0(a∣s)eQπk(s,a)/α.\begin{aligned} &\text{Initialize a policy }\pi_0.\\[0.25em] &\text{Repeat:}\\[0.25em] &\qquad \text{evaluate }Q^{\pi_k}\text{ using}\\[0.25em] &\qquad\quad Q^{\pi_k}(s,a) \leftarrow r(s,a)+\gamma\mathbb E_{s'} \mathbb E_{a'\sim\pi_k} \left[ Q^{\pi_k}(s',a') -\alpha\log\frac{\pi_k(a'\mid s')}{p_0(a'\mid s')} \right],\\[0.25em] &\qquad \text{improve with}\\[0.25em] &\qquad\quad \pi_{k+1}(a\mid s) \propto p_0(a\mid s)e^{Q^{\pi_k}(s,a)/\alpha}. \end{aligned}

For small discrete problems, these steps can be exact. For continuous actions, the log-partition integral and exact soft policy are usually intractable. This motivates a learned actor.

Soft Actor-Critic

SAC approximates soft policy iteration with:

  • a stochastic actor πϕ\pi_\phi;
  • twin critics Qθ1Q_{\theta_1} and Qθ2Q_{\theta_2};
  • slowly updated target critics;
  • experience replay;
  • reparameterized action samples.

Soft critic target

For a replay transition (s,a,r,s′,d)(s,a,r,s',d), sample

a′∼πϕ(⋅∣s′).a'\sim\pi_\phi(\cdot\mid s').

The target is

y=r+γ(1−d)[min⁡i∈{1,2}Qθˉi(s′,a′)−αlog⁡πϕ(a′∣s′)].\boxed{ y =r+\gamma(1-d) \left[ \min_{i\in\{1,2\}}Q_{\bar\theta_i}(s',a') -\alpha\log\pi_\phi(a'\mid s') \right] }.

The minimum reduces positive critic bias. The negative log-probability is the sampled entropy bonus.

Each critic minimizes

LQ(θi)=ED[(Qθi(s,a)−stopgrad⁡[y])2].\boxed{ \mathcal L_Q(\theta_i) =\mathbb E_\mathcal D \left[ \left(Q_{\theta_i}(s,a)-\operatorname{stopgrad}[y]\right)^2 \right] }.

The targets move slowly through Polyak averaging:

θˉi←ρθˉi+(1−ρ)θi,ρ≈1.\bar\theta_i \leftarrow \rho\bar\theta_i+(1-\rho)\theta_i, \qquad \rho\approx1.

Squashed reparameterized actions

The actor predicts a mean and standard deviation. Sample

ϵ∼N(0,I),\epsilon\sim\mathcal N(0,I), u=μϕ(s)+σϕ(s)⊙ϵ,u=\mu_\phi(s)+\sigma_\phi(s)\odot\epsilon,

and squash to the action bounds:

a=tanh⁡(u).\boxed{a=\tanh(u)}.

This turns stochastic sampling into a differentiable function of parameter-free noise. Gradients can flow from the critic through the action into the actor.

The transformation changes the action density. Since

∂aj∂uj=1−tanh⁡2(uj)=1−aj2,\frac{\partial a_j}{\partial u_j} =1-\tanh^2(u_j) =1-a_j^2,

the change-of-variables formula gives

log⁡πϕ(a∣s)=log⁡N(u;μϕ(s),σϕ2(s))−∑jlog⁡(1−aj2).\boxed{ \log\pi_\phi(a\mid s) =\log\mathcal N (u;\mu_\phi(s),\sigma_\phi^2(s)) -\sum_j\log(1-a_j^2) }.

In practice, we add a small ε\varepsilon or use an algebraically stable expression. Without this correction, the entropy term describes the unsquashed Gaussian rather than the bounded action distribution actually sent to the environment.

Actor loss

Soft policy improvement fits the actor to the exponentiated-Q distribution. The resulting loss is

Lπ(ϕ)=Es∼D, a∼πϕ[αlog⁡πϕ(a∣s)−min⁡i∈{1,2}Qθi(s,a)].\boxed{ \mathcal L_\pi(\phi) =\mathbb E_{ s\sim\mathcal D,\, a\sim\pi_\phi } \left[ \alpha\log\pi_\phi(a\mid s) -\min_{i\in\{1,2\}}Q_{\theta_i}(s,a) \right] }.
See derivation

Let the target soft policy be

πQ(a∣s)=eQ(s,a)/αZ(s).\pi_Q(a\mid s) =\frac{e^{Q(s,a)/\alpha}}{Z(s)}.

Fit the actor by minimizing reverse KL:

DKL(πϕ∥πQ)=Ea∼πϕ[log⁡πϕ(a∣s)−Q(s,a)α+log⁡Z(s)].\begin{aligned} D_{\mathrm{KL}}(\pi_\phi\|\pi_Q) &=\mathbb E_{a\sim\pi_\phi} \left[ \log\pi_\phi(a\mid s) -\frac{Q(s,a)}\alpha +\log Z(s) \right]. \end{aligned}

Multiply by α\alpha:

αDKL(πϕ∥πQ)=Eπϕ[αlog⁡πϕ(a∣s)−Q(s,a)]+αlog⁡Z(s).\alpha D_{\mathrm{KL}}(\pi_\phi\|\pi_Q) =\mathbb E_{\pi_\phi} [\alpha\log\pi_\phi(a\mid s)-Q(s,a)] +\alpha\log Z(s).

Dropping it yields the SAC actor loss.

The two terms exert opposing pressures:

−Q(s,a)⏟choose high-value actions+αlog⁡π(a∣s)⏟avoid excessive concentration.\underbrace{-Q(s,a)}_{\text{choose high-value actions}} + \underbrace{\alpha\log\pi(a\mid s)}_{\text{avoid excessive concentration}}.

Automatic temperature tuning

A fixed α\alpha is sensitive to reward scale and action dimension. SAC can learn it to match a target entropy Htgt\mathcal H_{\mathrm{tgt}}:

Lα=E[−α(log⁡πϕ(a∣s)+Htgt)].\boxed{ \mathcal L_\alpha =\mathbb E \left[ -\alpha \left( \log\pi_\phi(a\mid s) +\mathcal H_{\mathrm{tgt}} \right) \right] }.

Parameterize

α=eη,η=log⁡α,\alpha=e^\eta, \qquad \eta=\log\alpha,

to guarantee positivity. If entropy is below the target, optimization increases α\alpha; if it is above the target, α\alpha decreases. The actor's log-probability is detached during the temperature update.

Soft Actor-Critic\textbf{Soft Actor-Critic}Initialize πϕ,Qθ1,Qθ2, target critics, and replay D.For each environment step:at∼πϕ(⋅∣st),execute at and store the transition,(s,a,r,s′,d)∼D,a′∼πϕ(⋅∣s′),y←r+γ(1−d)[min⁡iQθˉi(s′,a′)−αlog⁡πϕ(a′∣s′)],fit both critics to the detached target y,a∼πϕ(⋅∣s) by reparameterization,ϕ←arg⁡min⁡ϕE[αlog⁡πϕ(a∣s)−min⁡iQθi(s,a)],optionally update α,Polyak-update the target critics.\begin{aligned} &\text{Initialize }\pi_\phi,Q_{\theta_1},Q_{\theta_2}, \text{ target critics, and replay }\mathcal D.\\[0.25em] &\text{For each environment step:}\\[0.25em] &\qquad a_t\sim\pi_\phi(\cdot\mid s_t),\\[0.25em] &\qquad \text{execute }a_t\text{ and store the transition},\\[0.25em] &\qquad (s,a,r,s',d)\sim\mathcal D,\\[0.25em] &\qquad a'\sim\pi_\phi(\cdot\mid s'),\\[0.25em] &\qquad y\leftarrow r+\gamma(1-d) \left[ \min_iQ_{\bar\theta_i}(s',a') -\alpha\log\pi_\phi(a'\mid s') \right],\\[0.25em] &\qquad \text{fit both critics to the detached target }y,\\[0.25em] &\qquad a\sim\pi_\phi(\cdot\mid s)\text{ by reparameterization},\\[0.25em] &\qquad \phi\leftarrow\arg\min_\phi \mathbb E[\alpha\log\pi_\phi(a\mid s)-\min_iQ_{\theta_i}(s,a)],\\[0.25em] &\qquad \text{optionally update }\alpha,\\[0.25em] &\qquad \text{Polyak-update the target critics}. \end{aligned}

Boundaries and Special Cases

Boltzmann exploration is not maximum-entropy RL. An ordinary Q-learning agent may select actions with

πbeh(a∣s)∝eQordinary(s,a)/α\pi_{\mathrm{beh}}(a\mid s) \propto e^{Q_{\mathrm{ordinary}}(s,a)/\alpha}

while still training the critic with

y=r+γmax⁡a′Q(s′,a′).y=r+\gamma\max_{a'}Q(s',a').

Here softmax is only an exploration rule. In maximum-entropy RL, entropy changes the objective, value function, critic target, and policy-improvement operator:

softmax action selection≠maximum-entropy reinforcement learning.\boxed{ \text{softmax action selection} \neq \text{maximum-entropy reinforcement learning} }.

Non-uniform priors. The general KL-regularized objective is

J(π)=Eπ[∑tγt(r(st,at)−αDKL(π(⋅∣st)∥p0(⋅∣st)))].J(\pi) =\mathbb E_\pi \left[ \sum_t\gamma^t \left( r(s_t,a_t) -\alpha D_{\mathrm{KL}} (\pi(\cdot\mid s_t)\|p_0(\cdot\mid s_t)) \right) \right].

The prior can represent a previous policy, safe controller, demonstration policy, skill, or reference model. The learned policy deviates only when reward justifies the KL cost.

Ordinary reinforcement learning. As temperature approaches zero,

V∗(s)=αlog⁡∫p0(a∣s)eQ∗(s,a)/αda⟶max⁡a:p0(a∣s)>0Q∗(s,a).V^*(s) =\alpha\log\int p_0(a\mid s)e^{Q^*(s,a)/\alpha}da \longrightarrow \max_{a:p_0(a\mid s)>0}Q^*(s,a).

Therefore,

lim⁡α→0+maximum-entropy RL=ordinary optimal control.\boxed{ \lim_{\alpha\to0^+} \text{maximum-entropy RL} =\text{ordinary optimal control} }.

At the other extreme,

lim⁡α→∞π∗(a∣s)=p0(a∣s).\lim_{\alpha\to\infty}\pi^*(a\mid s) =p_0(a\mid s).

Temperature interpolates between greedy reward maximization and following the prior.

Practical Diagnosis

SymptomLikely causeUseful response
Critic values are too optimisticTransition inference or critic overestimationKeep real dynamics fixed and use twin critics
SAC entropy collapses earlyTemperature is too smallTune or learn α\alpha and inspect target entropy
SAC remains random despite learningEntropy pressure dominates rewardCheck reward scale and temperature
Actions reach bounds but log-probabilities look normalMissing tanh Jacobian correctionCompute density after the action transformation
Q-values explodeTarget graph, terminal mask, or reward scale is wrongDetach targets and verify bootstrapping
Temperature moves in the wrong directionSign convention is inconsistentCheck entropy against Htgt\mathcal H_{\mathrm{tgt}}
Actor improves Q but not environment returnCritic is being exploitedMonitor realized return and use conservative critics

The full derivation can be remembered as

p(Ot=1∣st,at)∝er(st,at)/αp(O_t=1\mid s_t,a_t) \propto e^{r(s_t,a_t)/\alpha} ⇓\Downarrow p(τ∣O1:T=1)∝p(τ)eR(τ)/αp(\tau\mid O_{1:T}=1) \propto p(\tau)e^{R(\tau)/\alpha} ⇓restrict inference to real dynamics\Downarrow\quad\text{restrict inference to real dynamics} Eπ[∑tr(st,at)−αlog⁡π(at∣st)p0(at∣st)]\mathbb E_\pi \left[ \sum_t r(s_t,a_t) -\alpha\log\frac{\pi(a_t\mid s_t)}{p_0(a_t\mid s_t)} \right] ⇓p0 uniform\Downarrow\quad p_0\text{ uniform} Eπ[∑tr(st,at)+αH(π(⋅∣st))].\mathbb E_\pi \left[ \sum_t r(s_t,a_t)+\alpha\mathcal H(\pi(\cdot\mid s_t)) \right].

Control as inference turns rewards into evidence about optimal behavior. Variational control prevents that inference from cheating through favorable dynamics. Soft Actor-Critic then approximates the resulting soft policy iteration with replay, twin critics, and a reparameterized stochastic actor.