Search

Search papers and lessons

Soft Actor-Critic

Level 4 · 16 min read

Soft Actor-Critic is an off-policy actor-critic method that trains a stochastic policy to maximise reward plus policy entropy, with a learned temperature that sets how much randomness is worth.

On this page

  • Colour = topic mix
  • Reinforcement learning
  • Probability
  • Calculus
  • Discrete math
  • Linear algebra
  • Inference
  • Sign in to see your progress

TL;DR

Soft Actor-Critic (SAC) learns two things at once: a policy that chooses actions, and critics that estimate how good those actions are. The policy is trained to earn reward while staying as random as the temperature setting allows. Because the method reuses old experience from a replay buffer, it is off-policy and sample efficient, and it works well on continuous control tasks such as robot joints.

It combines three ideas: the maximum entropy objective, the Actor-Critic Methods split between policy and value, and the Reparameterization Trick for differentiating through sampled actions. Its temperature is adjusted during training so that the policy keeps a chosen amount of randomness.

Where you'll see it: in robot locomotion and manipulation experiments, in simulated control benchmarks, and as a default baseline for continuous-action RL.

Intuition

A learner who keeps options open

Picture a robot learning to reach a cup. Early on, several arm motions look promising. A policy that commits to the first one that works will often stop improving, and a small disturbance such as a bumped table can knock it off course. A policy that keeps a spread of good motions can recover from a bump, and it keeps testing alternatives as it learns.

SAC builds this into training. Each action is scored by the reward it earns plus a bonus for the entropy of the policy at that state. The bonus is a price on certainty, set by a number called the temperature. A high price keeps the policy broad, and a low price lets it sharpen onto the best known motion.

Why it is off-policy

A method is off-policy when it learns from data that was not produced by its current policy. SAC stores every transition in a replay buffer (see Experience Replay) and samples mini-batches from it. The critics learn from old data, which makes each environment step much more useful than in on-policy methods that discard data after one update. The critic can learn from old data without any correction because it scores one specific action, and only the next action in the target is drawn from the current policy.

flowchart LR
  A[Environment step] --> B[Replay buffer]
  B --> C[Sample a mini-batch]
  C --> D[Update two soft Q critics]
  C --> E[Update the stochastic policy]
  D --> E
  E --> F[Adjust temperature alpha]
  F --> A

The math

The objective

SAC optimises the maximum entropy objective from maximum entropy RL. The policy πϕ(a∣s)\pi_\phi(a \mid s) has parameters ϕ\phi. The soft action value Qθ(s,a)Q_\theta(s, a) has parameters θ\theta, and the temperature α>0\alpha \gt 0 sets the value of entropy. The soft state value is

V(s)=Ea∼π[Q(s,a)−αlog⁡π(a∣s)].V(s) = \mathbb{E}_{a \sim \pi}\left[ Q(s, a) - \alpha \log \pi(a \mid s) \right].

The term −log⁡π(a∣s)-\log \pi(a \mid s) is the surprise of the sampled action, and its average under the policy is the Entropy of the policy at ss. So VV is the expected action value plus the entropy bonus.

The critic

The critics are trained to satisfy the soft Bellman equation with a target network. Given a transition (s,a,r,s′)(s, a, r, s'), the target is

y=r+γ(min⁡i=1,2Qˉθi(s′,a′)−αlog⁡πϕ(a′∣s′)),a′∼πϕ(⋅∣s′).y = r + \gamma \left( \min_{i=1,2} \bar{Q}_{\theta_i}(s', a') - \alpha \log \pi_\phi(a' \mid s') \right), \qquad a' \sim \pi_\phi(\cdot \mid s').

Here γ\gamma is the discount factor, and Qˉ\bar{Q} denotes the target networks. Two critics are trained, and the minimum of their estimates is used in the target. Taking the smaller of two estimates counters the tendency of a single learned value to be too optimistic, the same overestimation problem that double Q-Learning addresses. Each critic is then regressed toward yy by squared error.

The target networks are updated slowly, as a moving average of the live critics, θˉ←τθ+(1−τ)θˉ\bar{\theta} \leftarrow \tau \theta + (1-\tau)\bar{\theta} with τ\tau a small number such as 0.005. This keeps the regression target from moving with every update.

The actor and the reparameterization

The policy is updated to minimise

Jπ(ϕ)=Es∼D, ϵ[αlog⁡πϕ(aϕ(s,ϵ)∣s)−Qθ(s,aϕ(s,ϵ))].J_\pi(\phi) = \mathbb{E}_{s \sim \mathcal{D}, \, \epsilon}\left[ \alpha \log \pi_\phi(a_\phi(s, \epsilon) \mid s) - Q_\theta(s, a_\phi(s, \epsilon)) \right].

