5.2.11Deep & Advanced RL

Soft Actor-Critic (SAC)

1,694 words8 min readdifficulty · medium1 backlinks

WHY does SAC exist?

Classic policy-gradient methods (like DDPG, TD3) have two chronic pains:

  1. Brittle exploration — a deterministic policy collapses to one action too early and stops exploring.
  2. Sample inefficiency vs stability trade-off — on-policy methods (PPO) are stable but throw away data; off-policy methods (DDPG) reuse data but are unstable.

WHAT is the objective? (Maximum-entropy RL)

Since logπ(as)-\log\pi(a|s) appears, we can fold entropy into the reward by defining a soft reward r+αHr + \alpha\mathcal{H}. Everything else follows by re-deriving Bellman equations with this augmented reward.


HOW: deriving the soft Bellman equations from scratch

Step 1 — Soft value. Define the soft state value as expected return-plus-entropy from ss: V(s)=Eaπ[Q(s,a)αlogπ(as)]V(s)=\mathbb{E}_{a\sim\pi}\big[Q(s,a)-\alpha\log\pi(a|s)\big] Why this step? The extra αlogπ-\alpha\log\pi term is exactly the entropy contribution (H=E[logπ]\mathcal{H}=\mathbb{E}[-\log\pi]) pulled out of VV. So value = expected Q plus how random the policy is.

Step 2 — Soft Q Bellman backup. The Q-value = immediate reward + discounted next soft value: Q(s,a)=r(s,a)+γEs[V(s)]Q(s,a)=r(s,a)+\gamma\,\mathbb{E}_{s'}\big[V(s')\big] Why this step? Standard Bellman recursion, but VV already carries the entropy bonus of all future steps. So future exploration is baked in.

