Search

Search papers and lessons

Soft Actor-Critic: Off-Policy Maximum Entropy Deep RL with a Stochastic Actor

Tuomas Haarnoja, Aurick Zhou, Pieter Abbeel et al. · 2018

Haarnoja et al. (2018) train a random policy to earn reward and stay random, reusing past data, which made continuous control faster and more stable.

On this page

  • Colour = topic mix
  • Reinforcement learning
  • Probability
  • Discrete math
  • Calculus
  • Linear algebra
  • Inference
  • ReadReady to readNot yet

TL;DR

Soft Actor-Critic (SAC) is a reinforcement learning algorithm for continuous actions. It trains a random policy to earn reward and to stay as random as possible, and it reuses past experience through a replay buffer. The entropy term keeps exploration going, and two value estimators keep training stable. On the harder continuous-control benchmarks, including the 21-dimensional Humanoid, SAC learned faster and reached higher returns than the off-policy DDPG, the on-policy PPO and soft Q-learning.

Builds on: Maximum Entropy Reinforcement Learning, Actor-Critic Methods, Experience Replay, Reparameterization Trick · Leads to: the SAC algorithm

Why it mattered

Before SAC, deep RL on robot-style control problems was expensive in samples and fragile in tuning. On-policy methods such as PPO discard each batch after a few updates, so they need many fresh rollouts. Off-policy methods such as DDPG can reuse data but often failed on the hardest tasks. SAC showed that one algorithm could reuse data, stay stable and explore well on these tasks, and it became a common baseline for continuous control in research and in robot learning.

Its maximum entropy idea also spread beyond this algorithm. Choosing the entropy weight, the temperature, became a central design question, and the 2018 follow-up by the same authors (arXiv 1812.05905) made that weight adjust itself during training, which removed one of the main hand-tuned settings.

The problem

The paper names two problems with deep RL that limit real use.

  1. Sample complexity. Many methods need a very large number of environment steps, even on simple tasks. On-policy methods throw data away after each update, so every step needs new experience.
  2. Brittleness. Learning rates, exploration constants and other settings must be tuned carefully, and small changes can make training fail.

Off-policy learning is the obvious cure for the first problem, since it reuses old data. But combining off-policy updates with neural function approximation is unstable, and the deterministic policies of DDPG explore poorly. SAC addresses both by making the policy stochastic, adding entropy to the objective so exploration is built in, and combining off-policy updates with two value estimators.

How it works

The walkthrough follows one update for one stored transition. The policy outputs a Gaussian with mean μ\mu and standard deviation σ\sigma for a single action. The critics estimate the value of state-action pairs.

flowchart LR
  A[Act with the policy and store the transition] --> B[Replay buffer]
  B --> C[Sample a minibatch]
  C --> D[Update the value network]
  C --> E[Update the two Q-functions]
  C --> F[Update the policy]
  E --> G[Slowly move the target value network]
  D --> G
  1. Act and store. In state ss the policy samples an action aa and the environment returns reward r=1r = 1 and next state s′s'. The transition (s,a,r,s′)(s, a, r, s') goes into the replay buffer. Check: the buffer grows with every step, so old transitions stay available for reuse.
  2. Sample a minibatch. Pick transitions from the buffer at random. This breaks the link between neighbouring steps, as in Experience Replay.
  3. Update the value network. Its target is the average over actions of Q−log⁡πQ - \log \pi. Say the sampled QQ is 6.06.0 and log⁡π(a∣s)=−1.0\log \pi(a \mid s) = -1.0. The target is 6.0−(−1.0)=7.06.0 - (-1.0) = 7.0. Check: the entropy part raises the target when the policy is less certain, since −log⁡π-\log \pi is larger.
  4. Update the two Q-functions. The target is r+γVtarget(s′)r + \gamma V_{\text{target}}(s'). With γ=0.9\gamma = 0.9 and Vtarget(s′)=5.0V_{\text{target}}(s') = 5.0, the target is 1+0.9×5.0=5.51 + 0.9 \times 5.0 = 5.5. If Q1(s,a)=4.0Q_1(s, a) = 4.0, the squared error is 12(4.0−5.5)2=1.125\tfrac{1}{2}(4.0 - 5.5)^2 = 1.125. Check: the loss falls as Q1Q_1 moves toward 5.55.5. The same update is done for Q2Q_2.
  5. Update the policy. Draw noise ϵ=1.0\epsilon = 1.0 and form the action a=μ+σϵa = \mu + \sigma \epsilon with μ=0.2\mu = 0.2 and σ=0.5\sigma = 0.5, which gives a=0.7a = 0.7. The policy loss is log⁡π(a∣s)−min⁡(Q1,Q2)(s,a)\log \pi(a \mid s) - \min(Q_1, Q_2)(s, a). Say log⁡π=−0.9\log \pi = -0.9 and the smaller Q is 6.06.0. The loss is −0.9−6.0=−6.9-0.9 - 6.0 = -6.9. Check: gradient descent makes the policy favour actions with high QQ, while the log⁡π\log \pi term stops it from collapsing onto one action.
  6. Move the target. The target value network follows the value network slowly. With smoothing τ=0.1\tau = 0.1, a value network output of 7.07.0 and an old target of 5.05.0 give a new target of 0.1×7.0+0.9×5.0=5.20.1 \times 7.0 + 0.9 \times 5.0 = 5.2.