The action is written as a deterministic function of the state and a noise sample, aϕ(s,ϵ)=tanh⁡(μϕ(s)+σϕ(s) ϵ)a_\phi(s, \epsilon) = \tanh(\mu_\phi(s) + \sigma_\phi(s) \, \epsilon) with ϵ∼N(0,I)\epsilon \sim \mathcal{N}(0, I). This is the reparameterization trick: the randomness sits in ϵ\epsilon, so the gradient of the loss can flow through the action into the policy's parameters. The tanh⁡\tanh squashes the action into a bounded range, such as the torque limits of a motor.

Squashing changes the density, so the log-probability is corrected. If u=μ+σϵu = \mu + \sigma\epsilon is the pre-squash sample and a=tanh⁡(u)a = \tanh(u), then

log⁡π(a∣s)=log⁡N(u;μ,σ2)−∑jlog⁡(1−tanh⁡2(uj)),\log \pi(a \mid s) = \log \mathcal{N}(u; \mu, \sigma^2) - \sum_j \log\left(1 - \tanh^2(u_j)\right),

summed over the action dimensions jj.

The temperature

Rather than fix α\alpha, SAC can tune it so the policy's entropy stays near a target Hˉ\bar{\mathcal{H}}, commonly set to minus the number of action dimensions. The temperature loss is

J(α)=Ea∼π[−α(log⁡π(a∣s)+Hˉ)].J(\alpha) = \mathbb{E}_{a \sim \pi}\left[ -\alpha \left( \log \pi(a \mid s) + \bar{\mathcal{H}} \right) \right].

When the policy is more random than the target, the gradient pushes α\alpha down, and when it is too narrow, the gradient pushes α\alpha up.

Worked examples

Example
1. One critic update, two actions (tiny)