Step 3 — What policy is optimal? Solve maxπJ\max_\pi J with the entropy term. The maximizer of Ea[Qαlogπ]\mathbb{E}_a[Q-\alpha\log\pi] subject to π\pi being a distribution is the Boltzmann (softmax) policy: π(as)=exp ⁣(1αQ(s,a))Z(s),Z(s)=exp ⁣(1αQ(s,a))da\pi^*(a|s)=\frac{\exp\!\big(\tfrac{1}{\alpha}Q(s,a)\big)}{Z(s)},\qquad Z(s)=\int \exp\!\big(\tfrac{1}{\alpha}Q(s,a')\big)\,da'

Figure — Soft Actor-Critic (SAC)

The three learned pieces (training)

SAC learns: two Q-networks Qθ1,Qθ2Q_{\theta_1},Q_{\theta_2} (+ their targets), and one stochastic policy πϕ\pi_\phi.


Worked example 1 — computing a soft target yy

Suppose r=1r=1, γ=0.99\gamma=0.99, next action aa' gives Qθˉ1=5.0Q_{\bar\theta_1}=5.0, Qθˉ2=4.6Q_{\bar\theta_2}=4.6, logπ(as)=1.2\log\pi(a'|s')=-1.2, α=0.2\alpha=0.2.

  • minjQ=4.6\min_j Q = 4.6. Why? Pessimistic estimate avoids overestimation.
  • Entropy bonus =αlogπ=0.2×(1.2)=+0.24=-\alpha\log\pi = -0.2\times(-1.2)=+0.24. Why? More random policy → higher next value.
  • V(s)=4.6+0.24=4.84V(s') = 4.6 + 0.24 = 4.84.
  • y=1+0.99×4.84=1+4.7916=5.79y = 1 + 0.99\times4.84 = 1 + 4.7916 = \mathbf{5.79}.

Worked example 2 — effect of temperature α\alpha

Two actions with Q=(2.0,1.0)Q=(2.0, 1.0). Boltzmann policy πeQ/α\pi\propto e^{Q/\alpha}.

  • α=1\alpha=1: weights e2,e1=7.39,2.72e^{2},e^{1}=7.39,2.72π=(0.73,0.27)\pi=(0.73,0.27). Why? Mild preference, still explores.
  • α=0.1\alpha=0.1: weights e20,e10e^{20},e^{10}π(0.99995,0.00005)\pi\approx(0.99995,0.00005). Why? Small α\alpha ⇒ near-greedy.
  • α=10\alpha=10: weights e0.2,e0.1e^{0.2},e^{0.1}π(0.525,0.475)\pi\approx(0.525,0.475). Why? Big α\alpha ⇒ near-uniform, max exploration.
Recall Feynman: explain to a 12-year-old

Imagine a robot learning to reach candy. If it only cares about candy, it finds ONE path and always walks it — even if a wall appears there tomorrow. SAC pays the robot a little bonus for trying different paths ("stay surprising!"). So it keeps a few good options open and doesn't get stuck. The knob α\alpha decides how much bonus it gets for being adventurous. Two "judges" (the two Q-networks) score each move and we always trust the stricter judge so the robot doesn't get overconfident.


Forecast-then-Verify

Answers: (1) deterministic/greedy — reward-only RL. (2) to counter overestimation via min\min. (3) off-policy: it reuses replay data, so it needs far fewer environment samples than PPO.


Flashcards

What objective does SAC maximize?
Expected return plus policy entropy: tE[r+αH(π(st))]\sum_t \mathbb{E}[r + \alpha\mathcal{H}(\pi(\cdot|s_t))].
What is the temperature α\alpha?
The coefficient trading off reward vs entropy (exploration); small α\alpha → greedy, large α\alpha → uniform.
Derive the optimal soft policy form.
Maximizing E[Q]αE[logπ]\mathbb{E}[Q]-\alpha\mathbb{E}[\log\pi] gives πexp(Q/α)\pi^*\propto\exp(Q/\alpha) (Boltzmann).
Write the soft state value V(s)V(s).
V(s)=Eaπ[Q(s,a)αlogπ(as)]V(s)=\mathbb{E}_{a\sim\pi}[Q(s,a)-\alpha\log\pi(a|s)].
Write the soft Q target yy.
y=r+γ(minjQθˉj(s,a)αlogπ(as))y=r+\gamma(\min_j Q_{\bar\theta_j}(s',a')-\alpha\log\pi(a'|s')), aπa'\sim\pi.
Why take the min of two Q-networks?
To reduce overestimation bias (clipped double-Q, like TD3).
Why the reparameterization trick in the actor loss?
To backpropagate through the sampled action; a=tanh(μ+σϵ)a=\tanh(\mu+\sigma\odot\epsilon).
What is the tanh log-prob correction?
logπ(a)=logμ(u)ilog(1tanh2ui)\log\pi(a)=\log\mu(u)-\sum_i\log(1-\tanh^2 u_i).
How is α\alpha auto-tuned?
Minimize E[α(logπ+Hˉ)]\mathbb{E}[-\alpha(\log\pi+\bar{\mathcal H})] with target entropy Hˉ=dim(A)\bar{\mathcal H}=-\dim(\mathcal A).
Is SAC on- or off-policy?
Off-policy; uses a replay buffer, making it sample-efficient.
What networks does SAC train?
Two Q-critics (+targets) and one stochastic actor; value net is optional.
Actor loss expression?
Jπ=E[αlogπϕ(as)minjQθj(s,a)]J_\pi=\mathbb{E}[\alpha\log\pi_\phi(a|s)-\min_j Q_{\theta_j}(s,a)].

Connections

  • Soft Q-Learning — the value-based ancestor with the same exp(Q/α)\exp(Q/\alpha) policy.
  • TD3 — source of the twin-Q / clipped double-Q trick.
  • DDPG — deterministic off-policy actor-critic SAC improves on.
  • Maximum Entropy RL — the framework behind the objective.
  • Reparameterization Trick — low-variance stochastic actor gradients.
  • Bellman Equation — soft version derived here.
  • PPO — on-policy contrast; more stable but less sample-efficient.
  • Entropy (Information Theory) — the E[logπ]-\mathbb{E}[\log\pi] measure of randomness.

Concept Map

motivates

part of

part of

maximizes

reward plus

scaled by

folded into

re-derives

defines

gives

maximized by

off-policy

DDPG TD3 problems

Soft Actor-Critic

Brittle exploration

Sample efficiency vs stability

Max-entropy objective

Policy entropy H

Temperature alpha

Soft reward

Soft Bellman equations

Soft value V

Soft Q backup

Boltzmann softmax policy

Self-annealing exploration

Hinglish (regional understanding)

Intuition Hinglish mein samjho

SAC ka core idea simple hai: normal RL sirf reward maximize karta hai, lekin SAC reward ke saath-saath policy ki entropy (yaani randomness) ko bhi maximize karta hai. Iska matlab agent ko fayda milta hai jab woh success bhi paaye aur thoda "unpredictable" bhi rahe. Isse exploration apne aap zinda rehti hai — agent ek hi action pe stuck nahi hota, kyunki uspe "adventurous raho" ka bonus milta hai. Yeh bonus kitna strong ho, woh temperature α\alpha decide karta hai: chhota α\alpha matlab greedy (ek hi best action), bada α\alpha matlab sab actions barabar (full exploration).

Maths side pe, hum normal Bellman equation ko "soft" bana dete hain. Value V(s)=E[Qαlogπ]V(s)=\mathbb{E}[Q-\alpha\log\pi] — yani expected Q plus entropy ka bonus. Jab hum entropy ke saath objective ko maximize karte hain, toh optimal policy πexp(Q/α)\pi^*\propto \exp(Q/\alpha) nikalti hai — yeh ek softmax/Boltzmann distribution hai. Yehi exp(Q/α)\exp(Q/\alpha) formula batata hai ki kyun α\alpha exploration ka knob hai.

Practical training mein SAC teen cheezein seekhta hai: do Q-networks (twin critics), aur ek stochastic actor. Do Q-networks ka min\min lete hain taaki Q overestimate na ho (TD3 wala trick). Actor ko update karte hain reparameterization trick se — a=tanh(μ+σϵ)a=\tanh(\mu+\sigma\epsilon) — taaki gradient sample ke through flow kare. Ek important detail: tanh lagane ke baad log-probability mein log(1tanh2)\log(1-\tanh^2) correction add karna padta hai, warna entropy galat aayegi.

Yeh matter kyun karta hai? SAC off-policy hai, matlab replay buffer se purana data reuse karta hai — isliye bahut sample-efficient hai, PPO se kam environment interactions chahiye. Aur entropy ki wajah se training stable aur robust rehti hai. Continuous control (robotics, locomotion) mein SAC aaj bhi ek top baseline hai — yaad rakho: Soft targets, Adventurous actor, Cautious critic.

Go deeper — visual, from zero

Test yourself — Deep & Advanced RL

Connections