Steps 3 to 6 run on gradient updates, and step 1 runs on every environment step. Each environment step is followed by one or more updates; how many is a hyperparameter.

The math

The objective is the sum of expected reward and expected policy entropy, weighted by a temperature α\alpha:

J(π)=∑tE(st,at)∼ρπ[r(st,at)+α H(π(⋅∣st))].J(\pi) = \sum_{t} \mathbb{E}_{(s_t, a_t) \sim \rho_\pi}\left[ r(s_t, a_t) + \alpha\, \mathcal{H}\big(\pi(\cdot \mid s_t)\big) \right].

The entropy H\mathcal{H} measures how spread out the policy is over actions. A larger α\alpha gives entropy more weight relative to reward. The reward scale and α\alpha trade off against each other, so tuning one changes the balance set by the other.

Step 3 in symbols

The soft value network VψV_\psi is trained to match the average over actions of Qθ−log⁡πϕQ_\theta - \log \pi_\phi:

JV(ψ)=Es∼D[12(Vψ(s)−Ea∼πϕ[Qθ(s,a)−log⁡πϕ(a∣s)])2].J_V(\psi) = \mathbb{E}_{s \sim \mathcal{D}}\left[ \tfrac{1}{2}\Big( V_\psi(s) - \mathbb{E}_{a \sim \pi_\phi}\big[ Q_\theta(s, a) - \log \pi_\phi(a \mid s) \big] \Big)^2 \right].

Here D\mathcal{D} is the replay buffer.

Step 4 in symbols

The Q-functions are trained toward the soft Bellman target, using a target value network VψˉV_{\bar{\psi}}:

Q^(s,a)=r(s,a)+γ Es′∼p[Vψˉ(s′)],JQ(θ)=E(s,a)∼D[12(Qθ(s,a)−Q^(s,a))2].\hat{Q}(s, a) = r(s, a) + \gamma\, \mathbb{E}_{s' \sim p}\big[ V_{\bar{\psi}}(s') \big], \qquad J_Q(\theta) = \mathbb{E}_{(s, a) \sim \mathcal{D}}\left[ \tfrac{1}{2}\big( Q_\theta(s, a) - \hat{Q}(s, a) \big)^2 \right].

The paper trains two Q-functions with parameters θ1\theta_1 and θ2\theta_2. The policy update uses the smaller of their values, and this reduces the positive bias that value methods pick up from maximising over noisy estimates.

Step 5 in symbols

The policy is written as a deterministic transformation of noise, a=fϕ(ϵ;s)a = f_\phi(\epsilon; s) with ϵ\epsilon drawn from a fixed distribution such as a standard normal. This is the Reparameterization Trick. The policy objective becomes

Jπ(ϕ)=Es∼D, ϵ[log⁡πϕ(fϕ(ϵ;s)∣s)−Qθ(s,fϕ(ϵ;s))].J_\pi(\phi) = \mathbb{E}_{s \sim \mathcal{D},\, \epsilon}\Big[ \log \pi_\phi\big(f_\phi(\epsilon; s) \mid s\big) - Q_\theta\big(s, f_\phi(\epsilon; s)\big) \Big].

Because QθQ_\theta is a differentiable network, gradients flow through fϕf_\phi into the action. This is the same kind of gradient that DDPG uses for deterministic policies, now available for stochastic ones.

Step 6 in symbols

The target network moves by a moving average:

ψˉ←τψ+(1−τ)ψˉ.\bar{\psi} \leftarrow \tau \psi + (1 - \tau)\bar{\psi}.

A value of τ\tau near 11 makes the target follow the value network closely, and a small τ\tau makes it slow.

Worked examples

Example
1. One critic target (tiny)

Reward r=2r = 2, discount γ=0.5\gamma = 0.5, and the next state's target value Vtarget(s′)=4V_{\text{target}}(s') = 4.

  1. Discounted next value: 0.5×4=20.5 \times 4 = 2.
  2. Add the reward: 2+2=42 + 2 = 4.
  3. Target: Q^=4\hat{Q} = 4.
  4. Interpretation: if the current Q-estimate is 33, the update pushes it up, with a squared error of 12(3−4)2=0.5\tfrac{1}{2}(3 - 4)^2 = 0.5.
Example
2. The policy objective for two samples (standard)