A state s′s' has two actions with equal probability π=(0.5,0.5)\pi = (0.5, 0.5), and their soft values are Q=(3,1)Q = (3, 1). Take α=0.5\alpha = 0.5, reward r=0.5r = 0.5 and discount γ=0.9\gamma = 0.9.

  1. Expected action value: 0.5×3+0.5×1=20.5 \times 3 + 0.5 \times 1 = 2.
  2. Entropy term: −log⁡0.5=0.6931-\log 0.5 = 0.6931 for each action, so the expected surprise is 0.69310.6931.
  3. Soft state value: V(s′)=2+α×0.6931=2+0.3466=2.3466V(s') = 2 + \alpha \times 0.6931 = 2 + 0.3466 = 2.3466.
  4. Target: y=0.5+0.9×2.3466=0.5+2.1119=2.6119y = 0.5 + 0.9 \times 2.3466 = 0.5 + 2.1119 = 2.6119.

Interpretation: the target is higher than a plain Bellman target would be, 0.5+0.9×2=2.30.5 + 0.9 \times 2 = 2.3, because the uniform policy has entropy that adds to the value of the next state. The critic learns this bonus, and the policy is rewarded for keeping it.

Example
2. One reparameterized action and its log-probability (standard case)

A Gaussian policy in one dimension has μ=0.2\mu = 0.2 and σ=0.5\sigma = 0.5. The noise sample is ϵ=1.0\epsilon = 1.0.

  1. Pre-squash sample: u=0.2+0.5×1.0=0.7u = 0.2 + 0.5 \times 1.0 = 0.7.
  2. Squashed action: a=tanh⁡(0.7)≈0.6044a = \tanh(0.7) \approx 0.6044.
  3. Gaussian log-density at uu: log⁡N(0.7;0.2,0.25)=−12log⁡(2π×0.25)−(0.7−0.2)22×0.25=−0.2258−0.5=−0.7258\log \mathcal{N}(0.7; 0.2, 0.25) = -\tfrac{1}{2}\log(2\pi \times 0.25) - \frac{(0.7 - 0.2)^2}{2 \times 0.25} = -0.2258 - 0.5 = -0.7258.
  4. Squash correction: 1−a2=1−0.3653=0.63471 - a^2 = 1 - 0.3653 = 0.6347, and log⁡0.6347=−0.4544\log 0.6347 = -0.4544.
  5. Log-probability of the squashed action: log⁡π(a∣s)=−0.7258−(−0.4544)=−0.2714\log \pi(a \mid s) = -0.7258 - (-0.4544) = -0.2714.
  6. With α=0.2\alpha = 0.2 and a critic estimate Q=1.5Q = 1.5, the actor's loss for this sample is 0.2×(−0.2714)−1.5=−1.55430.2 \times (-0.2714) - 1.5 = -1.5543.

Interpretation: the gradient of this loss with respect to μ\mu and σ\sigma can be taken through a=tanh⁡(μ+σϵ)a = \tanh(\mu + \sigma\epsilon), so the policy moves toward actions the critic rates higher, with the entropy term keeping the spread from collapsing.

Example
3. Tuning the temperature on a six-joint arm (realistic)

A simulated arm has six action dimensions, so the target entropy is Hˉ=−6\bar{\mathcal{H}} = -6. In a batch, the average of −log⁡π-\log \pi is 5.25.2 nats (entropy measured with the natural logarithm), meaning the policy is fairly random.

  1. Estimated entropy of the policy: H≈5.2\mathcal{H} \approx 5.2.
  2. Temperature loss gradient: ∂J/∂α=H−Hˉ=5.2−(−6)=11.2\partial J / \partial \alpha = \mathcal{H} - \bar{\mathcal{H}} = 5.2 - (-6) = 11.2. This is positive.
  3. Starting from α=0.2\alpha = 0.2, a plain gradient step with learning rate 0.00030.0003 gives α←0.2−0.0003×11.2=0.19664\alpha \leftarrow 0.2 - 0.0003 \times 11.2 = 0.19664.
  4. Repeat over many updates. As the policy narrows toward the target, the gradient shrinks and α\alpha settles.

Interpretation: the policy is too random for the target, so the temperature falls and the reward term regains weight. The practical benefit is that the designer does not have to guess a good fixed α\alpha for each task, which varies with reward scale. The implementation in practice uses an adaptive optimiser and parameterises α\alpha through its logarithm to keep it positive.

Tradeoffs and failure modes

SituationWhat goes wrongBetter choice
Discrete actions with a small set of choicesThe reparameterization gradient is not available for discrete samplingUse a discrete-action method such as [[deep-q-network
Reward scale changes between tasksA fixed temperature is too strong or too weakUse the automatic temperature rule, or rescale rewards
Critics disagree stronglyThe minimum of two critics can underestimate for a long timeTune the target update rate, or check the critic loss curves
The replay buffer is stale or narrowCritics learn from data far from the current policyKeep the buffer large, and monitor how far the policy has moved
The task needs a single deterministic action at test timeSampling noise hurts the final behaviourEvaluate with the mean of the policy instead of a sample

SAC is usually compared with DDPG, which uses a deterministic policy and no entropy bonus, and with on-policy methods such as PPO. SAC tends to need more compute per step but fewer environment steps, which matters most when simulation or hardware time is expensive.

History

Soft Q-learning, the value-based precursor, was introduced by Haarnoja and colleagues in 2017, using the soft Bellman equation and an energy-based policy. Their 2018 paper turned this into SAC, an off-policy actor-critic with a separate stochastic actor, two critics and the reparameterized actor update. It showed strong results on continuous control benchmarks with a single set of hyperparameters across tasks. A follow-up in late 2018 added the automatic temperature adjustment and reported results on real robots. Its stability and low tuning burden are why it became a standard baseline in continuous-action reinforcement learning.

Recap

SAC optimises reward plus the entropy of a stochastic policy, as set out in maximum entropy RL. Its critics learn the soft Bellman target from a replay buffer, taking the smaller of two estimates to limit overestimation. Its actor is updated through a reparameterized action, tanh⁡(μ+σϵ)\tanh(\mu + \sigma\epsilon), with a log-probability corrected for the squash. A temperature rule adjusts the entropy bonus so the policy keeps a target level of randomness. The same Bellman structure, with a soft maximum in place of a hard one, underlies the whole method.

Check yourself

QuestionRecall: what are the two main roles in SAC, and what is the role of the temperature α\alpha?Show answer

The policy chooses actions and the critics estimate their soft values. The temperature α\alpha sets how much the policy's entropy counts relative to reward. Large α\alpha keeps the policy broad, and small α\alpha lets it sharpen onto the best known actions.

QuestionComputation: a state has two equally likely actions with soft values Q=(4,2)Q = (4, 2). Take α=1\alpha = 1, reward r=1r = 1 and discount γ=0.5\gamma = 0.5. What is the soft target yy?Show answer

The expected action value is 0.5×4+0.5×2=30.5 \times 4 + 0.5 \times 2 = 3. The entropy term is α×(−log⁡0.5)=0.6931\alpha \times (-\log 0.5) = 0.6931, so the soft state value is 3.69313.6931. The target is y=1+0.5×3.6931=2.8466y = 1 + 0.5 \times 3.6931 = 2.8466.

QuestionWhy does SAC use a reparameterized action tanh⁡(μ+σϵ)\tanh(\mu + \sigma\epsilon) rather than sampling the action directly from the policy?Show answer

Sampling directly does not give a gradient with respect to μ\mu and σ\sigma, because the random draw sits inside the sampling step. Writing the action as a smooth function of the parameters and a fixed noise sample ϵ\epsilon moves the randomness into ϵ\epsilon, so the loss can be differentiated through the action into the policy.

QuestionApplied: a researcher fixes α\alpha at a value that works well on one robot task, then moves to a task whose rewards are 100 times larger. What is likely to happen, and what is the fix?Show answer

Rewards 100 times larger make the fixed entropy bonus relatively tiny, so the policy becomes too narrow and explores less. The fix is to let α\alpha adapt with the automatic temperature rule, which keeps the entropy at its target whatever the reward scale, or to rescale the rewards so the trade-off is comparable across tasks.

Sources