Soft Q-Learning
Soft Q-learning adds an entropy bonus to every backup, so the agent keeps several good actions alive instead of collapsing onto one greedy choice.
Why Does This Exist?
Ordinary Q-learning backs up the single best next value: . That hard max has two sharp edges. First, it overestimates, because the maximum over noisy estimates is biased upward. Second, it collapses the policy onto one action. If steering left and steering right around an obstacle are both good, greedy Q-learning picks one and forgets the other. Block that corridor later and the agent has no fallback.
Soft Q-learning fixes both edges with one change: maximize reward plus entropy. The agent is paid for high return and for staying unpredictable, with a temperature setting the exchange rate. This page covers that mechanism and the soft backup it produces. The broader case for entropy objectives lives in maximum entropy RL, and the actor-critic successor that made the idea practical at scale is soft actor-critic.
The one prerequisite is the Bellman backup itself: a Q-value is learned by regressing toward reward plus the discounted value of the next state, as in deep Q-networks. Everything here is that same loop with a softer target.
Think of It Like This
Two soup stalls and a hungry regular
You eat lunch on a street with two soup stalls. Stall A serves a 9 out of 10 bowl, stall B a 7 out of 10. A greedy friend tries A once, then eats at A every day for a year and never learns B improved its recipe.
You play it softer. You visit A about three times out of four and B about one time in four. You still eat very well on average, and you keep tasting both menus, so when A closes for renovations you already know exactly where your second-favorite bowl is.
The visit frequencies are the policy, the soup scores are the Q-values, and your willingness to keep tasting B is the temperature . Turn it down and you become your greedy friend. Turn it up and you eat everywhere at random.
Where it stops: soup scores stay fixed while you taste around. Q-values move as the agent learns, so the real algorithm is tasting menus that the tasting itself keeps rewriting.
How It Actually Works
Standard RL maximizes expected discounted return. Soft Q-learning adds the policy entropy at every step, scaled by :
Here is Shannon entropy, a number that is large when the policy spreads probability across many actions and zero when it always picks one. The optimal policy for this objective has a closed form: it is the Boltzmann distribution with Q-values as negative energies,
where the soft state value is the log-sum-exp over actions:
In plain words: exponentiate each Q-value divided by temperature, add them up, take the log, multiply back by temperature. When goes to zero this recovers the hard . When is large the policy goes nearly uniform. The network is trained with the usual squared error against the soft target , reusing a frozen target network for the term.
Worked example
Two actions, temperature , discount , reward , current estimate , next-state values and .
Scale by temperature: and . Exponentiate: and . Add: . Log and rescale: .
Notice sits above the hard max . The surplus is the entropy bonus: the policy below has entropy , and , where is the expected Q under the policy.
The backup: . Against the current , the TD error is and the half-squared loss is . The implied policy is and : mostly the better action, with the runner-up kept firmly alive.
Why continuous actions needed a successor
In continuous action spaces the sum becomes an integral with no closed form, and sampling from means sampling an unnormalized density. The original paper used Stein variational gradient descent particles for this, which was fiddly to tune. Soft actor-critic kept the entropy objective and replaced the sampler with a plain reparameterized Gaussian actor, which is why SAC is what people actually run.
Code
import math
def soft_value(q: list[float], alpha: float) -> float: """Log-sum-exp soft value, stabilized by subtracting the max.""" m = max(q) return m + alpha * math.log(sum(math.exp((v - m) / alpha) for v in q))
def soft_policy(q: list[float], alpha: float) -> list[float]: """Boltzmann policy over a discrete action set.""" m = max(q) exps = [math.exp((v - m) / alpha) for v in q] z = sum(exps) return [v / z for v in exps]
alpha: float = 2.0gamma: float = 0.9q_next = [4.0, 2.0]v_soft = soft_value(q_next, alpha)target = 1.5 + gamma * v_softtd_error = target - 5.0loss = 0.5 * td_error**2print(f"{v_soft:.4f}") # -> 4.6265print(f"{target:.4f}") # -> 5.6639print(f"{td_error:.4f}") # -> 0.6639print(f"{loss:.4f}") # -> 0.2204print([round(p, 4) for p in soft_policy(q_next, alpha)]) # -> [0.7311, 0.2689]Watch Out For
Setting the temperature by feel instead of tuning it
The failure looks like this: with too high the agent dithers and never commits, and returns plateau far below greedy Q-learning. With too low you paid for the soft machinery and got hard Q-learning back, overestimation included. Both regimes look like ordinary bad training, so people blame the network size or the learning rate first.
The symptom that points at temperature is the policy entropy itself. Log the mean entropy of over evaluation states. If it sits near for actions, you are close to uniform and is too high. If it sits near zero from the first thousand steps, is too low. Tune until entropy declines slowly over training, or use the automatic temperature tuning from the SAC follow-up, which treats a target entropy as a constraint.
Letting the soft target leak gradients into the target network
The soft target is computed from a frozen copy of the Q-network, and that copy must stay frozen during the gradient step. If the implementation accidentally differentiates through with respect to the online weights, the loss gets a second pathway that pushes values up without bound, and training diverges upward instead of converging.
The symptom is unmistakable once you know it: Q-values climb past any plausible return scale within a few thousand steps while the loss stays small. The fix is to detach or stop-gradient the target branch before computing the loss, and to assert after each optimizer step that the target weights did not move.
The Quick Version
- Soft Q-learning maximizes reward plus policy entropy, with temperature setting the exchange rate between the two.
- The hard becomes a log-sum-exp soft value, which sits above the max by exactly the entropy bonus.
- The optimal policy is Boltzmann in the Q-values, so near-optimal actions keep real probability instead of being discarded.
- As goes to zero the method reduces to ordinary Q-learning, overestimation and all.
- Continuous action spaces need approximate samplers, which is why soft actor-critic replaced the original SVGD scheme.
- Tune by watching policy entropy, and always detach the frozen target branch from the gradient.