Two actions are sampled for one state. The first has log⁡π=−1.0\log \pi = -1.0 and Q=5.0Q = 5.0. The second has log⁡π=−0.5\log \pi = -0.5 and Q=4.0Q = 4.0.

  1. First term: log⁡π−Q=−1.0−5.0=−6.0\log \pi - Q = -1.0 - 5.0 = -6.0.
  2. Second term: log⁡π−Q=−0.5−4.0=−4.5\log \pi - Q = -0.5 - 4.0 = -4.5.
  3. Average loss: (−6.0+(−4.5))/2=−5.25(-6.0 + (-4.5)) / 2 = -5.25.
  4. Interpretation: the first action has the lower term because its Q is higher, even though it is less probable under the policy. The log⁡π\log \pi part of each term is what keeps the policy from putting all its weight on the single highest-Q action.
Example
3. The entropy share of the objective (realistic)

Suppose a continuous-control task gives reward 11 per step, the temperature is α=0.2\alpha = 0.2, and the policy's entropy for one action dimension is H=0.5\mathcal{H} = 0.5 nats at each step. Take an episode of 10001000 steps.

  1. Total reward: 1000×1=10001000 \times 1 = 1000.
  2. Total entropy bonus: 1000×0.2×0.5=1001000 \times 0.2 \times 0.5 = 100.
  3. Share of the objective from entropy: 100/(1000+100)=0.091100 / (1000 + 100) = 0.091.
  4. If the reward were scaled by 1010 instead, the total reward would be 1000010000 and the entropy share would be 100/10100≈0.010100 / 10100 \approx 0.010.
  5. Interpretation: scaling the reward shrinks the entropy share, so the reward scale and the temperature must be tuned together for each environment.

Try it: the entropy term for one step is αH\alpha \mathcal{H} with α=0.1\alpha = 0.1 and H=2.0\mathcal{H} = 2.0, and the reward for that step is 3.03.0. What share of the step's objective comes from entropy? Answer: 0.1×2.0=0.20.1 \times 2.0 = 0.2, and the total is 3.0+0.2=3.23.0 + 0.2 = 3.2, so the share is 0.2/3.2=0.06250.2 / 3.2 = 0.0625.

Results

The paper evaluates on OpenAI Gym continuous-control tasks and on the rllab Humanoid task, which has 21 action dimensions. It compares SAC with DDPG, PPO and soft Q-learning, and reports evaluation return over training.

On the easier tasks, SAC performs about as well as the baselines. On the harder tasks, including Humanoid, it learns faster and reaches higher return. DDPG makes little or no progress on the hardest tasks. PPO needs far more environment steps to reach similar return, because it cannot reuse data. Soft Q-learning learns the tasks but more slowly.

The limits are clear from the paper. In the 2018 version the temperature is a hyperparameter tuned per environment, and the results are on simulated benchmarks, not on real robots. The follow-up paper addresses the temperature by adjusting it automatically.

Common mistakes and tradeoffs

It is tempting to think that SAC is simply DDPG with noise added. In fact the policy is trained to maximise entropy as well as value, the critic is a soft value function, and the policy update uses the smaller of two Q-functions. Adding exploration noise to a deterministic policy does not reproduce those properties.

It is tempting to think the entropy bonus is just an exploration trick that can be switched off once training is done. In fact the entropy term is part of the objective the policy is trained on. The deterministic policy used for evaluation is a separate choice made after training.

It is tempting to think a larger reward always helps learning. In fact the reward and the entropy term sit in one objective, so scaling the reward changes how much the entropy term matters. The two must be tuned together.

When to use SAC: continuous actions, expensive environment steps, and a need for off-policy reuse. When not to use it: discrete action spaces, where other methods are simpler. Alternatives include PPO when parallel simulation is cheap, and TD3, a deterministic off-policy method, when a simpler update is preferred.

Reading guide

Read the paper in this order, with the lessons that cover what each part needs.

Check yourself

QuestionWhat does the entropy term in the SAC objective encourage, and why does that help learning?Show answer

It encourages the policy to stay spread out over actions. This keeps exploration going while the policy still earns reward, so the agent keeps trying actions it does not yet know well.

QuestionA critic target has reward r=1r = 1, discount γ=0.9\gamma = 0.9, and next-state target value Vtarget(s′)=10V_{\text{target}}(s') = 10. What is the target Q^\hat{Q}?Show answer

The discounted next value is 0.9×10=90.9 \times 10 = 9. The target is 1+9=101 + 9 = 10. The Q-estimate is pushed toward 1010.

QuestionWhy does SAC use a reparameterized action a=fϕ(ϵ;s)a = f_\phi(\epsilon; s) when computing the policy gradient?Show answer

Because the action is a differentiable function of the noise and the policy parameters, the gradient of the Q-function with respect to the action can flow back into the policy. The paper notes that this gives a lower-variance estimator than the likelihood-ratio gradient.

QuestionA policy gives an action log⁡π=−2.0\log \pi = -2.0 with Q=3.0Q = 3.0. What is its policy loss term log⁡π−Q\log \pi - Q?Show answer

The loss term is −2.0−3.0=−5.0-2.0 - 3.0 = -5.0. Minimising it favours actions with high Q, and the log⁡π\log \pi part keeps the policy from becoming too concentrated.

Sources

Read the full paper

Tuomas Haarnoja, Aurick Zhou, Pieter Abbeel et al., 2018. arXiv:1801.01290