Actor–Critic & Soft Actor–Critic
Combining value estimates with policies.
An actor proposes, a critic evaluates. That division of labor, which goes back to Barto, Sutton and Anderson's pole-balancing learner of 1983, runs from A3C's parallel workers to the deterministic critics that steer simulated robot arms, and to soft actor–critic, which pays the actor for staying random and, in doing so, turns the Bellman max into a log-sum-exp.
What you’ll learn
- Write the advantage actor–critic loss with -step returns and an entropy bonus, and explain how A3C and A2C decorrelate data without replay.
- State and prove the deterministic policy-gradient theorem, and explain how DDPG and TD3 turn it into off-policy algorithms.
- Derive the soft Bellman equations and the Boltzmann optimal policy of maximum-entropy RL, and prove that the soft backup is a contraction.
- Write SAC's critic, actor and temperature losses, including the reparameterization trick and the tanh-squashing correction.
Advantage actor–critic, in parallel: A3C and A2C
An actor–critic keeps two function approximators: the policy , the actor, and a value estimate , the critic, whose job is to turn returns into low-variance advantage estimates. Its deep-learning form is A3C (Mnih et al., 2016), which minimizes, over a short rollout,
Without replay, consecutive samples from one environment are strongly correlated. A3C's answer was to run many actor–learners in parallel, 16 CPU threads in the paper, each in its own copy of the environment, applying gradients asynchronously to shared parameters. At any moment the workers are in different states, so the combined update is far less correlated, and the data stay on-policy. Mnih et al. reported state-of-the-art Atari results in half the training time of earlier GPU agents, on a single multi-core CPU, and the same method handled continuous control and navigation in 3D mazes.
The synchronous variant A2C waits for all workers and averages their gradients. It uses GPUs better and, in practice, performs at least as well, which suggests that the asynchrony mattered less than the parallel decorrelation. Asynchrony has a cost anyway: workers compute gradients with stale parameters, so their data come from slightly older policies. At larger scale, IMPALA (Espeholt et al., 2018) decouples acting from learning entirely and corrects for the lag with V-trace importance weights (Off-Policy Learning & Importance Sampling).
Deterministic policy gradients: DPG, DDPG and TD3
For continuous actions, a stochastic policy gradient averages over actions, and its variance grows as the policy narrows. Silver et al. (2014) showed that a deterministic policy has its own gradient theorem, which needs only the critic's slope in the action.
Write the value through the critic, , with . The parameters enter through the action and, at a fixed action, through at the next state:
This is the recursion of the stochastic theorem, a local term plus the discounted expected gradient at the next state. Unroll it and average over the start distribution:
No integral over actions appears, so the gradient can be estimated from states in a replay buffer collected by any exploratory behavior: an off-policy actor–critic. (Strictly, averaging over the behavior's states instead of gives an approximation of the gradient, as in Degris, White & Sutton's off-policy actor–critic, 2012.) DDPG (Lillicrap et al., 2016) made this deep with DQN's machinery: a replay buffer, target networks updated softly, with , batch normalization, and exploration noise added to the actor's actions.
DDPG is brittle, and TD3 (Fujimoto, van Hoof & Meger, 2018) traced much of the brittleness to the critic's overestimation, the same maximization bias as in Q-learning: an actor that climbs seeks out exactly the actions where the critic errs upward. TD3's three fixes:
| Fix | Rule | Reason |
|---|---|---|
| Clipped double Q-learning | an upward error must fool both critics at once; the resulting underestimation is safer, because the actor does not seek it out | |
| Delayed policy updates | update the actor and the targets once per 2 critic updates | let the critic's error shrink before the actor exploits it |
| Target policy smoothing | , | similar actions should have similar values; smoothing denies the actor narrow, spurious peaks of |
What does the deterministic policy gradient require from the critic that the likelihood-ratio (stochastic) policy gradient does not?
- Nothing: both need only sampled returns
- The transition model
- Importance weights between the behavior and target policies
- The gradient of with respect to the action, at
Show the answer
D. The gradient of with respect to the action, at
The deterministic gradient chains with , so it needs a critic that is differentiable in the action; the likelihood-ratio estimator needs only the value of the sampled action. In exchange, DPG avoids integrating over actions and is cheap to estimate off-policy.
Maximum-entropy RL and the soft Bellman equation
Maximum-entropy RL adds the policy's entropy to every reward, , with a temperature (Ziebart, 2010; Haarnoja et al., 2017). Among policies with similar returns it prefers the most random one, which keeps exploring, resists premature collapse onto one action, and learns several ways of solving a task instead of one.
Fix a state and a vector of action values . Soft policy improvement maximizes expected value plus times entropy:
Let and . Then , and the objective becomes a KL divergence:
The maximum is , attained only at the Boltzmann policy . The hard max is the limit . The KL-regularized optimum of RLHF in Fine-Tuning, RLHF & Prompting is the same computation with a reference policy in place of the uniform one.
A state has two actions, both with , and . What is its soft value ?
Show the answer
1.69
. The soft value exceeds every action's value by the entropy of the optimal (here uniform) policy, : in maximum-entropy RL, having options is itself worth something. With 10 equally good actions the bonus would be .
Soft actor–critic
SAC (Haarnoja, Zhou, Abbeel & Levine, 2018) is soft policy iteration with function approximation, run off-policy from a replay buffer. Soft policy evaluation converges to , because its backup is an ordinary Bellman backup with an entropy-augmented reward. Soft policy improvement projects the Boltzmann policy of the current critic onto the policy class, , which never lowers the soft values. Alternating the two converges to the best policy in , in the tabular case (their Theorem 1).
For continuous actions the actor is a Gaussian squashed by tanh: with and . Because is a differentiable function of and the noise, the actor's loss can be differentiated straight through the critic (the reparameterization trick), usually with much lower variance than the likelihood-ratio estimator. The squashing changes densities, . The original SAC already took the minimum of two critics, as TD3 does; a later version dropped its separate state-value network and tuned the temperature automatically, keeping the policy's entropy near a target, by default (Haarnoja et al., 2018b).
import torch
import torch.nn.functional as F
def sac_losses(batch, actor, q1, q2, q1_targ, q2_targ, log_alpha, gamma=0.99, target_entropy=-6.0):
s, a, r, s2, done = batch
alpha = log_alpha.exp()
with torch.no_grad(): # soft Bellman target
a2, logp2 = actor.sample(s2) # reparameterized tanh-Gaussian
q_next = torch.min(q1_targ(s2, a2), q2_targ(s2, a2)) - alpha * logp2
y = r + gamma * (1.0 - done) * q_next
critic_loss = F.mse_loss(q1(s, a), y) + F.mse_loss(q2(s, a), y)
a_new, logp = actor.sample(s) # gradients flow through a_new
q_new = torch.min(q1(s, a_new), q2(s, a_new))
actor_loss = (alpha.detach() * logp - q_new).mean()
alpha_loss = -(log_alpha * (logp.detach() + target_entropy)).mean()
return critic_loss, actor_loss, alpha_lossThe three SAC losses, each minimized by its own optimizer over its own parameters (critics, actor, ); the target critics track the critics by Polyak averaging. A target entropy of suits a 6-dimensional action space.
| Aspect | DDPG | TD3 | SAC |
|---|---|---|---|
| Policy | deterministic | deterministic | stochastic tanh-Gaussian |
| Exploration | noise added to actions | noise added to actions | the policy's own entropy |
| Critic target | , smoothed | , | |
| Actor update | the same, every second critic update | reparameterized gradient of |
Key takeaways
- Advantage actor–critic (A2C, A3C) combines -step advantages, an entropy bonus and a shared critic; parallel workers decorrelate on-policy data without replay, and IMPALA corrects worker lag with V-trace.
- Deterministic policy gradient: , with no integral over actions, hence off-policy learning from replay; DDPG adds DQN's machinery, and TD3 adds clipped double Q, delayed actor updates and target smoothing.
- Maximum-entropy RL rewards the policy's entropy; its optimal values satisfy a soft Bellman equation with a log-sum-exp, its optimal policy is Boltzmann (the Gibbs variational principle), and the soft backup is a -contraction.
- SAC performs soft policy iteration off-policy, with twin soft critics, a reparameterized tanh-Gaussian actor and a temperature tuned toward a target entropy such as .
- Open questions include sample efficiency at scale, loss of plasticity, what entropy really buys, expressive multimodal policies, and theory for deep actor–critics.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Why could A3C train deep networks without a replay buffer?
- Its entropy bonus decorrelates the samples
- It used a target network instead
- Parallel workers in different states make each combined update much less correlated, while the data stay on-policy
- Its -step returns remove the correlation between successive states
Show the answer
C. Parallel workers in different states make each combined update much less correlated, while the data stay on-policy
Replay decorrelates by sampling old transitions, which makes data off-policy. A3C decorrelates across space instead of time: many copies of the environment, each in a different state, contribute to the same parameters.
TD3 computes its target with the minimum of two critics. Which problem does this address?
- Slow exploration in continuous action spaces
- Overestimated action values, which the actor actively seeks out and exploits
- The variance of the policy gradient
- The cost of computing
Show the answer
B. Overestimated action values, which the actor actively seeks out and exploits
An actor that climbs concentrates on actions where the critic errs upward, and bootstrapping spreads the error. Taking the minimum of two independently trained critics biases the target downward, which is safer because the actor does not seek out underestimates.
As the temperature , the soft Bellman optimality equation becomes:
- The standard Bellman optimality equation, with
- The Bellman equation of the uniform random policy
- Undefined, because the log-sum-exp diverges
- The Bellman equation of SARSA's ε-greedy policy
Show the answer
A. The standard Bellman optimality equation, with
lies between and , so it tends to the max as , and the Boltzmann policy concentrates on the greedy actions.
SAC tunes its temperature by minimizing for a target entropy . When does this lower ?
- When the policy's entropy is below the target
- When the critic's loss increases
- Never: only grows during training
- When the policy's entropy is above the target
Show the answer
D. When the policy's entropy is above the target
The derivative with respect to is . Gradient descent lowers when the entropy exceeds the target, so less encouragement to explore is needed, and raises it when the entropy falls below the target.
Further reading
- V. Mnih, A. P. Badia, M. Mirza, A. Graves, T. Lillicrap, T. Harley, D. Silver & K. Kavukcuoglu, "Asynchronous methods for deep reinforcement learning," ICML (2016).
- D. Silver, G. Lever, N. Heess, T. Degris, D. Wierstra & M. Riedmiller, "Deterministic policy gradient algorithms," ICML (2014).
- S. Fujimoto, H. van Hoof & D. Meger, "Addressing function approximation error in actor-critic methods," ICML (2018).
- T. Haarnoja, A. Zhou, P. Abbeel & S. Levine, "Soft actor-critic: Off-policy maximum entropy deep reinforcement learning with a stochastic actor," ICML (2018).
- R. S. Sutton & A. G. Barto, Reinforcement Learning: An Introduction, ch. 13 (MIT Press, 2nd ed., 2018).