Skip to content

Deep reinforcement learning and decision models · Method reference · DRL010 · Algorithm

Soft actor–critic (SAC)

Learn a stochastic policy that balances expected reward with entropy, supporting exploration and off-policy reuse of interactions. This entry uses the commonly deployed twin-Q formulation;

Explained here Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

Documented in the Academy only. There's no Set up button.

Availability
Explained here
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Learn a stochastic policy that balances expected reward with entropy, supporting exploration and off-policy reuse of interactions. This entry uses the commonly deployed twin-Q formulation; the original and later SAC papers differ in details such as an explicit value network and temperature adaptation.

Question / hypotheses

Which policy maximizes reward while retaining a chosen incentive for stochastic behavior? Entropy is part of the optimized objective, so the learned solution can differ from the unregularized maximum-return policy.

Architecture / computational structure

A stochastic actor commonly predicts Gaussian mean and log scale, followed by a tanh action transform. Twin critics and delayed targets estimate soft action values from replay. Reparameterization supports actor gradients, and a learned log-temperature may enforce a target entropy.

Assumptions

Action densities must include the change-of-variables correction for bounded transforms. Replay coverage and critic accuracy matter, especially for actions proposed by the current actor. Reward and entropy scales must be compatible; partial observation needs adequate state/history encoding.

Formula / computational definition

y=r+γ(1−d)[min⁡iQϕˉi(s′,a′)−αlog⁡πθ(a′∣s′)]y=r+\gamma(1-d)[\min_iQ_{\bar\phi_i}(s^\prime,a^\prime)-\alpha\log\pi_\theta(a^\prime\mid s^\prime)], with a′∼πθa^\prime\sim\pi_\theta. The actor minimizes E[αlog⁡πθ(a∣s)−min⁡iQϕi(s,a)]\mathbb E[\alpha\log\pi_\theta(a\mid s)-\min_iQ_{\phi_i}(s,a)]; targets are detached.

Parameters and notation

Learned parameters are actor, twin critics, and optionally log alpha; target critics are delayed copies. Hyperparameters include target entropy or fixed alpha, learning rates, action distribution bounds, discount, replay size, updates per environment step, batch size, and target averaging.

Fitting, fine-tuning, and adaptation

Transfer an encoder and compatible stochastic policy, then recalibrate critics and entropy scale for the new reward/action space. Start with a frozen-feature probe policy where useful, then unfreeze gradually. Refresh replay and reconsider target entropy when action dimension changes; old temperature values are not universal.

How to use and calibrate

Report return and entropy separately, stating whether evaluation samples actions or uses the actor mean. Monitor temperature, log-probability stability, critic disagreement, and action saturation. Compare algorithms with matched interactions and policy-selection protocols rather than only their best seed.

Example application

Train a stochastic torque policy for continuous locomotion. The example calculates soft critic targets and the actor objective using supplied corrected log densities; it does not implement the Gaussian reparameterization or tanh Jacobian itself.

Interpretation / pitfalls

Watch out

Missing the tanh log-Jacobian produces an incorrect objective. Entropy coefficients depend on reward scale and units. SAC is off-policy, but standard SAC can fail on a fixed offline dataset through unsupported action-value extrapolation; CQL and IQL address different offline restrictions.

Python and R code examples

Scope: computational component described in the application subsection; not a complete estimator, neural trainer, or environment loop. Dependencies/validation: Python with NumPy executed successfully; base R not executed. Targets and supplied network outputs are ordinary arrays here, so this does not validate automatic-differentiation boundaries.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — computational component described in the application subsection; not a complete estimator, neural trainer, or environment loop. · needs numpy

import numpy as np
q1=np.array([2.,1.]); q2=np.array([1.8,1.2]); logp=np.array([-.7,-1.1]); alpha=.2
reward=np.array([1.,0.]); terminal=np.array([0.,1.])
soft_value=np.minimum(q1,q2)-alpha*logp
target=reward+.99*(1-terminal)*soft_value
actor_loss=np.mean(alpha*logp-np.minimum(q1,q2))
assert target[1]==0; print(target,actor_loss)
R

Component demo, not a trained model — computational component described in the application subsection; not a complete estimator, neural trainer, or environment loop.

q1 <- c(2,1); q2 <- c(1.8,1.2); logp <- c(-.7,-1.1); alpha <- .2
reward <- c(1,0); terminal <- c(0,1)
soft_value <- pmin(q1,q2)-alpha*logp
target <- reward+.99*(1-terminal)*soft_value
actor_loss <- mean(alpha*logp-pmin(q1,q2))
stopifnot(target[2]==0); print(target); print(actor_loss)

Sources

From the CodeSplash reference, entry DRL010.