Reinforcement Learning series

03. Q-learning

2026-07-12

In the previous post, we looked at policy gradient methods. The basic idea there was to parameterize the policy directly as πθ(as)\pi_\theta(a \mid s), and then update θ\theta so that the expected return increases.

Q-learning takes a different route. Instead of directly optimizing the policy, it tries to learn an ideal state-action value function, which we will call QQ^* for now. Intuitively, Q(s,a)Q^*(s,a) tells us how good it is to take action aa in state ss, assuming we act optimally afterward.

If we had this ideal Q-function, then we could get an optimal policy by choosing the action with the largest Q-value:

π(s)argmaxaAQ(s,a).\pi^*(s) \in \arg\max_{a \in \mathcal{A}} Q^*(s,a).

In practice, Q-learning learns an approximation QϕQQ_\phi \approx Q^*. Given the learned QϕQ_\phi, the greedy policy is

πgreedy(s)argmaxaAQϕ(s,a).\pi^{\mathrm{greedy}}(s) \in \arg\max_{a \in \mathcal{A}} Q_\phi(s,a).

So the policy is derived from the learned Q-function. If QϕQ_\phi were exactly equal to QQ^*, then the greedy policy would be optimal. In reality, QϕQ_\phi is only an approximation, so the greedy policy is only as good as the learned Q-values.

Bellman optimality

Optimal policy and value functions

Now let us define the optimal Q-function more precisely.

Recall that the state value function and state-action value function are defined as

Vπ(s)=Eτpπ(τs0=s)[t=0T1γtrt],Qπ(s,a)=Eτpπ(τs0=s, a0=a)[t=0T1γtrt].\begin{aligned} V^\pi(s) &= \mathbb{E}_{\tau \sim p_\pi(\tau \mid s_0=s)} \left[ \sum_{t=0}^{T-1}\gamma^t r_t \right], \\ Q^\pi(s,a) &= \mathbb{E}_{\tau \sim p_\pi(\tau \mid s_0=s,\ a_0=a)} \left[ \sum_{t=0}^{T-1}\gamma^t r_t \right]. \end{aligned}

We say a policy π\pi^* is optimal if it is at least as good as any other policy from every starting state:

Vπ(s)Vπ(s)sS, π.V^{\pi^*}(s) \ge V^\pi(s) \quad \forall s \in \mathcal{S},\ \forall \pi .

At first glance, it is not obvious that an optimal policy exists. Different states might seem to prefer different policies, so it is not clear that one policy can dominate globally. However, we will soon establish that an optimal policy always exists.

If an optimal policy π\pi^* exists, we define the optimal value function and optimal Q-function by

V(s)=Vπ(s),Q(s,a)=Qπ(s,a).\begin{aligned} V^*(s) &= V^{\pi^*}(s), \\ Q^*(s,a) &= Q^{\pi^*}(s,a). \end{aligned}

Bellman optimality equation for VV^*

Recall the one-step Bellman equation for a fixed policy π\pi:

Vπ(s)=Eaπ(s), (r,s)p(s,a)[r+γVπ(s)].V^\pi(s) = \mathbb{E}_{a \sim \pi(\cdot \mid s),\ (r,s') \sim p(\cdot \mid s,a)} \left[ r+\gamma V^\pi(s') \right].

If an optimal policy π\pi^* exists, the same one-step relation should hold for VV^*:

V(s)=Eaπ(s), (r,s)p(s,a)[r+γV(s)].V^*(s) = \mathbb{E}_{a \sim \pi^*(\cdot \mid s),\ (r,s') \sim p(\cdot \mid s,a)} \left[ r+\gamma V^*(s') \right].

Now the question is: which action should π\pi^* choose? If we take action aa first and then act optimally afterward, the expected value is

E(r,s)p(s,a)[r+γV(s)].\mathbb{E}_{(r,s') \sim p(\cdot \mid s,a)} \left[ r+\gamma V^*(s') \right].

So an optimal policy would choose an action that maximizes this quantity. Therefore, we expect VV^* to satisfy

V(s)=maxaAE(r,s)p(s,a)[r+γV(s)].V^*(s) = \max_{a \in \mathcal{A}} \mathbb{E}_{(r,s') \sim p(\cdot \mid s,a)} \left[ r+\gamma V^*(s') \right].

This is called the Bellman optimality equation. Just as we did for the Bellman equation, we can establish a similar theorem.

Theorem. Assume γ(0,1)\gamma \in (0,1), S<|\mathcal{S}|<\infty, A<|\mathcal{A}|<\infty, and rR<|r|\le R<\infty almost surely. Then the optimal value function V:SRV^*:\mathcal{S}\to\mathbb{R} exists and satisfies the Bellman optimality equation:

V(s)=maxaAE(r,s)p(s,a)[r+γV(s)].V^*(s) = \max_{a \in \mathcal{A}} \mathbb{E}_{(r,s') \sim p(\cdot \mid s,a)} \left[ r+\gamma V^*(s') \right].

Define the Bellman optimality operator TT^* on value functions by

(TV)(s)=maxaAE(r,s)p(s,a)[r+γV(s)].(T^*V)(s) = \max_{a \in \mathcal{A}} \mathbb{E}_{(r,s') \sim p(\cdot \mid s,a)} \left[ r+\gamma V(s') \right].

Then the Bellman optimality equation can be written as

TV=V.T^*V^*=V^*.

Conversely, if a function V:SRV:\mathcal{S}\to\mathbb{R} satisfies

TV=V,T^*V=V,

then V=VV=V^*.

Moreover, for any bounded initial function V0V_0,

(T)kV0Vas k.\left(T^*\right)^k V_0 \to V^* \quad \text{as } k\to\infty.

Finally, any greedy policy of the form

π(s)argmaxaAE(r,s)p(s,a)[r+γV(s)]\pi^*(s) \in \arg\max_{a \in \mathcal{A}} \mathbb{E}_{(r,s') \sim p(\cdot \mid s,a)} \left[ r+\gamma V^*(s') \right]

is an optimal policy.

Proof

1. Existence and uniqueness of the fixed point

First, TT^* is a strict contraction with respect to \|\cdot\|_\infty:

TV1TV2=maxsS(TV1)(s)(TV2)(s)=maxsSmaxaAE[r+γV1(s)s,a]maxaAE[r+γV2(s)s,a]maxsSmaxaAγE[V1(s)V2(s)s,a]maxsSmaxaAγmaxsSV1(s)V2(s)=γV1V2.\begin{aligned} \|T^*V_1-T^*V_2\|_\infty &= \max_{s \in \mathcal{S}} \left| (T^*V_1)(s)-(T^*V_2)(s) \right| \\ &= \max_{s \in \mathcal{S}} \left| \max_{a \in \mathcal{A}} \mathbb{E} \left[ r+\gamma V_1(s') \mid s,a \right] - \max_{a \in \mathcal{A}} \mathbb{E} \left[ r+\gamma V_2(s') \mid s,a \right] \right| \\ &\le \max_{s \in \mathcal{S}} \max_{a \in \mathcal{A}} \gamma \mathbb{E} \left[ \left|V_1(s')-V_2(s')\right| \mid s,a \right] \\ &\le \max_{s \in \mathcal{S}} \max_{a \in \mathcal{A}} \gamma \max_{s' \in \mathcal{S}} \left|V_1(s')-V_2(s')\right| \\ &= \gamma \|V_1-V_2\|_\infty . \end{aligned}

So TT^* has a unique fixed point. Denote it by VV^*:

TV=V.T^*V^*=V^*.

At this point, we only know that VV^* is the fixed point of TT^*. We still need to show that it is the optimal value function.

2. Define π\pi^* and show V=VπV^*=V^{\pi^*}

Next, define π\pi^* to be a deterministic policy given by

π(s)argmaxaAE(r,s)p(s,a)[r+γV(s)],\pi^*(s) \in \arg\max_{a \in \mathcal{A}} \mathbb{E}_{(r,s') \sim p(\cdot \mid s,a)} \left[ r+\gamma V^*(s') \right],

where ties in the argmax are broken arbitrarily. We do not yet know that π\pi^* is optimal.

Then, by the fixed point equation and the definition of π\pi^*,

V(s)=maxaAE(r,s)p(s,a)[r+γV(s)]=Eaπ(s), (r,s)p(s,a)[r+γV(s)].\begin{aligned} V^*(s) &= \max_{a \in \mathcal{A}} \mathbb{E}_{(r,s') \sim p(\cdot \mid s,a)} \left[ r+\gamma V^*(s') \right] \\ &= \mathbb{E}_{a \sim \pi^*(\cdot \mid s),\ (r,s') \sim p(\cdot \mid s,a)} \left[ r+\gamma V^*(s') \right]. \end{aligned}

Unrolling this equation until the terminal state gives

V(s)=Eπ[r0+γV(s1)s0=s]=Eπ[r0+γr1+γ2V(s2)s0=s]=Eπ[r0+γr1++γT1rT1+γTV(sT)s0=s]=Eπ[t=0T1γtrts0=s]=Vπ(s).\begin{aligned} V^*(s) &= \mathbb{E}^{\pi^*} \left[ r_0+\gamma V^*(s_1) \mid s_0=s \right] \\ &= \mathbb{E}^{\pi^*} \left[ r_0+\gamma r_1+\gamma^2 V^*(s_2) \mid s_0=s \right] \\ &= \mathbb{E}^{\pi^*} \left[ r_0+\gamma r_1+\cdots+\gamma^{T-1}r_{T-1} +\gamma^T V^*(s_T) \mid s_0=s \right] \\ &= \mathbb{E}^{\pi^*} \left[ \sum_{t=0}^{T-1}\gamma^t r_t \mid s_0=s \right] \\ &= V^{\pi^*}(s). \end{aligned}

So the greedy policy π\pi^* has value VV^*.

3. Show π\pi^* is optimal

It remains to show that no other policy can do better. We use two simple facts:

TπVTVfor any policy π,T^\pi V \le T^*V \quad \text{for any policy } \pi,

and if UVU \le V pointwise, then

TUTV.T^*U \le T^*V.

The first fact holds because TT^* maximizes over actions. The second says that increasing the input value function cannot decrease the Bellman optimality update.

For any policy π\pi,

Vπ=TπVπTVπ.V^\pi = T^\pi V^\pi \le T^*V^\pi .

Applying the monotonicity of TT^* repeatedly,

VπTVπ(T)2Vπ.V^\pi \le T^*V^\pi \le (T^*)^2V^\pi \le \cdots .

Since TT^* is a contraction, (T)kVπV(T^*)^kV^\pi \to V^*. Therefore VπVV^\pi \le V^* for every policy π\pi.

Vπ=VandVπVπ.V^{\pi^*}=V^* \quad \text{and} \quad V^\pi \le V^* \quad \forall \pi.

Thus π\pi^* is an optimal policy, and VV^* is the optimal value function.

Finally, if a function VV satisfies the Bellman optimality equation, then TV=VT^*V=V. Since TT^* has a unique fixed point, V=VV=V^*.

Bellman optimality equation for QQ^*

The same result can be written directly for the optimal state-action value function.

Theorem. Assume γ(0,1)\gamma \in (0,1), S<|\mathcal{S}|<\infty, A<|\mathcal{A}|<\infty, and rR<|r|\le R<\infty almost surely. Then the optimal state-action value function Q:S×ARQ^*:\mathcal{S}\times\mathcal{A}\to\mathbb{R} exists and satisfies the Bellman optimality equation:

Q(s,a)=E(r,s)p(s,a)[r+γmaxaAQ(s,a)].Q^*(s,a) = \mathbb{E}_{(r,s') \sim p(\cdot \mid s,a)} \left[ r+\gamma \max_{a' \in \mathcal{A}} Q^*(s',a') \right].

Define the Bellman optimality operator TT^* on Q-functions by

(TQ)(s,a)=E(r,s)p(s,a)[r+γmaxaAQ(s,a)].(T^*Q)(s,a) = \mathbb{E}_{(r,s') \sim p(\cdot \mid s,a)} \left[ r+\gamma \max_{a' \in \mathcal{A}} Q(s',a') \right].

Then the Bellman optimality equation can be written as

TQ=Q.T^*Q^*=Q^*.

Conversely, if a function Q:S×ARQ:\mathcal{S}\times\mathcal{A}\to\mathbb{R} satisfies

TQ=Q,T^*Q=Q,

then Q=QQ=Q^*.

Moreover, for any bounded initial function Q0Q_0,

(T)kQ0Qas k.\left(T^*\right)^k Q_0 \to Q^* \quad \text{as } k\to\infty.

Finally, any greedy policy of the form

π(s)argmaxaAQ(s,a)\pi^*(s) \in \arg\max_{a \in \mathcal{A}} Q^*(s,a)

is an optimal policy.

Deep Q-Networks (DQN)

From Q-iteration to DQN

Now we can turn the Bellman optimality equation for QQ^* into an algorithm.

The theory above gives us Q-iteration:

Qk+1=TQk,limkQk=Q.\begin{aligned} Q_{k+1} &= T^*Q_k, \\ \lim_{k\to\infty}Q_k &= Q^*. \end{aligned}

In the ideal case, suppose we can store Q(s,a)Q(s,a) for every possible state-action pair, and suppose we know the dynamics well enough to compute the expectation exactly. Then we can start from any initial Q0Q_0 and iterate

Qk+1(s,a)=(TQk)(s,a)=E(r,s)p(s,a)[r+γmaxaAQk(s,a)].Q_{k+1}(s,a) = (T^*Q_k)(s,a) = \mathbb{E}_{(r,s') \sim p(\cdot \mid s,a)} \left[ r+\gamma \max_{a' \in \mathcal{A}} Q_k(s',a') \right].

Then QkQ_k converges to QQ^*. This is the Q-function version of value iteration.

In practice, both assumptions are usually false. The state space may be too large, or even continuous, so we cannot store a table of all Q(s,a)Q(s,a) values. Also, we usually do not know the transition dynamics, so we cannot compute the expectation exactly.

This suggests two approximations. Instead of storing all Q-values, we represent the Q-function with a neural network Qϕ(s,a)Q_\phi(s,a). Instead of computing the expectation exactly, we estimate the Bellman backup from sampled transitions (s,a,r,s)(s,a,r,s').

This gives the simplest version of deep Q-learning. Following Mnih et al., we call this kind of neural-network Q-learning a deep Q-network, or DQN.1 Here, sg[]\operatorname{sg}[\cdot] denotes the stop-gradient operator.

Algorithm: DQN version 0repeatGiven a transition (s,a,r,s)y{r,if s is terminalr+γmaxaAQϕ(s,a),otherwiseϕϕαϕ12(Qϕ(s,a)sg[y])2until convergence\begin{array}{l} \hline \textbf{Algorithm: DQN version 0} \\ \hline \textbf{repeat} \\ \quad \text{Given a transition } (s,a,r,s') \\ \quad y \leftarrow \begin{cases} r, & \text{if } s' \text{ is terminal} \\ r+\gamma \displaystyle\max_{a' \in \mathcal{A}} Q_\phi(s',a'), & \text{otherwise} \end{cases} \\ \quad \phi \leftarrow \phi -\alpha \nabla_\phi \frac{1}{2} \left( Q_\phi(s,a)-\operatorname{sg}[y] \right)^2 \\ \textbf{until convergence} \\ \hline \end{array}

Note that we do not propagate the gradient through the target value yy.

Why stop-gradient?

Why do we stop the gradient through the target value yy? Remember that the goal is to approximate Q-iteration:

Qk+1TQk.Q_{k+1} \approx T^*Q_k.

With a neural network, this means the next network should be close to the Bellman update of the previous network. At iteration kk, we write this as

ϕk+1=argminϕE[(Qϕ(s,a)(TQϕk)(s,a))2].\phi_{k+1} = \arg\min_\phi \mathbb{E} \left[ \left( Q_\phi(s,a) - (T^*Q_{\phi_k})(s,a) \right)^2 \right].

The point is that we want QϕQ_\phi to move closer to TQϕkT^*Q_{\phi_k}. We do not want to move TQϕkT^*Q_{\phi_k} during the same update. If we also differentiate through TQϕkT^*Q_{\phi_k}, then we are no longer just fitting the next Q-function to the Bellman update of the previous one.

So in the DQN update, (TQϕk)(s,a)(T^*Q_{\phi_k})(s,a) is treated as a fixed target. This is what the stop-gradient operator does in the algorithm above.

Residual learning

What if we do not stop the gradient through the target? Then the loss becomes

Lres(ϕ)=12(Qϕ(s,a)(r+γmaxaAQϕ(s,a)))2.\mathcal{L}_{\mathrm{res}}(\phi) = \frac{1}{2} \left( Q_\phi(s,a) - \left( r+\gamma \max_{a' \in \mathcal{A}} Q_\phi(s',a') \right) \right)^2.

This loss minimizes the Bellman residual of the Q-function. This is usually called Bellman residual minimization, and the corresponding gradient method is usually called the residual-gradient method, or Residual Gradient (RG).2

Empirically, residual-gradient methods often perform worse than DQN-style semi-gradient updates in deep RL.34 One reason is gradient cancellation.

Consider one transition from (s,a)(s,a) to (s,a)(s',a^*), where

aargmaxaAQϕ(s,a).a^* \in \arg\max_{a' \in \mathcal{A}} Q_\phi(s',a').

The Bellman residual is

δ=Qϕ(s,a)(r+γQϕ(s,a)).\delta = Q_\phi(s,a) - \left( r+\gamma Q_\phi(s',a^*) \right).

In the DQN update, we reduce this error by moving only Qϕ(s,a)Q_\phi(s,a) toward the target. In residual learning, both sides are updated. Roughly speaking,

ΔQϕ(s,a)δ,ΔQϕ(s,a)γδ.\Delta Q_\phi(s,a) \propto -\delta, \qquad \Delta Q_\phi(s',a^*) \propto \gamma\delta.

So the current value and the next-state value are pushed in opposite directions. But ss and ss' come from the same transition, so their network gradients can be similar. If

ϕQϕ(s,a)ϕQϕ(s,a),\nabla_\phi Q_\phi(s,a) \approx \nabla_\phi Q_\phi(s',a^*),

then the residual-gradient direction contains the difference

ϕQϕ(s,a)γϕQϕ(s,a)(1γ)ϕQϕ(s,a).\nabla_\phi Q_\phi(s,a) - \gamma\nabla_\phi Q_\phi(s',a^*) \approx (1-\gamma)\nabla_\phi Q_\phi(s,a).

When γ\gamma is close to one, the two terms almost cancel. This makes the update small and learning slow.

Sampling states and actions

Let's go back to the DQN update. So far, we have assumed that a transition (s,a,r,s)(s,a,r,s') is given. In practice, how do we get this transition?

In some cases, we can sample state-action pairs directly. For example, a simulator may let us reset to a chosen state and try a chosen action.

In general, this is not possible. For example, a real robot cannot freely jump to any state we want. In the usual RL setup, transitions come from interaction with the environment: the agent is at state ss, chooses action aa, observes (r,s)(r,s'), and then continues from ss'.

Then the algorithm becomes:

Algorithm: DQN with online samplingrepeatChoose action a and observe (r,s)y{r,if s is terminalr+γmaxaAQϕ(s,a),otherwiseϕϕαϕ12(Qϕ(s,a)sg[y])2ssuntil convergence\begin{array}{l} \hline \textbf{Algorithm: DQN with online sampling} \\ \hline \textbf{repeat} \\ \quad \text{Choose action } a \text{ and observe } (r,s') \\ \quad y \leftarrow \begin{cases} r, & \text{if } s' \text{ is terminal} \\ r+\gamma \displaystyle\max_{a' \in \mathcal{A}} Q_\phi(s',a'), & \text{otherwise} \end{cases} \\ \quad \phi \leftarrow \phi -\alpha \nabla_\phi \frac{1}{2} \left( Q_\phi(s,a)-\operatorname{sg}[y] \right)^2 \\ \quad s \leftarrow s' \\ \textbf{until convergence} \\ \hline \end{array}

Replay buffer

However, this version has a problem. As we update QϕQ_\phi along one trajectory, nearby updates use nearby transitions. These transitions are highly correlated, so the network may overfit to a local part of the state-action space.

DQN reduces this problem with a replay buffer.1 A replay buffer is just a dataset of past transitions.

Each time the agent acts, we add (s,a,r,s)(s,a,r,s') to the buffer. For training, we sample one or more minibatches from the buffer instead of using only the newest transition.

This has two advantages. First, minibatch samples are less correlated than consecutive samples from one episode. Second, using a batch gives a lower-variance gradient estimate than using one transition.

In practice, the buffer has fixed capacity: it stores at most NN transitions, and once it is full, adding a new transition removes an old one. Capacity matters because it changes the data used for training. A very small buffer stays too close to recent trajectories, while a very large buffer can keep data that no longer matches the current agent well.

With a replay buffer, the algorithm becomes:

Algorithm: DQN with replay bufferrepeatChoose action a and observe (r,s)DD{(s,a,r,s)}for each sampled minibatch BD doyi{ri,if si is terminalri+γmaxaAQϕ(si,a),otherwise,(si,ai,ri,si)Bϕϕαϕ1BiB12(Qϕ(si,ai)sg[yi])2end foruntil convergence\begin{array}{l} \hline \textbf{Algorithm: DQN with replay buffer} \\ \hline \textbf{repeat} \\ \quad \text{Choose action } a \text{ and observe } (r,s') \\ \quad \mathcal{D} \leftarrow \mathcal{D} \cup \{(s,a,r,s')\} \\ \quad \textbf{for each sampled minibatch } B \subset \mathcal{D} \textbf{ do} \\ \quad\quad y_i \leftarrow \begin{cases} r_i, & \text{if } s_i' \text{ is terminal} \\ r_i+\gamma \displaystyle\max_{a' \in \mathcal{A}} Q_\phi(s_i',a'), & \text{otherwise} \end{cases}, \quad (s_i,a_i,r_i,s_i') \in B \\ \quad\quad \phi \leftarrow \phi -\alpha \nabla_\phi \displaystyle \frac{1}{|B|} \sum_{i \in B} \frac{1}{2} \left( Q_\phi(s_i,a_i)-\operatorname{sg}[y_i] \right)^2 \\ \quad \textbf{end for} \\ \textbf{until convergence} \\ \hline \end{array}

Exploration

How should the agent choose actions while collecting transitions?

One simple idea is to always choose the greedy action:

aargmaxaAQϕ(s,a).a \in \arg\max_{a \in \mathcal{A}} Q_\phi(s,a).

This can be a problem early in training. The Q-function is still inaccurate, so the greedy action may only look good because of a bad estimate. If the agent keeps choosing it, the replay buffer may contain data from only a narrow part of the state-action space.

The opposite extreme is to choose actions uniformly at random:

aUniform(A).a \sim \operatorname{Uniform}(\mathcal{A}).

Choosing actions uniformly at random gives broad exploration, but it is usually inefficient in RL. Useful rewards often require several good actions in a row, and a uniformly random policy is unlikely to follow such a trajectory for long.

A common compromise is ϵ\epsilon-greedy exploration:

a{argmaxaAQϕ(s,a),with probability 1ϵUniform(A),with probability ϵa \sim \begin{cases} \displaystyle \arg\max_{a \in \mathcal{A}} Q_\phi(s,a), & \text{with probability } 1-\epsilon \\ \operatorname{Uniform}(\mathcal{A}), & \text{with probability } \epsilon \end{cases}

The parameter ϵ\epsilon controls the balance between exploration and exploitation. A larger ϵ\epsilon makes the agent explore more. A smaller ϵ\epsilon makes the agent follow the current Q-function more often.

With ϵ\epsilon-greedy exploration, the replay-buffer algorithm becomes:

Algorithm: DQN with explorationrepeata{argmaxaAQϕ(s,a),w.p. 1ϵUniform(A),w.p. ϵTake action a and observe (r,s)DD{(s,a,r,s)}for each sampled minibatch BD doyi{ri,if si is terminalri+γmaxaAQϕ(si,a),otherwise,(si,ai,ri,si)Bϕϕαϕ1BiB12(Qϕ(si,ai)sg[yi])2end foruntil convergence\begin{array}{l} \hline \textbf{Algorithm: DQN with exploration} \\ \hline \textbf{repeat} \\ \quad a \sim \begin{cases} \displaystyle \arg\max_{a \in \mathcal{A}} Q_\phi(s,a), & \text{w.p. } 1-\epsilon \\ \operatorname{Uniform}(\mathcal{A}), & \text{w.p. } \epsilon \end{cases} \\ \quad \text{Take action } a \text{ and observe } (r,s') \\ \quad \mathcal{D} \leftarrow \mathcal{D} \cup \{(s,a,r,s')\} \\ \quad \textbf{for each sampled minibatch } B \subset \mathcal{D} \textbf{ do} \\ \quad\quad y_i \leftarrow \begin{cases} r_i, & \text{if } s_i' \text{ is terminal} \\ r_i+\gamma \displaystyle\max_{a' \in \mathcal{A}} Q_\phi(s_i',a'), & \text{otherwise} \end{cases}, \quad (s_i,a_i,r_i,s_i') \in B \\ \quad\quad \phi \leftarrow \phi -\alpha \nabla_\phi \displaystyle \frac{1}{|B|} \sum_{i \in B} \frac{1}{2} \left( Q_\phi(s_i,a_i)-\operatorname{sg}[y_i] \right)^2 \\ \quad \textbf{end for} \\ \textbf{until convergence} \\ \hline \end{array}

In practice, ϵ\epsilon is often scheduled during training. Early in training, the Q-function is unreliable, so we use a large ϵ\epsilon. Later, as the Q-function becomes more stable, we reduce ϵ\epsilon. In the original DQN experiments, ϵ\epsilon was annealed from 1.01.0 to 0.10.1 during training.1

The choice of exploration strategy can have a large effect on learning. If exploration is too weak, the agent may miss useful actions. If exploration is too strong, the agent may not use what it has already learned. Later DQN variants also found that changing the exploration mechanism can significantly affect performance.5

On-policy vs off-policy

Let us pause and think about this question:

Can we use the replay buffer idea for policy-gradient methods?

For the policy-gradient methods we discussed, the answer is no. The policy-gradient estimator assumes that actions are sampled from the current policy:

θJ(θ)=Eτpθ(τ)[(t=0T1θlogπθ(atst))(t=0T1γtrt)],atπθ(st).\nabla_\theta J(\theta) = \mathbb{E}_{\tau \sim p_\theta(\tau)} \left[ \left( \sum_{t=0}^{T-1} \nabla_\theta \log \pi_\theta(a_t \mid s_t) \right) \left( \sum_{t=0}^{T-1} \gamma^t r_t \right) \right], \qquad a_t \sim \pi_\theta(\cdot \mid s_t).

This is why old transitions cannot be reused directly. If the action came from an older policy, this is no longer the right gradient estimate.

DQN is different. To update the Q-function, we only need a transition (s,a,r,s)(s,a,r,s'). The action aa does not have to come from the current policy.

So we use the following terminology:

On-policy:learn from data generated by the same policy being updatedOff-policy:learn from data generated by a different policy\begin{array}{ll} \text{On-policy:} & \text{learn from data generated by the same policy being updated} \\ \text{Off-policy:} & \text{learn from data generated by a different policy} \end{array}

Off-policy methods can learn from data generated by a different policy. This data may come from past experience, exploratory actions, or expert demonstrations. The policy that generates the data is called the behavior policy.

The main advantage of off-policy methods is sample efficiency: a single experience can be used multiple times. On-policy methods cannot reuse old data directly after the policy changes. This may be acceptable when data collection is cheap, such as in a simulated environment, but it becomes a serious limitation when data collection is expensive, such as with a real-world robot.

Off-policy methods can also learn while following a safe behavior policy. This is useful in practical robotics, where the robot may need to improve its policy without directly executing risky exploratory actions.

Stabilizing DQN

Target networks

Let's go back to the DQN update. In the algorithm above, the target is

yi={ri,if si is terminalri+γmaxaAQϕ(si,a),otherwisey_i = \begin{cases} r_i, & \text{if } s_i' \text{ is terminal} \\ r_i+\gamma \max_{a' \in \mathcal{A}} Q_\phi(s_i',a'), & \text{otherwise} \end{cases}

The same network QϕQ_\phi is used to compute the target and is also being updated. So after every gradient step, ϕ\phi changes, and the target yiy_i changes too.

But in fitted Q-learning, we wanted

ϕk+1=argminϕE[(Qϕ(s,a)(TQϕk)(s,a))2].\phi_{k+1} = \arg\min_\phi \mathbb{E} \left[ \left( Q_\phi(s,a) - (T^*Q_{\phi_k})(s,a) \right)^2 \right].

The replay-buffer algorithm above does not quite follow this. It takes only one gradient step toward the current target. Then ϕ\phi changes, so the next target also changes. In other words, the target moves before we have had much time to fit it.

DQN handles this with a target network. We keep two networks: an online network QϕQ_\phi, which is updated by gradient descent, and a target network QϕQ_{\phi^-}, which is used to compute the target. In practice, we update the target network every CC gradient updates.

Algorithm: DQN with target networkrepeata{argmaxaAQϕ(s,a),w.p. 1ϵUniform(A),w.p. ϵTake action a and observe (r,s)DD{(s,a,r,s)}for each sampled minibatch BD doyi{ri,if si is terminalri+γmaxaAQϕ(si,a),otherwise,(si,ai,ri,si)Bϕϕαϕ1BiB12(Qϕ(si,ai)sg[yi])2Every C gradient updates, set ϕϕend foruntil convergence\begin{array}{l} \hline \textbf{Algorithm: DQN with target network} \\ \hline \textbf{repeat} \\ \quad a \sim \begin{cases} \displaystyle \arg\max_{a \in \mathcal{A}} Q_\phi(s,a), & \text{w.p. } 1-\epsilon \\ \operatorname{Uniform}(\mathcal{A}), & \text{w.p. } \epsilon \end{cases} \\ \quad \text{Take action } a \text{ and observe } (r,s') \\ \quad \mathcal{D} \leftarrow \mathcal{D} \cup \{(s,a,r,s')\} \\ \quad \textbf{for each sampled minibatch } B \subset \mathcal{D} \textbf{ do} \\ \quad\quad y_i \leftarrow \begin{cases} r_i, & \text{if } s_i' \text{ is terminal} \\ r_i+\gamma \displaystyle\max_{a' \in \mathcal{A}} Q_{\phi^-}(s_i',a'), & \text{otherwise} \end{cases}, \quad (s_i,a_i,r_i,s_i') \in B \\ \quad\quad \phi \leftarrow \phi -\alpha \nabla_\phi \displaystyle \frac{1}{|B|} \sum_{i \in B} \frac{1}{2} \left( Q_\phi(s_i,a_i)-\operatorname{sg}[y_i] \right)^2 \\ \quad\quad \text{Every } C \text{ gradient updates, set } \phi^- \leftarrow \phi \\ \quad \textbf{end for} \\ \textbf{until convergence} \\ \hline \end{array}

This algorithm is now close to Algorithm 1 in the original DQN paper. Two remaining differences are that the paper samples one random minibatch per environment step, and its implementation uses clipped TD errors, which is equivalent to using a Huber loss with threshold 11. See the paper for the full implementation details.

Polyak averaging

Original DQN updates the target network by a hard copy:

ϕϕ.\phi^- \leftarrow \phi .

This happens only every CC gradient updates. Between these updates, the target network QϕQ_{\phi^-} becomes older and older. Then it is suddenly replaced by the current online network QϕQ_\phi.

Polyak averaging is a smoother alternative. Instead of copying the whole network at once, we update the target network slowly:

ϕτϕ+(1τ)ϕ,\phi^- \leftarrow \tau \phi^- + (1-\tau)\phi,

where τ\tau is close to 11, such as τ=0.999\tau=0.999.

Double DQN

Consider an MDP where r=0r=0 for every transition.

Q(s,a)=0s,a.Q^*(s,a)=0 \quad \forall s,a.

What happens if we apply DQN to this MDP?

The DQN target is

y=r+γmaxaQϕ(s,a).y = r+\gamma \max_{a'} Q_{\phi^-}(s',a').

Since r=0r=0,

y=γmaxaQϕ(s,a).y = \gamma \max_{a'} Q_{\phi^-}(s',a').

Early in training, QϕQ_{\phi^-} is noisy. Although the correct value is zero,

maxaQϕ(s,a)\max_{a'} Q_{\phi^-}(s',a')

will often be positive. Hence the target is positive, and the update pushes Qϕ(s,a)Q_\phi(s,a) upward.

This is not just an artifact of this example. In practice, DQN can overestimate action values; van Hasselt, Guez, and Silver observed substantial overestimation in some Atari games.6

To see why the max operator creates this bias, rewrite it as

maxaQϕ(s,a)=Qϕ(s,argmaxaQϕ(s,a)).\max_{a'} Q_{\phi^-}(s',a') = Q_{\phi^-} \left( s', \arg\max_{a'} Q_{\phi^-}(s',a') \right).

The argmax\arg\max favors actions with positive estimation error. The outer QϕQ_{\phi^-} then reuses the same positive error as the value. This correlation between action selection and value evaluation causes overestimation.

Double Q-learning avoids this reuse by separating action selection from value evaluation:7

QϕA(s,a)r+γQϕB(s,argmaxaQϕA(s,a)),Q_{\phi_A}(s,a) \leftarrow r+\gamma Q_{\phi_B} \left( s', \arg\max_{a'} Q_{\phi_A}(s',a') \right), QϕB(s,a)r+γQϕA(s,argmaxaQϕB(s,a)).Q_{\phi_B}(s,a) \leftarrow r+\gamma Q_{\phi_A} \left( s', \arg\max_{a'} Q_{\phi_B}(s',a') \right).

Look again at the r=0r=0 MDP. Even if QϕAQ_{\phi_A} selects an action with large positive noise, the value is evaluated by QϕBQ_{\phi_B}. The noise in QϕBQ_{\phi_B} is not tied to the noise in QϕAQ_{\phi_A}, so the same positive noise is not reused.

In DQN, we already have two networks: the online network QϕQ_\phi and the target network QϕQ_{\phi^-}. Double DQN uses them for the same separation.

Standard DQN uses the target network for both selection and evaluation:

yi=ri+γQϕ(si,argmaxaQϕ(si,a)).y_i = r_i+\gamma Q_{\phi^-} \left( s_i', \arg\max_{a'} Q_{\phi^-}(s_i',a') \right).

Double DQN changes only the action selection inside the target:

yi=ri+γQϕ(si,argmaxaQϕ(si,a)).y_i = r_i+\gamma Q_{\phi^-} \left( s_i', \arg\max_{a'} Q_\phi(s_i',a') \right).

This gives the following Double DQN algorithm:

Algorithm: Double DQNrepeata{argmaxaAQϕ(s,a),w.p. 1ϵUniform(A),w.p. ϵTake action a and observe (r,s)DD{(s,a,r,s)}for each sampled minibatch BD doyi{ri,if si is terminalri+γQϕ(si,argmaxaAQϕ(si,a)),otherwise,(si,ai,ri,si)Bϕϕαϕ1BiB12(Qϕ(si,ai)sg[yi])2Every C gradient updates, set ϕϕend foruntil convergence\begin{array}{l} \hline \textbf{Algorithm: Double DQN} \\ \hline \textbf{repeat} \\ \quad a \sim \begin{cases} \displaystyle \arg\max_{a \in \mathcal{A}} Q_\phi(s,a), & \text{w.p. } 1-\epsilon \\ \operatorname{Uniform}(\mathcal{A}), & \text{w.p. } \epsilon \end{cases} \\ \quad \text{Take action } a \text{ and observe } (r,s') \\ \quad \mathcal{D} \leftarrow \mathcal{D} \cup \{(s,a,r,s')\} \\ \quad \textbf{for each sampled minibatch } B \subset \mathcal{D} \textbf{ do} \\ \quad\quad y_i \leftarrow \begin{cases} r_i, & \text{if } s_i' \text{ is terminal} \\ r_i+\gamma Q_{\phi^-} \left( s_i', \displaystyle \arg\max_{a' \in \mathcal{A}} Q_\phi(s_i',a') \right), & \text{otherwise} \end{cases}, \quad (s_i,a_i,r_i,s_i') \in B \\ \quad\quad \phi \leftarrow \phi -\alpha \nabla_\phi \displaystyle \frac{1}{|B|} \sum_{i \in B} \frac{1}{2} \left( Q_\phi(s_i,a_i)-\operatorname{sg}[y_i] \right)^2 \\ \quad\quad \text{Every } C \text{ gradient updates, set } \phi^- \leftarrow \phi \\ \quad \textbf{end for} \\ \textbf{until convergence} \\ \hline \end{array}

Q-learning with continuous actions

So far, we have assumed a discrete action space. What goes wrong if the action space is continuous?

The issue is that DQN repeatedly asks us to maximize the Q-function over actions.

When collecting data, this appears in the greedy action:

aargmaxaAQϕ(s,a).a \in \arg\max_{a \in \mathcal{A}} Q_\phi(s,a).

When training the Q-function, it appears again in the target value:

yi=ri+γmaxaAQϕ(si,a).y_i = r_i+\gamma \max_{a' \in \mathcal{A}} Q_{\phi^-}(s_i',a').

The simplest workaround is to sample candidate actions and choose the one with the largest Q-value:

maxaQϕ(s,a)max{Qϕ(s,a1),,Qϕ(s,aN)}(a1,,aN) sampled from some distribution (e.g., uniform)\begin{aligned} \max_a Q_\phi(s,a) &\approx \max \left\{ Q_\phi(s,a_1),\ldots,Q_\phi(s,a_N) \right\} \\ (a_1,\ldots,a_N) &\text{ sampled from some distribution (e.g., uniform)} \end{aligned}

A more accurate version is to use an iterative stochastic optimizer. For example, QT-Opt uses the cross-entropy method (CEM) to approximately maximize the Q-function over continuous actions.8

Algorithm: CEM action maximizationInitialize a Gaussian distribution over actionsrepeat K timesSample N candidate actions a1,,aNEvaluate Qϕ(s,aj) for all candidatesKeep the top M elite actionsRefit the Gaussian distribution to the elite actionsreturn the best action found\begin{array}{l} \hline \textbf{Algorithm: CEM action maximization} \\ \hline \text{Initialize a Gaussian distribution over actions} \\ \textbf{repeat } K \textbf{ times} \\ \quad \text{Sample } N \text{ candidate actions } a_1,\ldots,a_N \\ \quad \text{Evaluate } Q_\phi(s,a_j) \text{ for all candidates} \\ \quad \text{Keep the top } M \text{ elite actions} \\ \quad \text{Refit the Gaussian distribution to the elite actions} \\ \textbf{return } \text{the best action found} \\ \hline \end{array}

This can work when the action dimension is low enough. But as the dimension grows, sampled actions are unlikely to land near the maximizer, so the resulting target can be a poor approximation of the true max.

Normalized Advantage Functions (NAF)

Another way to avoid numerical maximization is to choose a Q-function class that is easy to maximize.

Normalized Advantage Functions (NAF) do this by changing the structure of the Q-network.9 Given a state ss, the network outputs a value Vϕ(s)V_\phi(s), an action mean μϕ(s)\mu_\phi(s), and a lower-triangular matrix Lϕ(s)L_\phi(s). These outputs define the Q-value by

Qϕ(s,a)=Vϕ(s)12(aμϕ(s))Lϕ(s)Lϕ(s)(aμϕ(s)).Q_\phi(s,a) = V_\phi(s) -\frac{1}{2} \left( a-\mu_\phi(s) \right)^\top L_\phi(s)L_\phi(s)^\top \left( a-\mu_\phi(s) \right).

The diagonal entries of Lϕ(s)L_\phi(s) are constrained to be positive, so Lϕ(s)Lϕ(s)L_\phi(s)L_\phi(s)^\top is positive definite. Therefore, the quadratic term is always non-positive and is maximized at a=μϕ(s)a=\mu_\phi(s):

argmaxaQϕ(s,a)=μϕ(s),maxaQϕ(s,a)=Vϕ(s).\arg\max_a Q_\phi(s,a) = \mu_\phi(s), \qquad \max_a Q_\phi(s,a) = V_\phi(s).

So the Q-learning target no longer needs an inner optimization problem. For the next state, the target network can directly use Vϕ(s)V_{\phi^-}(s') as the maximized Q-value.

However, this works because NAF restricts the advantage to be quadratic in the action. The resulting Q-function is easy to maximize, but it cannot represent more complicated action-value landscapes, such as cases where several distinct actions are good for the same state.

Deep Deterministic Policy Gradient (DDPG)

Another approach is to learn an approximate maximizer. The maximized Q-value can be written as

maxaQϕ(s,a)=Qϕ(s,argmaxaQϕ(s,a)).\max_a Q_\phi(s,a) = Q_\phi \left( s, \arg\max_a Q_\phi(s,a) \right).

DDPG introduces another network πθ(s)\pi_\theta(s), called the actor, and trains it to approximate the action that maximizes the critic QϕQ_\phi:10

πθ(s)argmaxaQϕ(s,a).\pi_\theta(s) \approx \arg\max_a Q_\phi(s,a).

For a single state, the idealized update would be

θargmaxθQϕ(s,πθ(s)).\theta \leftarrow \arg\max_\theta Q_\phi \left( s, \pi_\theta(s) \right).

In practice, we optimize this over states sampled from the replay buffer:

maxθEsD[Qϕ(s,πθ(s))].\max_\theta \mathbb{E}_{s \sim \mathcal{D}} \left[ Q_\phi \left( s, \pi_\theta(s) \right) \right].

In DQN, exploration was done with ϵ\epsilon-greedy actions over a finite action set. For a deterministic actor in a continuous action space, a natural exploration strategy is to perturb the actor's action:

a=πθ(s)+ϵ,a = \pi_\theta(s)+\epsilon,

where ϵ\epsilon is some exploration noise.

DDPG also updates the target networks by Polyak averaging, instead of periodically copying the online networks.

This gives the following DDPG algorithm:

Algorithm: DDPGrepeataπθ(s)+ϵ,ϵ is some exploration noiseTake action a and observe (r,s)DD{(s,a,r,s)}for each sampled minibatch BD doyi{ri,if si is terminalri+γQϕ(si,πθ(si)),otherwise,(si,ai,ri,si)BϕϕαQϕ1BiB12(Qϕ(si,ai)sg[yi])2θθ+απθ1BiBQϕ(si,πθ(si))ϕτϕ+(1τ)ϕθτθ+(1τ)θend foruntil convergence\begin{array}{l} \hline \textbf{Algorithm: DDPG} \\ \hline \textbf{repeat} \\ \quad a \leftarrow \pi_\theta(s)+\epsilon, \quad \epsilon \text{ is some exploration noise} \\ \quad \text{Take action } a \text{ and observe } (r,s') \\ \quad \mathcal{D} \leftarrow \mathcal{D} \cup \{(s,a,r,s')\} \\ \quad \textbf{for each sampled minibatch } B \subset \mathcal{D} \textbf{ do} \\ \quad\quad y_i \leftarrow \begin{cases} r_i, & \text{if } s_i' \text{ is terminal} \\ r_i+\gamma Q_{\phi^-} \left( s_i', \pi_{\theta^-}(s_i') \right), & \text{otherwise} \end{cases}, \quad (s_i,a_i,r_i,s_i') \in B \\ \quad\quad \phi \leftarrow \phi -\alpha_Q \nabla_\phi \displaystyle \frac{1}{|B|} \sum_{i \in B} \frac{1}{2} \left( Q_\phi(s_i,a_i)-\operatorname{sg}[y_i] \right)^2 \\ \quad\quad \theta \leftarrow \theta + \alpha_\pi \nabla_\theta \displaystyle \frac{1}{|B|} \sum_{i \in B} Q_\phi \left( s_i, \pi_\theta(s_i) \right) \\ \quad\quad \phi^- \leftarrow \tau \phi^- + (1-\tau)\phi \\ \quad\quad \theta^- \leftarrow \tau \theta^- + (1-\tau)\theta \\ \quad \textbf{end for} \\ \textbf{until convergence} \\ \hline \end{array}

DDPG as deterministic policy gradient method

The previous section described DDPG from the DQN view:

πθ(s)argmaxaQϕ(s,a).\pi_\theta(s) \approx \arg\max_a Q_\phi(s,a).

However, the original DDPG paper presents the algorithm from the deterministic policy gradient (DPG) view.11

To see what DPG means, recall the policy-gradient problem from Policy Gradient Methods:

Find a stochastic policy πθ(atst) that maximizes J(θ)=Eτpθ(τ)[t=0T1γtrt].\text{Find a stochastic policy } \pi_\theta(a_t\mid s_t) \text{ that maximizes } J(\theta) = \mathbb{E}_{\tau\sim p_\theta(\tau)} \left[ \sum_{t=0}^{T-1}\gamma^t r_t \right].

DPG changes the stochastic policy into a deterministic policy:

at=πθ(st).a_t=\pi_\theta(s_t).

The objective is still

J(θ)=Es0p0[Vθ(s0)].J(\theta) = \mathbb{E}_{s_0\sim p_0} \left[ V^\theta(s_0) \right].

Theorem. The deterministic policy gradient is

θJ(θ)=Es0p0,πθ[t=0T1γtθπθ(st)aQθ(st,a)a=πθ(st)].\nabla_\theta J(\theta) = \mathbb{E}_{s_0\sim p_0,\pi_\theta} \left[ \sum_{t=0}^{T-1} \gamma^t \nabla_\theta \pi_\theta(s_t) \left. \nabla_a Q^\theta(s_t,a) \right|_{a=\pi_\theta(s_t)} \right].

Equivalently, using the discounted state distribution,

θJ(θ)=Esρθ[θπθ(s)aQθ(s,a)a=πθ(s)],ρθ(s)=Es0p0,πθ[t=0T1γt1{st=s}].\begin{aligned} \nabla_\theta J(\theta) &= \mathbb{E}_{s\sim \rho^\theta} \left[ \nabla_\theta \pi_\theta(s) \left. \nabla_a Q^\theta(s,a) \right|_{a=\pi_\theta(s)} \right], \\ \rho^\theta(s) &= \mathbb{E}_{s_0\sim p_0,\pi_\theta} \left[ \sum_{t=0}^{T-1} \gamma^t \mathbf{1}\{s_t=s\} \right]. \end{aligned}
Proof

First,

Vθ(s)=Qθ(s,πθ(s)).V^\theta(s)=Q^\theta(s,\pi_\theta(s)).

Also,

Qθ(s,a)=Er,sp(,s,a)[r+γVθ(s)].Q^\theta(s,a) = \mathbb{E}_{r,s'\sim p(\cdot,\cdot|s,a)} \left[ r+\gamma V^\theta(s') \right].

Differentiate Vθ(s)V^\theta(s):

θVθ(s)=θQθ(s,πθ(s))=φQθ(s,πφ(s))φ=θ+ηQη(s,πθ(s))η=θ.\begin{aligned} \nabla_\theta V^\theta(s) &= \nabla_\theta Q^\theta(s,\pi_\theta(s)) \\ &= \left. \nabla_\varphi Q^\theta(s,\pi_\varphi(s)) \right|_{\varphi=\theta} + \left. \nabla_\eta Q^\eta(s,\pi_\theta(s)) \right|_{\eta=\theta}. \end{aligned}

The first term changes only the current action:

φQθ(s,πφ(s))φ=θ=φπφ(s)aQθ(s,a)φ=θa=πθ(s).\left. \nabla_\varphi Q^\theta(s,\pi_\varphi(s)) \right|_{\varphi=\theta} = \left. \nabla_\varphi \pi_\varphi(s) \nabla_a Q^\theta(s,a) \right|_{\substack{\varphi=\theta\\a=\pi_\theta(s)}}.

So

φQθ(s,πφ(s))φ=θ=θπθ(s)aQθ(s,a)a=πθ(s).\left. \nabla_\varphi Q^\theta(s,\pi_\varphi(s)) \right|_{\varphi=\theta} = \nabla_\theta \pi_\theta(s) \left. \nabla_a Q^\theta(s,a) \right|_{a=\pi_\theta(s)}.

The second term changes the future policy inside QθQ^\theta:

ηQη(s,πθ(s))η=θ=ηEr,sp(,s,πθ(s))[r+γVη(s)]η=θ=γEr,sp(,s,πθ(s))[θVθ(s)].\begin{aligned} \left. \nabla_\eta Q^\eta(s,\pi_\theta(s)) \right|_{\eta=\theta} &= \left. \nabla_\eta \mathbb{E}_{r,s'\sim p(\cdot,\cdot|s,\pi_\theta(s))} \left[ r+\gamma V^\eta(s') \right] \right|_{\eta=\theta} \\ &= \gamma \mathbb{E}_{r,s'\sim p(\cdot,\cdot|s,\pi_\theta(s))} \left[ \nabla_\theta V^\theta(s') \right]. \end{aligned}

Therefore,

θVθ(s)=θπθ(s)aQθ(s,a)a=πθ(s)+γEsp(s,πθ(s))[θVθ(s)]\boxed{ \nabla_\theta V^\theta(s) = \nabla_\theta \pi_\theta(s) \left. \nabla_a Q^\theta(s,a) \right|_{a=\pi_\theta(s)} + \gamma \mathbb{E}_{s'\sim p(\cdot|s,\pi_\theta(s))} \left[ \nabla_\theta V^\theta(s') \right] }

Starting from s0s_0,

θVθ(s0)=θπθ(s0)aQθ(s0,a)a=πθ(s0)+γE[θVθ(s1)s0].\begin{aligned} \nabla_\theta V^\theta(s_0) =& \nabla_\theta \pi_\theta(s_0) \left. \nabla_a Q^\theta(s_0,a) \right|_{a=\pi_\theta(s_0)} \\ &+ \gamma \mathbb{E} \left[ \nabla_\theta V^\theta(s_1) \mid s_0 \right]. \end{aligned}

Apply the same identity to s1s_1:

θVθ(s1)=θπθ(s1)aQθ(s1,a)a=πθ(s1)+γE[θVθ(s2)s1].\begin{aligned} \nabla_\theta V^\theta(s_1) =& \nabla_\theta \pi_\theta(s_1) \left. \nabla_a Q^\theta(s_1,a) \right|_{a=\pi_\theta(s_1)} \\ &+ \gamma \mathbb{E} \left[ \nabla_\theta V^\theta(s_2) \mid s_1 \right]. \end{aligned}

Substituting this into the previous equation gives

θVθ(s0)=θπθ(s0)aQθ(s0,a)a=πθ(s0)+γE[θπθ(s1)aQθ(s1,a)a=πθ(s1)s0]+γ2E[θVθ(s2)s0].\begin{aligned} \nabla_\theta V^\theta(s_0) =& \nabla_\theta \pi_\theta(s_0) \left. \nabla_a Q^\theta(s_0,a) \right|_{a=\pi_\theta(s_0)} \\ &+ \gamma \mathbb{E} \left[ \nabla_\theta \pi_\theta(s_1) \left. \nabla_a Q^\theta(s_1,a) \right|_{a=\pi_\theta(s_1)} \mid s_0 \right] \\ &+ \gamma^2 \mathbb{E} \left[ \nabla_\theta V^\theta(s_2) \mid s_0 \right]. \end{aligned}

Repeating once more,

θVθ(s0)=θπθ(s0)aQθ(s0,a)a=πθ(s0)+γE[θπθ(s1)aQθ(s1,a)a=πθ(s1)s0]+γ2E[θπθ(s2)aQθ(s2,a)a=πθ(s2)s0]+γ3E[θVθ(s3)s0].\begin{aligned} \nabla_\theta V^\theta(s_0) =& \nabla_\theta \pi_\theta(s_0) \left. \nabla_a Q^\theta(s_0,a) \right|_{a=\pi_\theta(s_0)} \\ &+ \gamma \mathbb{E} \left[ \nabla_\theta \pi_\theta(s_1) \left. \nabla_a Q^\theta(s_1,a) \right|_{a=\pi_\theta(s_1)} \mid s_0 \right] \\ &+ \gamma^2 \mathbb{E} \left[ \nabla_\theta \pi_\theta(s_2) \left. \nabla_a Q^\theta(s_2,a) \right|_{a=\pi_\theta(s_2)} \mid s_0 \right] \\ &+ \gamma^3 \mathbb{E} \left[ \nabla_\theta V^\theta(s_3) \mid s_0 \right]. \end{aligned}

After TT steps,

θVθ(s0)=E[t=0T1γtθπθ(st)aQθ(st,a)a=πθ(st)+γTθVθ(sT)    s0]=E[t=0T1γtθπθ(st)aQθ(st,a)a=πθ(st) | s0].\begin{aligned} \nabla_\theta V^\theta(s_0) &= \mathbb{E} \Bigg[ \sum_{t=0}^{T-1} \gamma^t \nabla_\theta \pi_\theta(s_t) \left. \nabla_a Q^\theta(s_t,a) \right|_{a=\pi_\theta(s_t)} + \gamma^T \nabla_\theta V^\theta(s_T) \;\Bigg|\;s_0 \Bigg] \\ &= \mathbb{E} \left[ \sum_{t=0}^{T-1} \gamma^t \nabla_\theta \pi_\theta(s_t) \left. \nabla_a Q^\theta(s_t,a) \right|_{a=\pi_\theta(s_t)} \ \middle|\ s_0 \right]. \end{aligned}

Finally,

J(θ)=Es0p0[Vθ(s0)],J(\theta) = \mathbb{E}_{s_0\sim p_0} \left[ V^\theta(s_0) \right],

so

θJ(θ)=Es0p0,πθ[t=0T1γtθπθ(st)aQθ(st,a)a=πθ(st)]\boxed{ \nabla_\theta J(\theta) = \mathbb{E}_{s_0\sim p_0,\pi_\theta} \left[ \sum_{t=0}^{T-1} \gamma^t \nabla_\theta \pi_\theta(s_t) \left. \nabla_a Q^\theta(s_t,a) \right|_{a=\pi_\theta(s_t)} \right] }

Using the discounted state distribution,

θJ(θ)=Esρθ[θπθ(s)aQθ(s,a)a=πθ(s)].\boxed{ \nabla_\theta J(\theta) = \mathbb{E}_{s\sim \rho^\theta} \left[ \nabla_\theta \pi_\theta(s) \left. \nabla_a Q^\theta(s,a) \right|_{a=\pi_\theta(s)} \right]. }

This proves the theorem.

DDPG uses two approximations:

Qθ(s,a)Qϕ(s,a),ρθD.Q^\theta(s,a)\approx Q_\phi(s,a), \qquad \rho^\theta\approx \mathcal{D}.

Therefore,

θJ(θ)EsD[θπθ(s)aQϕ(s,a)a=πθ(s)].\nabla_\theta J(\theta) \approx \mathbb{E}_{s\sim \mathcal{D}} \left[ \nabla_\theta \pi_\theta(s) \left. \nabla_a Q_\phi(s,a) \right|_{a=\pi_\theta(s)} \right].

Equivalently, the actor performs gradient ascent on

EsD[Qϕ(s,πθ(s))].\mathbb{E}_{s\sim \mathcal{D}} \left[ Q_\phi(s,\pi_\theta(s)) \right].

In minibatch form,

θθ+απθ1BiBQϕ(si,πθ(si)).\theta \leftarrow \theta + \alpha_\pi \nabla_\theta \frac{1}{|B|} \sum_{i \in B} Q_\phi(s_i,\pi_\theta(s_i)).

This is the actor update used in the DDPG algorithm above.

Twin Delayed Deep Deterministic Policy Gradient (TD3)

Clipped double Q-learning

DDPG uses the target

y=r+γQϕ(s,μθ(s)).y = r + \gamma Q_{\phi^-} \left( s', \mu_{\theta^-}(s') \right).

This has the same kind of overestimation problem we saw in DQN. TD3 applies the basic idea of Double Q-learning to reduce this overestimation.12

Unlike Double DQN, which reused the online and target networks as the two value estimates, TD3 follows the original Double Q-learning idea and learns two critics:

Qϕ1(s,a),Qϕ2(s,a).Q_{\phi_1}(s,a), \qquad Q_{\phi_2}(s,a).

A direct actor-critic version of Double Q-learning can be written as

y1=r+γQϕ2(s,μθ1(s)),Qϕ1y1,y2=r+γQϕ1(s,μθ2(s)),Qϕ2y2.\begin{aligned} y_1 &= r + \gamma Q_{\phi_2^-} \left( s', \mu_{\theta_1}(s') \right), & Q_{\phi_1} &\leftarrow y_1, \\ y_2 &= r + \gamma Q_{\phi_1^-} \left( s', \mu_{\theta_2}(s') \right), & Q_{\phi_2} &\leftarrow y_2. \end{aligned}

TD3 makes this idea more conservative. Instead of letting each critic use the other critic's value, TD3 uses the smaller of the two target critic values:

y=r+γminj=1,2Qϕj(s,μθ(s)).y = r + \gamma \min_{j=1,2} Q_{\phi_j^-} \left( s', \mu_{\theta^-}(s') \right).

This is called clipped double Q-learning. Taking the minimum can make the target underestimate the true value, but this is usually safer than overestimation. Since the actor update maximizes Qϕ(s,μθ(s))Q_\phi(s,\mu_\theta(s)), overestimated actions are more likely to be selected. They can then appear again in future rollouts and TD targets, so the error can reinforce itself. Underestimated actions are less likely to be selected, so their errors are less likely to propagate.

Target policy smoothing

TD3 also adds a small amount of clipped noise to the target action. This target policy smoothing prevents the critic target from overfitting to narrow peaks in the value estimate, and encourages nearby actions to have similar values:

a~=μθ(s)+ϵ,ϵclip(N(0,σ),c,c).\tilde a' = \mu_{\theta^-}(s')+\epsilon, \qquad \epsilon \sim \operatorname{clip} \left( \mathcal{N}(0,\sigma), -c, c \right).

So the actual target is

y=r+γminj=1,2Qϕj(s,a~).y = r + \gamma \min_{j=1,2} Q_{\phi_j^-} \left( s', \tilde a' \right).

Delayed policy updates

TD3 updates the critic more frequently while updating the actor more cautiously. The critics are updated every gradient step, while the actor and target networks are updated only after every dd critic updates. A common choice is d=2d=2.

TD3 algorithm

Combining clipped double Q-learning, target policy smoothing, and delayed policy updates gives the complete TD3 algorithm:

Algorithm: TD3Initialize Qϕ1,Qϕ2,μθ and target networks Qϕ1,Qϕ2,μθInitialize replay buffer Drepeataμθ(s)+η,η is some exploration noiseTake action a and observe (r,s)DD{(s,a,r,s)}for each sampled minibatch BD doϵiclip(N(0,σ),c,c)a~iμθ(si)+ϵiyi{ri,if si is terminalri+γminj=1,2Qϕj(si,a~i),otherwise,(si,ai,ri,si)Bfor j{1,2} doϕjϕjαQϕj1BiB12(Qϕj(si,ai)sg[yi])2end forafter every d critic updates doθθ+αμθ1BiBQϕ1(si,μθ(si))ϕ1τϕ1+(1τ)ϕ1ϕ2τϕ2+(1τ)ϕ2θτθ+(1τ)θend afterend foruntil convergence\begin{array}{l} \hline \textbf{Algorithm: TD3} \\ \hline \text{Initialize } Q_{\phi_1}, Q_{\phi_2}, \mu_\theta \text{ and target networks } Q_{\phi_1^-}, Q_{\phi_2^-}, \mu_{\theta^-} \\ \text{Initialize replay buffer } \mathcal{D} \\ \textbf{repeat} \\ \quad a \leftarrow \mu_\theta(s)+\eta, \quad \eta \text{ is some exploration noise} \\ \quad \text{Take action } a \text{ and observe } (r,s') \\ \quad \mathcal{D} \leftarrow \mathcal{D} \cup \{(s,a,r,s')\} \\ \quad \textbf{for each sampled minibatch } B \subset \mathcal{D} \textbf{ do} \\ \quad\quad \epsilon_i' \sim \operatorname{clip} \left( \mathcal{N}(0,\sigma), -c, c \right) \\ \quad\quad \tilde a_i' \leftarrow \mu_{\theta^-}(s_i')+\epsilon_i' \\ \quad\quad y_i \leftarrow \begin{cases} r_i, & \text{if } s_i' \text{ is terminal} \\ r_i+\gamma \displaystyle \min_{j=1,2} Q_{\phi_j^-} \left( s_i', \tilde a_i' \right), & \text{otherwise} \end{cases}, \quad (s_i,a_i,r_i,s_i') \in B \\ \quad\quad \textbf{for } j \in \{1,2\} \textbf{ do} \\ \quad\quad\quad \phi_j \leftarrow \phi_j -\alpha_Q \nabla_{\phi_j} \displaystyle \frac{1}{|B|} \sum_{i \in B} \frac{1}{2} \left( Q_{\phi_j}(s_i,a_i)-\operatorname{sg}[y_i] \right)^2 \\ \quad\quad \textbf{end for} \\ \quad\quad \textbf{after every } d \textbf{ critic updates do} \\ \quad\quad\quad \theta \leftarrow \theta + \alpha_\mu \nabla_\theta \displaystyle \frac{1}{|B|} \sum_{i \in B} Q_{\phi_1} \left( s_i, \mu_\theta(s_i) \right) \\ \quad\quad\quad \phi_1^- \leftarrow \tau \phi_1^- + (1-\tau)\phi_1 \\ \quad\quad\quad \phi_2^- \leftarrow \tau \phi_2^- + (1-\tau)\phi_2 \\ \quad\quad\quad \theta^- \leftarrow \tau \theta^- + (1-\tau)\theta \\ \quad\quad \textbf{end after} \\ \quad \textbf{end for} \\ \textbf{until convergence} \\ \hline \end{array}

Maximum Entropy RL

So far, the goal has been to find a policy that maximizes expected return. Maximum-entropy RL changes the objective slightly. Instead of maximizing only reward, it maximizes reward and policy entropy:

JH(π)=Es0p0π[t=0T1γt(rt+βH(π(st)))]J_{\mathcal{H}}(\pi) = \mathbb{E}^{\pi}_{s_0 \sim p_0} \left[ \sum_{t=0}^{T-1} \gamma^t \left( r_t + \beta \mathcal{H} \left( \pi(\cdot \mid s_t) \right) \right) \right]

where H\mathcal{H} denotes the entropy of the policy at state ss:

H(π(s))=Eaπ(s)[logπ(as)].\mathcal{H} \left( \pi(\cdot \mid s) \right) = \mathbb{E}_{a \sim \pi(\cdot \mid s)} \left[ -\log \pi(a \mid s) \right].

Here β>0\beta > 0 is the temperature. When β\beta is small, the entropy term matters less and the objective becomes close to the usual RL objective. When β\beta is large, the policy is pushed to stay more random.

Why optimize this objective instead of the usual return? The ordinary objective gives a greedy policy:

π(s)argmaxaAQ(s,a).\pi^*(s) \in \arg\max_{a \in \mathcal{A}} Q^*(s,a).

So the policy tries to learn one best action for each state. This is sometimes exactly what we want. But in many tasks, a single state can have several good actions. For example, when picking up a cup, we might grab the handle, support the cup from the bottom, or hold it near the rim depending on the situation.

The maximum-entropy objective captures this idea. It still prefers actions with high return, but among actions with similar return, it prefers a policy that keeps multiple options available. So if there are many ways to solve the task, the policy is encouraged to represent many of them instead of collapsing immediately to one arbitrary choice.

Here is an example that demonstrates the difference between the ordinary objective and the maximum-entropy objective.

This video compares the standard RL objective with the maximum-entropy RL objective. DDPG represents the standard objective, while Soft Q-learning (SQL) represents the maximum-entropy objective. In this experiment, a quadruped is rewarded for moving quickly without specifying a particular direction.13 DDPG tends to pick one consistent direction, whereas SQL can keep several directions as plausible behavior because its objective rewards high-return behavior without forcing the policy to collapse to one mode.

Discrete entropy and differential entropy

The entropy you might be more familiar with is the entropy of a discrete random variable. If XX takes values in a finite set X\mathcal{X}, then

H(X)=xXp(x)logp(x)=Exp[logp(x)].\mathcal{H}(X) = - \sum_{x \in \mathcal{X}} p(x)\log p(x) = \mathbb{E}_{x \sim p} \left[ -\log p(x) \right].

For a finite discrete random variable,

0H(X)logX.0 \le \mathcal{H}(X) \le \log |\mathcal{X}|.

The minimum value 00 is achieved if and only if XX is deterministic. The maximum value logX\log |\mathcal{X}| is achieved by the uniform distribution.

For a continuous random variable, the analogous object is called differential entropy. If XX has density p(x)p(x), then

h(X)=p(x)logp(x)dx=Exp[logp(x)].h(X) = - \int p(x)\log p(x)dx = \mathbb{E}_{x \sim p} \left[ -\log p(x) \right].

This looks almost identical, but it behaves differently. Differential entropy has no universal lower or upper bound. It can be negative, and it can be made arbitrarily large by spreading the density over a wider region. So we should not treat discrete entropy and differential entropy as exactly the same object.

Soft value functions

From the augmented objective, we can define soft value functions. For a fixed policy π\pi,

VHπ(s)=Eπ[t=0T1γt(rt+βH(π(st)))s0=s],QHπ(s,a)=Eπ[r0+t=1T1γt(rt+βH(π(st)))s0=s, a0=a].\begin{aligned} V^\pi_{\mathcal{H}}(s) &= \mathbb{E}^{\pi} \left[ \sum_{t=0}^{T-1} \gamma^t \left( r_t + \beta \mathcal{H} \left( \pi(\cdot \mid s_t) \right) \right) \mid s_0=s \right], \\ Q^\pi_{\mathcal{H}}(s,a) &= \mathbb{E}^{\pi} \left[ r_0 + \sum_{t=1}^{T-1} \gamma^t \left( r_t + \beta \mathcal{H} \left( \pi(\cdot \mid s_t) \right) \right) \mid s_0=s,\ a_0=a \right]. \end{aligned}

Note that QHπ(s,a)Q^\pi_{\mathcal{H}}(s,a) does not include the entropy term for the current state ss, because the current action is already determined.

As before, the two value functions are related:

QHπ(s,a)=E(r,s)p(s,a)[r+γVHπ(s)],VHπ(s)=Eaπ(s)[QHπ(s,a)βlogπ(as)].\begin{aligned} Q^\pi_{\mathcal{H}}(s,a) &= \mathbb{E}_{(r,s') \sim p(\cdot \mid s,a)} \left[ r+\gamma V^\pi_{\mathcal{H}}(s') \right], \\[0.5em] V^\pi_{\mathcal{H}}(s) &= \mathbb{E}_{a \sim \pi(\cdot \mid s)} \left[ Q^\pi_{\mathcal{H}}(s,a) - \beta \log \pi(a \mid s) \right]. \end{aligned}
Derivation

For the first relation, separate the first transition in the definition of QHπQ^\pi_{\mathcal{H}}:

QHπ(s,a)=Eπ[r0+t=1T1γt(rt+βH(π(st)))s0=s, a0=a]=E(r,s)p(s,a)[r+γVHπ(s)].\begin{aligned} Q^\pi_{\mathcal{H}}(s,a) &= \mathbb{E}^\pi \left[ r_0 + \sum_{t=1}^{T-1} \gamma^t \left( r_t + \beta \mathcal{H} \left( \pi(\cdot \mid s_t) \right) \right) \mid s_0=s,\ a_0=a \right] \\ &= \mathbb{E}_{(r,s') \sim p(\cdot \mid s,a)} \left[ r+\gamma V^\pi_{\mathcal{H}}(s') \right]. \end{aligned}

For the second relation, start from the definition of VHπV^\pi_{\mathcal{H}} and separate the first action:

VHπ(s)=Eaπ(s)[E[r0+t=1T1γt(rt+βH(π(st)))s0=s, a0=a]]+βH(π(s))=Eaπ(s)[QHπ(s,a)]+βEaπ(s)[logπ(as)]=Eaπ(s)[QHπ(s,a)βlogπ(as)].\begin{aligned} V^\pi_{\mathcal{H}}(s) &= \mathbb{E}_{a \sim \pi(\cdot \mid s)} \left[ \mathbb{E} \left[ r_0 + \sum_{t=1}^{T-1} \gamma^t \left( r_t + \beta \mathcal{H} \left( \pi(\cdot \mid s_t) \right) \right) \mid s_0=s,\ a_0=a \right] \right] + \beta \mathcal{H} \left( \pi(\cdot \mid s) \right) \\ &= \mathbb{E}_{a \sim \pi(\cdot \mid s)} \left[ Q^\pi_{\mathcal{H}}(s,a) \right] + \beta \mathbb{E}_{a \sim \pi(\cdot \mid s)} \left[ -\log \pi(a \mid s) \right] \\ &= \mathbb{E}_{a \sim \pi(\cdot \mid s)} \left[ Q^\pi_{\mathcal{H}}(s,a) - \beta \log \pi(a \mid s) \right]. \end{aligned}

The first term is the expected return after fixing the first action. The entropy term for the current state can then be written as an expectation over the sampled first action.

Handling terminal states

Under the original MDP definition, a trajectory ends when it reaches sT=sterms_T=s_{\mathrm{term}}. Since the agent does not choose another action at the terminal state, π(sterm)\pi(\cdot\mid s_{\mathrm{term}}), and therefore its entropy, is not defined.

To avoid writing a separate terminal case in every equation, we introduce a single dummy action aterma_{\mathrm{term}} at the terminal state. This action keeps the process at sterms_{\mathrm{term}} and produces zero reward:

p(r=0,s=stermsterm,aterm)=1.p(r=0,s'=s_{\mathrm{term}} \mid s_{\mathrm{term}},a_{\mathrm{term}}) = 1.

Since it is the only available action,

π(atermsterm)=1,H(π(sterm))=0.\pi(a_{\mathrm{term}}\mid s_{\mathrm{term}})=1, \qquad \mathcal{H}\left(\pi(\cdot\mid s_{\mathrm{term}})\right)=0.

There is no reward or entropy bonus after reaching the terminal state. Therefore,

VHπ(sterm)=0,QHπ(sterm,aterm)=0.V^\pi_{\mathcal H}(s_{\mathrm{term}})=0, \qquad Q^\pi_{\mathcal H}(s_{\mathrm{term}},a_{\mathrm{term}})=0.

To keep the notation simple, we will omit separate terminal-state cases in the equations below. If a fully explicit terminal-state equation is needed, it can be recovered using the dummy-action convention above.

Soft Bellman equation

We can derive a similar Bellman equation for the soft Q-function.

Theorem. Let π\pi be a fixed policy. Assume γ(0,1)\gamma \in (0,1), S<|\mathcal{S}|<\infty, A<|\mathcal{A}|<\infty, and rR<|r|\le R<\infty almost surely. Then QHπQ^\pi_{\mathcal{H}} exists and satisfies the soft Bellman equation

QHπ(s,a)=E(r,s)p(s,a), aπ(s)[r+γ(QHπ(s,a)βlogπ(as))].Q^\pi_{\mathcal{H}}(s,a) = \mathbb{E}_{(r,s') \sim p(\cdot \mid s,a),\ a' \sim \pi(\cdot \mid s')} \left[ r + \gamma \left( Q^\pi_{\mathcal{H}}(s',a') - \beta \log \pi(a' \mid s') \right) \right].

Define the soft Bellman operator THπT^\pi_{\mathcal{H}} by

(THπQ)(s,a)=E(r,s)p(s,a), aπ(s)[r+γ(Q(s,a)βlogπ(as))].\left( T^\pi_{\mathcal{H}}Q \right)(s,a) = \mathbb{E}_{(r,s') \sim p(\cdot \mid s,a),\ a' \sim \pi(\cdot \mid s')} \left[ r + \gamma \left( Q(s',a') - \beta \log \pi(a' \mid s') \right) \right].

The soft Bellman equation can be written as

THπQHπ=QHπ.T^\pi_{\mathcal{H}}Q^\pi_{\mathcal{H}} = Q^\pi_{\mathcal{H}}.

Conversely, if a bounded function Q:S×ARQ:\mathcal{S}\times\mathcal{A}\to\mathbb{R} satisfies

THπQ=Q,T^\pi_{\mathcal{H}}Q = Q,

then Q=QHπQ=Q^\pi_{\mathcal{H}}.

Moreover, for any bounded initial function Q0Q_0,

(THπ)kQ0QHπas k.\left(T^\pi_{\mathcal{H}}\right)^k Q_0 \to Q^\pi_{\mathcal{H}} \quad \text{as } k\to\infty .
Proof sketch

Since rewards are bounded and A\mathcal{A} is finite, the entropy term is bounded:

0H(π(s))logA.0 \le \mathcal{H} \left( \pi(\cdot \mid s) \right) \le \log |\mathcal{A}|.

So the discounted soft return is bounded, and QHπQ^\pi_{\mathcal{H}} is well defined.

The one-step decomposition gives

THπQHπ=QHπ.T^\pi_{\mathcal{H}}Q^\pi_{\mathcal{H}} = Q^\pi_{\mathcal{H}}.

Thus QHπQ^\pi_{\mathcal{H}} is a fixed point of THπT^\pi_{\mathcal{H}}.

The operator is a γ\gamma-contraction. For any bounded Q1,Q2Q_1,Q_2,

THπQ1THπQ2γmaxs,aEs,a[Q1(s,a)Q2(s,a)s,a]γQ1Q2.\begin{aligned} \left\| T^\pi_{\mathcal{H}}Q_1 - T^\pi_{\mathcal{H}}Q_2 \right\|_\infty &\le \gamma \max_{s,a} \mathbb{E}_{s',a'} \left[ \left| Q_1(s',a')-Q_2(s',a') \right| \mid s,a \right] \\ &\le \gamma \left\| Q_1-Q_2 \right\|_\infty. \end{aligned}

The reward and entropy terms cancel in the difference, so this is exactly the same contraction argument as before.

By the contraction mapping theorem, THπT^\pi_{\mathcal{H}} has a unique fixed point, and repeated application of the operator converges to it. Since QHπQ^\pi_{\mathcal{H}} is already a fixed point, it must be that unique fixed point.

Softmax policy

Now suppose the soft Q-function is given. What should the policy look like?

Before, we simply chose an action that maximized the Q-value:

πgreedy(s)argmaxaAQ(s,a).\pi^{\mathrm{greedy}}(s) \in \arg\max_{a \in \mathcal{A}} Q(s,a).

In maximum-entropy RL, the policy at state ss should maximize the Q-value and the entropy of the policy:

Eaπ(s)[QH(s,a)]+βH(π(s)).\mathbb{E}_{a \sim \pi(\cdot \mid s)} \left[ Q_{\mathcal{H}}(s,a) \right] + \beta \mathcal{H} \left( \pi(\cdot \mid s) \right).

The greedy argmax is no longer the right solution, because it maximizes only the Q-value and ignores the entropy bonus. We need a policy that balances high Q-values with high entropy.

For a Q-function QQ, define its softmax policy by

π1βQ(as)=exp(1βQ(s,a))a~Aexp(1βQ(s,a~)),if A<π1βQ(as)=exp(1βQ(s,a))Aexp(1βQ(s,a~))da~,if A is continuous\begin{aligned} \pi^{\frac{1}{\beta}Q}(a \mid s) &= \frac{ \exp \left( \frac{1}{\beta}Q(s,a) \right) }{ \sum_{\tilde a \in \mathcal{A}} \exp \left( \frac{1}{\beta}Q(s,\tilde a) \right) }, \quad \text{if } |\mathcal{A}|<\infty \\[0.5em] \pi^{\frac{1}{\beta}Q}(a \mid s) &= \frac{ \exp \left( \frac{1}{\beta}Q(s,a) \right) }{ \int_{\mathcal{A}} \exp \left( \frac{1}{\beta}Q(s,\tilde a) \right) d\tilde a }, \quad \text{if } \mathcal{A} \text{ is continuous} \end{aligned}
Derivation

For simplicity, assume A\mathcal{A} is finite. For a fixed state ss, write πa=π(as)\pi_a=\pi(a \mid s) and Qa=Q(s,a)Q_a=Q(s,a). The optimization problem is

π1βQ(s)=argmaxπ(s)aπaQaβaπalogπa\pi^{\frac{1}{\beta}Q}(\cdot \mid s) = \arg\max_{\pi(\cdot \mid s)} \sum_a \pi_a Q_a - \beta \sum_a \pi_a \log \pi_a

subject to

aπa=1,πa0\sum_a \pi_a=1, \qquad \pi_a \ge 0

The Lagrangian is

L(π,λ)=aπaQaβaπalogπa+λ(aπa1).\mathcal{L}(\pi,\lambda) = \sum_a \pi_a Q_a - \beta \sum_a \pi_a \log \pi_a + \lambda \left( \sum_a \pi_a-1 \right).

Taking the derivative with respect to πa\pi_a gives

Lπa=Qaβ(logπa+1)+λ.\frac{\partial \mathcal{L}}{\partial \pi_a} = Q_a - \beta \left( \log \pi_a+1 \right) + \lambda.

At the optimum,

Qaβ(logπa+1)+λ=0.Q_a - \beta \left( \log \pi_a+1 \right) + \lambda = 0.

Therefore,

logπa=Qaβ+λββ,\log \pi_a = \frac{Q_a}{\beta} + \frac{\lambda-\beta}{\beta},

so

πaexp(Qa/β).\pi_a \propto \exp(Q_a/\beta).

Normalizing over actions gives π1βQ(s)\pi^{\frac{1}{\beta}Q}(\cdot \mid s).

The softmax policy is proportional to exp(1βQ(s,a))\exp(\frac{1}{\beta}Q(s,a)). So every action can still be chosen, but actions with larger Q-values are chosen more often. Also, as β0\beta \to 0, the largest Q-value dominates the exponential, and the policy becomes the greedy argmax policy.

Softmax and log-sum-exp

The policy above is called a softmax policy because it uses the softmax function. Since this name is used in a slightly confusing way, let us pause and make the notation clear.

For β>0\beta>0, define the softmax function with temperature β\beta, softmaxβ:RnRn\operatorname{softmax}_\beta:\mathbb{R}^n\to\mathbb{R}^n, as

softmaxβ(x)=1i=1nexp(1βxi)[exp(1βx1)exp(1βxn)]\operatorname{softmax}_\beta(x) = \frac{ 1 }{ \sum_{i=1}^{n} \exp \left( \frac{1}{\beta}x_i \right) } \begin{bmatrix} \exp \left( \frac{1}{\beta}x_1 \right) \\ \vdots \\ \exp \left( \frac{1}{\beta}x_n \right) \end{bmatrix}

The β=1\beta=1 version is the function commonly called "softmax" in deep learning.

The softmax function can be viewed as a smoothed version of the one-hot argmax:

limβ0softmaxβ(x)=eargmaxixi(if maximizer is unique),\lim_{\beta \to 0} \operatorname{softmax}_\beta(x) = e_{\arg\max_i x_i} \quad \text{(if maximizer is unique)},

where eie_i is the one-hot vector whose ii-th entry is one.

For this reason, this function is sometimes called "softargmax," although in deep learning the standard name is "softmax."

Then what is a smoothed version of the maximum itself? This is the log-sum-exp function LSEβ:RnR\operatorname{LSE}_\beta:\mathbb{R}^n\to\mathbb{R}:

LSEβ(x)=βlogi=1nexp(xi/β).\operatorname{LSE}_\beta(x) = \beta \log \sum_{i=1}^{n} \exp(x_i/\beta). limβ0LSEβ(x)=maxixi.\lim_{\beta \to 0} \operatorname{LSE}_\beta(x) = \max_i x_i.

Finally, the two functions are connected by a simple gradient relation:

softmaxβ(x)=LSEβ(x).\operatorname{softmax}_\beta(x) = \nabla \operatorname{LSE}_\beta(x).

Soft Bellman optimality equation

Now we can write the maximum-entropy version of the Bellman optimality equation.

Theorem. Assume γ(0,1)\gamma \in (0,1), S<|\mathcal{S}|<\infty, A<|\mathcal{A}|<\infty, and rR<|r|\le R<\infty almost surely. Then the optimal soft Q-function QH:S×ARQ^*_{\mathcal{H}}:\mathcal{S}\times\mathcal{A}\to\mathbb{R} exists and satisfies the soft Bellman optimality equation

QH(s,a)=E(r,s)p(s,a)[r+γLSEβ(QH(s,))].Q^*_{\mathcal{H}}(s,a) = \mathbb{E}_{(r,s') \sim p(\cdot \mid s,a)} \left[ r + \gamma \operatorname{LSE}_\beta \left( Q^*_{\mathcal{H}}(s',\cdot) \right) \right].

Equivalently,

QH(s,a)=E(r,s)p(s,a)[r+γEaπ1βQH(s)[QH(s,a)βlogπ1βQH(as)]].\begin{aligned} Q^*_{\mathcal{H}}(s,a) &= \mathbb{E}_{(r,s') \sim p(\cdot \mid s,a)} \left[ r + \gamma \mathbb{E}_{a' \sim \pi^{\frac{1}{\beta}Q^*_{\mathcal{H}}}(\cdot \mid s')} \left[ Q^*_{\mathcal{H}}(s',a') - \beta \log \pi^{\frac{1}{\beta}Q^*_{\mathcal{H}}} \left( a' \mid s' \right) \right] \right]. \end{aligned}

Define the soft Bellman optimality operator THT^*_{\mathcal{H}} by

(THQ)(s,a)=E(r,s)p(s,a)[r+γLSEβ(Q(s,))].\left( T^*_{\mathcal{H}}Q \right)(s,a) = \mathbb{E}_{(r,s') \sim p(\cdot \mid s,a)} \left[ r + \gamma \operatorname{LSE}_\beta \left( Q(s',\cdot) \right) \right].

Then the soft Bellman optimality equation can be written as

THQH=QH.T^*_{\mathcal{H}}Q^*_{\mathcal{H}} = Q^*_{\mathcal{H}}.

Conversely, if a bounded function Q:S×ARQ:\mathcal{S}\times\mathcal{A}\to\mathbb{R} satisfies

THQ=Q,T^*_{\mathcal{H}}Q = Q,

then Q=QHQ=Q^*_{\mathcal{H}}.

Moreover, for any bounded initial function Q0Q_0,

(TH)kQ0QHas k.\left(T^*_{\mathcal{H}}\right)^k Q_0 \to Q^*_{\mathcal{H}} \quad \text{as } k\to\infty .

The corresponding optimal policy is

πH(s)=π1βQH(s).\pi^*_{\mathcal{H}}(\cdot \mid s) = \pi^{\frac{1}{\beta}Q^*_{\mathcal{H}}}(\cdot \mid s).
Proof

The proof follows the same three steps as the hard-max case. The only new ingredient is one identity, which we state first.

0. A key identity

For any bounded QQ, any policy π\pi, and any state ss,

LSEβ(Q(s,))Eaπ(s)[Q(s,a)βlogπ(as)]=βDKL(π(s)π1βQ(s)).\operatorname{LSE}_\beta \left( Q(s,\cdot) \right) - \mathbb{E}_{a \sim \pi(\cdot \mid s)} \left[ Q(s,a) - \beta \log \pi(a \mid s) \right] = \beta D_{\mathrm{KL}} \left( \pi(\cdot \mid s) \,\|\, \pi^{\frac{1}{\beta}Q}(\cdot \mid s) \right).

The left side is the gap between the soft maximum and the expected soft value that the policy π\pi actually achieves. The identity says this gap is exactly a KL divergence: it measures how far π\pi is from the softmax policy of QQ.

This can be checked directly. By definition of the softmax policy,

logπ1βQ(as)=1βQ(s,a)1βLSEβ(Q(s,)).\log \pi^{\frac{1}{\beta}Q}(a \mid s) = \frac{1}{\beta} Q(s,a) - \frac{1}{\beta} \operatorname{LSE}_\beta \left( Q(s,\cdot) \right).

Substituting this into the definition of the KL divergence gives

βDKL(ππ1βQ)=βaπ(as)logπ(as)aπ(as)Q(s,a)+LSEβ(Q(s,)),\beta D_{\mathrm{KL}} \left( \pi \,\|\, \pi^{\frac{1}{\beta}Q} \right) = \beta \sum_a \pi(a \mid s) \log \pi(a \mid s) - \sum_a \pi(a \mid s) Q(s,a) + \operatorname{LSE}_\beta \left( Q(s,\cdot) \right),

and rearranging gives the identity.

Now apply this identity inside the soft Bellman operator. Evaluating the identity at the next state ss' and taking expectations over (r,s)p(s,a)(r,s') \sim p(\cdot \mid s,a),

(THπQ)(s,a)=E(r,s)p(s,a)[r+γEaπ(s)[Q(s,a)βlogπ(as)]]=E(r,s)p(s,a)[r+γLSEβ(Q(s,))]γβEsp(s,a)[DKL(π(s)π1βQ(s))]=(THQ)(s,a)γβEsp(s,a)[DKL(π(s)π1βQ(s))].\begin{aligned} \left( T^\pi_{\mathcal{H}} Q \right)(s,a) &= \mathbb{E}_{(r,s') \sim p(\cdot \mid s,a)} \left[ r + \gamma \, \mathbb{E}_{a' \sim \pi(\cdot \mid s')} \left[ Q(s',a') - \beta \log \pi(a' \mid s') \right] \right] \\ &= \mathbb{E}_{(r,s') \sim p(\cdot \mid s,a)} \left[ r + \gamma \operatorname{LSE}_\beta \left( Q(s',\cdot) \right) \right] - \gamma \beta \, \mathbb{E}_{s' \sim p(\cdot \mid s,a)} \left[ D_{\mathrm{KL}} \left( \pi(\cdot \mid s') \,\|\, \pi^{\frac{1}{\beta}Q}(\cdot \mid s') \right) \right] \\ &= \left( T^*_{\mathcal{H}} Q \right)(s,a) - \gamma \beta \, \mathbb{E}_{s' \sim p(\cdot \mid s,a)} \left[ D_{\mathrm{KL}} \left( \pi(\cdot \mid s') \,\|\, \pi^{\frac{1}{\beta}Q}(\cdot \mid s') \right) \right]. \end{aligned}

In other words, the gap between the two operators is exactly an expected KL divergence:

(THQ)(s,a)(THπQ)(s,a)=γβEsp(s,a)[DKL(π(s)π1βQ(s))].\left( T^*_{\mathcal{H}} Q \right)(s,a) - \left( T^\pi_{\mathcal{H}} Q \right)(s,a) = \gamma \beta \, \mathbb{E}_{s' \sim p(\cdot \mid s,a)} \left[ D_{\mathrm{KL}} \left( \pi(\cdot \mid s') \,\|\, \pi^{\frac{1}{\beta}Q}(\cdot \mid s') \right) \right].

The right side is nonnegative, so this gives an inequality: for any policy π\pi and any bounded QQ,

THπQTHQ.T^\pi_{\mathcal{H}} Q \le T^*_{\mathcal{H}} Q.

The right side is zero exactly when the two distributions are equal at every next state, so the inequality becomes an equality exactly when π\pi is the softmax policy of QQ:

THπ1βQQ=THQ.T^{\pi^{\frac{1}{\beta}Q}}_{\mathcal{H}} Q = T^*_{\mathcal{H}} Q.

We will use both facts several times below.

Note that the key identity also strengthens the earlier softmax derivation. There we showed that the softmax policy maximizes the expected Q-value plus entropy. The identity says more: it gives the maximum value and the exact cost of using any other policy,

maxπ(s)Eaπ(s)[Q(s,a)βlogπ(as)]=LSEβ(Q(s,)),\max_{\pi(\cdot \mid s)} \, \mathbb{E}_{a \sim \pi(\cdot \mid s)} \left[ Q(s,a) - \beta \log \pi(a \mid s) \right] = \operatorname{LSE}_\beta \left( Q(s,\cdot) \right),

where the maximum is attained by π1βQ(s)\pi^{\frac{1}{\beta}Q}(\cdot \mid s), and any other policy falls short by exactly βDKL(ππ1βQ)\beta D_{\mathrm{KL}}(\pi \,\|\, \pi^{\frac{1}{\beta}Q}).

1. Existence and uniqueness of the fixed point

First, THT^*_{\mathcal{H}} is a strict contraction with respect to \|\cdot\|_\infty. We use two facts about LSEβ\operatorname{LSE}_\beta. It is monotone:

xy componentwise    LSEβ(x)LSEβ(y),x \le y \text{ componentwise} \implies \operatorname{LSE}_\beta(x) \le \operatorname{LSE}_\beta(y),

and adding a constant to every entry shifts the output by the same constant:

LSEβ(x1+c,,xn+c)=LSEβ(x1,,xn)+c.\operatorname{LSE}_\beta(x_1 + c, \ldots, x_n + c) = \operatorname{LSE}_\beta(x_1, \ldots, x_n) + c.

Now take any x,yx, y. Since xiyi+xyx_i \le y_i + \|x - y\|_\infty for every ii, the two facts give

LSEβ(x)LSEβ(y)+xy.\operatorname{LSE}_\beta(x) \le \operatorname{LSE}_\beta(y) + \|x - y\|_\infty.

Swapping xx and yy gives the other direction, so

LSEβ(x)LSEβ(y)xy.\left| \operatorname{LSE}_\beta(x) - \operatorname{LSE}_\beta(y) \right| \le \|x - y\|_\infty.

Therefore, for any bounded Q1,Q2Q_1, Q_2,

THQ1THQ2γmaxs,aEsp(s,a)[LSEβ(Q1(s,))LSEβ(Q2(s,))s,a]γQ1Q2.\begin{aligned} \left\| T^*_{\mathcal{H}} Q_1 - T^*_{\mathcal{H}} Q_2 \right\|_\infty &\le \gamma \max_{s,a} \mathbb{E}_{s' \sim p(\cdot \mid s,a)} \left[ \left| \operatorname{LSE}_\beta \left( Q_1(s',\cdot) \right) - \operatorname{LSE}_\beta \left( Q_2(s',\cdot) \right) \right| \mid s,a \right] \\ &\le \gamma \left\| Q_1 - Q_2 \right\|_\infty. \end{aligned}

Note that the contraction proof for the hard-max operator used exactly these two properties of the max, and nothing else. LSEβ\operatorname{LSE}_\beta has both, so the same argument goes through.

So THT^*_{\mathcal{H}} has a unique bounded fixed point. Denote it by Qˉ\bar{Q}:

THQˉ=Qˉ.T^*_{\mathcal{H}} \bar{Q} = \bar{Q}.

Also, for any bounded Q0Q_0, repeated application converges: (TH)kQ0Qˉ(T^*_{\mathcal{H}})^k Q_0 \to \bar{Q}.

At this point, we only know that Qˉ\bar{Q} is the fixed point of THT^*_{\mathcal{H}}. We still need to show that it is the optimal soft Q-function.

2. Define πˉ\bar{\pi} and show Qˉ=QHπˉ\bar{Q} = Q^{\bar{\pi}}_{\mathcal{H}}

Next, define πˉ\bar{\pi} to be the softmax policy of Qˉ\bar{Q}:

πˉ=π1βQˉ.\bar{\pi} = \pi^{\frac{1}{\beta}\bar{Q}}.

We do not yet know that πˉ\bar{\pi} is optimal.

By the equality case of the key identity, the KL term vanishes when the policy is the softmax policy of the same Q-function. So

THπˉQˉ=THQˉ=Qˉ.T^{\bar{\pi}}_{\mathcal{H}} \bar{Q} = T^*_{\mathcal{H}} \bar{Q} = \bar{Q}.

This says Qˉ\bar{Q} is a bounded fixed point of THπˉT^{\bar{\pi}}_{\mathcal{H}}. But the soft Bellman equation theorem tells us that THπˉT^{\bar{\pi}}_{\mathcal{H}} has a unique bounded fixed point, namely QHπˉQ^{\bar{\pi}}_{\mathcal{H}}. Therefore

Qˉ=QHπˉ.\bar{Q} = Q^{\bar{\pi}}_{\mathcal{H}}.

So the softmax policy πˉ\bar{\pi} has soft Q-function Qˉ\bar{Q}.

3. Show πˉ\bar{\pi} is optimal

It remains to show that no other policy can do better. We use two simple facts:

THπQTHQfor any policy π,T^\pi_{\mathcal{H}} Q \le T^*_{\mathcal{H}} Q \quad \text{for any policy } \pi,

and if Q1Q2Q_1 \le Q_2 pointwise, then

THQ1THQ2.T^*_{\mathcal{H}} Q_1 \le T^*_{\mathcal{H}} Q_2.

The first fact is the key identity from step 0. The second holds because LSEβ\operatorname{LSE}_\beta and expectations are both monotone.

For any policy π\pi,

QHπ=THπQHπTHQHπ.Q^\pi_{\mathcal{H}} = T^\pi_{\mathcal{H}} Q^\pi_{\mathcal{H}} \le T^*_{\mathcal{H}} Q^\pi_{\mathcal{H}}.

Applying the monotonicity of THT^*_{\mathcal{H}} repeatedly,

QHπTHQHπ(TH)2QHπ.Q^\pi_{\mathcal{H}} \le T^*_{\mathcal{H}} Q^\pi_{\mathcal{H}} \le \left(T^*_{\mathcal{H}}\right)^2 Q^\pi_{\mathcal{H}} \le \cdots.

Since THT^*_{\mathcal{H}} is a contraction, (TH)kQHπQˉ(T^*_{\mathcal{H}})^k Q^\pi_{\mathcal{H}} \to \bar{Q}. Therefore QHπQˉQ^\pi_{\mathcal{H}} \le \bar{Q} for every policy π\pi.

QHπˉ=QˉandQHπQˉπ.Q^{\bar{\pi}}_{\mathcal{H}} = \bar{Q} \quad \text{and} \quad Q^\pi_{\mathcal{H}} \le \bar{Q} \quad \forall \pi.

Thus πˉ\bar{\pi} is an optimal policy, and Qˉ\bar{Q} is the optimal soft Q-function: QH=QˉQ^*_{\mathcal{H}} = \bar{Q}. This also gives the claimed form of the optimal policy, πH=π1βQH\pi^*_{\mathcal{H}} = \pi^{\frac{1}{\beta}Q^*_{\mathcal{H}}}.

Finally, if a bounded function QQ satisfies the soft Bellman optimality equation, then THQ=QT^*_{\mathcal{H}} Q = Q. Since THT^*_{\mathcal{H}} has a unique fixed point, Q=QHQ = Q^*_{\mathcal{H}}.

Soft Q-learning

The theory above gives us soft Q-iteration:

Qk+1=THQk,limkQk=QH.\begin{aligned} Q_{k+1} &= T^*_{\mathcal{H}} Q_k, \\ \lim_{k \to \infty} Q_k &= Q^*_{\mathcal{H}}. \end{aligned}

Earlier, we derived the DQN algorithm from Q-iteration. Now we can apply the same idea to soft Q-iteration, which gives the following Soft Q-learning algorithm:13

Algorithm: Soft Q-learningInitialize Qϕ and target network QϕInitialize replay buffer Drepeataπ1βQϕ(s)Take action a and observe (r,s)DD{(s,a,r,s)}for each sampled minibatch BD doyiri+γLSEβ(Qϕ(si,)),(si,ai,ri,si)BϕϕαQϕ1BiB12(Qϕ(si,ai)sg[yi])2Every C gradient updates, set ϕϕend foruntil convergence\begin{array}{l} \hline \textbf{Algorithm: Soft Q-learning} \\ \hline \text{Initialize } Q_\phi \text{ and target network } Q_{\phi^-} \\ \text{Initialize replay buffer } \mathcal{D} \\ \textbf{repeat} \\ \quad a \sim \pi^{\frac{1}{\beta}Q_\phi}(\cdot \mid s) \\ \quad \text{Take action } a \text{ and observe } (r,s') \\ \quad \mathcal{D} \leftarrow \mathcal{D} \cup \{(s,a,r,s')\} \\ \quad \textbf{for each sampled minibatch } B \subset \mathcal{D} \textbf{ do} \\ \quad\quad y_i \leftarrow r_i+\gamma \operatorname{LSE}_\beta \left( Q_{\phi^-}(s_i',\cdot) \right), \quad (s_i,a_i,r_i,s_i') \in B \\ \quad\quad \phi \leftarrow \phi -\alpha_Q \nabla_\phi \displaystyle \frac{1}{|B|} \sum_{i \in B} \frac{1}{2} \left( Q_\phi(s_i,a_i)-\operatorname{sg}[y_i] \right)^2 \\ \quad\quad \text{Every } C \text{ gradient updates, set } \phi^- \leftarrow \phi \\ \quad \textbf{end for} \\ \textbf{until convergence} \\ \hline \end{array}

Compared with DQN, two parts of the algorithm have changed.

First, action sampling changes from ϵ\epsilon-greedy sampling to sampling from the softmax policy:

DQN:a{argmaxaAQϕ(s,a),w.p. 1ϵUniform(A),w.p. ϵSoft Q-learning:aπ1βQϕ(s)\begin{array}{ll} \text{DQN:} & a \sim \begin{cases} \displaystyle\arg\max_{a \in \mathcal{A}} Q_\phi(s,a), & \text{w.p. } 1-\epsilon \\ \operatorname{Uniform}(\mathcal{A}), & \text{w.p. } \epsilon \end{cases} \\[0.5em] \text{Soft Q-learning:} & a \sim \pi^{\frac{1}{\beta}Q_\phi}(\cdot \mid s) \end{array}

Second, target evaluation changes from a hard maximum to a soft maximum:

DQN:ri+γmaxaAQϕ(si,a)Soft Q-learning:ri+γLSEβ(Qϕ(si,))\begin{array}{ll} \text{DQN:} & r_i+\gamma \max_{a' \in \mathcal{A}} Q_{\phi^-}(s_i',a') \\[0.5em] \text{Soft Q-learning:} & r_i+\gamma \operatorname{LSE}_\beta \left( Q_{\phi^-}(s_i',\cdot) \right) \end{array}

Note that Soft Q-learning removes explicit ϵ\epsilon-greedy exploration from action sampling, because the maximum-entropy objective already encourages exploration.

In a finite action space, both soft replacements are straightforward to implement. We can evaluate Qϕ(s,a)Q_\phi(s,a) for every action, normalize the softmax probabilities, and sample from them. We can also compute LSEβ\operatorname{LSE}_\beta by summing over all actions.

The continuous case is different. Recall from Q-learning with continuous actions that continuous action spaces are difficult because max\max and argmax\arg\max over actions are no longer easy to compute. Soft Q-learning has the same issue in soft form.

For target evaluation, we need

LSEβ(Qϕ(s,))=βlogAexp(1βQϕ(s,a))da.\operatorname{LSE}_\beta \left( Q_{\phi^-}(s',\cdot) \right) = \beta \log \int_{\mathcal{A}} \exp \left( \frac{1}{\beta} Q_{\phi^-}(s',a') \right) da'.

For action sampling, we need

aπ1βQϕ(s),π1βQϕ(as)exp(1βQϕ(s,a)).a \sim \pi^{\frac{1}{\beta}Q_\phi}(\cdot \mid s), \qquad \pi^{\frac{1}{\beta}Q_\phi}(a \mid s) \propto \exp \left( \frac{1}{\beta} Q_\phi(s,a) \right).

For a general neural network QϕQ_\phi, the log-sum-exp term is intractable to compute, and sampling from the softmax policy is not straightforward. The Soft Q-learning paper solves these problems as follows.

Estimating the target

The log-sum-exp term in the target can be estimated by importance sampling. For any proposal distribution q(s)q(\cdot \mid s') whose density is positive on the action space,

Aexp(1βQ(s,a))da=Eaq(s)[exp(1βQ(s,a))q(as)].\int_{\mathcal{A}} \exp\left(\frac{1}{\beta}Q(s',a')\right) da' = \mathbb{E}_{a' \sim q(\cdot \mid s')} \left[ \frac{\exp\left(\frac{1}{\beta}Q(s',a')\right)} {q(a' \mid s')} \right].

With samples a1,,aMq(s)a'_1,\ldots,a'_M \sim q(\cdot \mid s'), this gives the estimate

V^Q(s)=βlog(1Mj=1Mexp(1βQ(s,aj))q(ajs)).\widehat V_Q(s') = \beta \log \left( \frac{1}{M} \sum_{j=1}^{M} \frac{ \exp \left( \frac{1}{\beta} Q(s',a'_j) \right) }{ q(a'_j \mid s') } \right).

So the soft Q-learning target becomes

yi=ri+γV^Qϕ(si).y_i = r_i+\gamma \widehat V_{Q_{\phi^-}}(s_i').

One simple choice is to sample actions uniformly from a bounded action space. For example, if A=[1,1]d\mathcal{A}=[-1,1]^d, then

q(as)=12d.q(a'\mid s') = \frac{1}{2^d}.

The estimate becomes

V^Q(s)=βlogj=1Mexp(1βQ(s,aj))βlogM+βdlog2.\widehat V_Q(s') = \beta \log \sum_{j=1}^{M} \exp \left( \frac{1}{\beta} Q(s',a'_j) \right) - \beta \log M + \beta d\log 2.

Sampling from the softmax policy

How can we draw an action aπ1βQϕ(s)a \sim \pi^{\frac{1}{\beta}Q_\phi}(\cdot \mid s) for a given state ss?

Generic sampling methods such as MCMC could be used in principle, but they would require an iterative sampling procedure for every action, which is too expensive in practice.

Soft Q-learning instead trains a separate network to be the sampler. Let

a=fθ(ξ;s),ξN(0,I).a = f_\theta(\xi;s), \qquad \xi \sim \mathcal{N}(0,I).

For each state ss, the noise-conditioned network fθf_\theta induces a distribution over actions. Call this distribution πθ(s)\pi_\theta(\cdot \mid s). Ideally, we want

πθ(s)π1βQϕ(s).\pi_\theta(\cdot \mid s) \approx \pi^{\frac{1}{\beta}Q_\phi}(\cdot \mid s).

Equivalently, we would like to minimize

Jπ(θ;s)=DKL(πθ(s)π1βQϕ(s)).J_\pi(\theta;s) = D_{\mathrm{KL}} \left( \pi_\theta(\cdot \mid s) \, \middle\|\, \pi^{\frac{1}{\beta}Q_\phi}(\cdot \mid s) \right).

Writing Zϕ(s)Z_\phi(s) for the normalizing constant, expanding the KL divergence gives

Jπ(θ;s)=Eaπθ(s)[logπθ(as)1βQϕ(s,a)]+logZϕ(s)=Eξ[logπθ(fθ(ξ;s)s)1βQϕ(s,fθ(ξ;s))]+logZϕ(s).\begin{aligned} J_\pi(\theta;s) &= \mathbb{E}_{a \sim \pi_\theta(\cdot \mid s)} \left[ \log \pi_\theta(a \mid s) - \frac{1}{\beta} Q_\phi(s,a) \right] + \log Z_\phi(s) \\ &= \mathbb{E}_{\xi} \left[ \log \pi_\theta \left( f_\theta(\xi;s) \mid s \right) - \frac{1}{\beta} Q_\phi \left( s, f_\theta(\xi;s) \right) \right] + \log Z_\phi(s). \end{aligned}

To train fθf_\theta, we need the gradient of this objective:

θJπ(θ;s)=θEξ[logπθ(fθ(ξ;s)s)1βQϕ(s,fθ(ξ;s))].\nabla_\theta J_\pi(\theta;s) = \nabla_\theta \mathbb{E}_{\xi} \left[ \log \pi_\theta \left( f_\theta(\xi;s) \mid s \right) - \frac{1}{\beta} Q_\phi \left( s, f_\theta(\xi;s) \right) \right].

The QQ term is straightforward. We can differentiate Qϕ(s,fθ(ξ;s))Q_\phi(s,f_\theta(\xi;s)) through the sampler fθf_\theta.

The problem is the density term logπθ(as)\log \pi_\theta(a \mid s). The network fθf_\theta lets us sample actions, but it does not directly give their probabilities. In other words, we can generate a=fθ(ξ;s)a=f_\theta(\xi;s), but we generally cannot evaluate πθ(as)\pi_\theta(a \mid s).

One way around this is to restrict the sampler to a distribution with a tractable density. For example, if the policy is Gaussian and the network outputs μθ(s)\mu_\theta(s) and σθ(s)\sigma_\theta(s), then we can sample

a=μθ(s)+σθ(s)ξ,ξN(0,I),a = \mu_\theta(s) + \sigma_\theta(s)\odot \xi, \qquad \xi \sim \mathcal{N}(0,I),

and evaluate logπθ(as)\log \pi_\theta(a \mid s) explicitly. Later, Soft Actor-Critic uses this kind of tractable stochastic policy.14

Soft Q-learning takes a different route. It keeps the more expressive implicit sampler fθf_\theta, and estimates the sampler update from samples without evaluating logπθ(as)\log \pi_\theta(a \mid s) directly.

Amortized SVGD

Soft Q-learning uses amortized SVGD to update the sampler. Instead of trying to evaluate the sampler density πθ(as)\pi_\theta(a \mid s), it directly updates the sampler toward the same target as before:

πθ(s)π1βQϕ(s).\pi_\theta(\cdot \mid s) \approx \pi^{\frac{1}{\beta}Q_\phi}(\cdot \mid s).

The idea of amortized SVGD is to use samples from the sampler itself. If the sampled actions are not distributed like the target softmax policy, we ask how those actions should move to look more like samples from the target. Then we update fθf_\theta so that future samples are already shifted in those directions.

Concretely, first sample particles from the current sampler:

ak=fθ(ξk;s),ξkN(0,I),k=1,,K.a_k=f_\theta(\xi_k;s), \qquad \xi_k\sim\mathcal{N}(0,I), \qquad k=1,\ldots,K.

SVGD assigns a movement direction to each particle:

Δ(a)=1Kj=1K[κ(aj,a)ajlogπ1βQϕ(ajs)+ajκ(aj,a)]=1Kj=1K[κ(aj,a)aj(1βQϕ(s,aj))+ajκ(aj,a)].\begin{aligned} \Delta(a) &= \frac{1}{K} \sum_{j=1}^{K} \left[ \kappa(a_j,a) \nabla_{a_j} \log \pi^{\frac{1}{\beta}Q_\phi}(a_j \mid s) + \nabla_{a_j} \kappa(a_j,a) \right] \\ &= \frac{1}{K} \sum_{j=1}^{K} \left[ \kappa(a_j,a) \nabla_{a_j} \left( \frac{1}{\beta}Q_\phi(s,a_j) \right) + \nabla_{a_j} \kappa(a_j,a) \right]. \end{aligned}

Here κ\kappa is a kernel, which measures similarity between actions. The first term pulls particles toward high-density actions. The second term makes nearby particles repel each other, which helps the particles represent a distribution instead of all chasing the same high-value action.

Ordinary SVGD would use this velocity field to iteratively move particles toward the target distribution. Amortized SVGD instead uses the velocity field as a training signal for the sampler: update θ\theta so that fθf_\theta produces actions shifted in those directions.

ΔθEξ[Δ(fθ(ξ;s))fθ(ξ;s)θ].\Delta \theta \propto \mathbb{E}_{\xi} \left[ \Delta \left( f_\theta(\xi;s) \right)^\top \frac{\partial f_\theta(\xi;s)}{\partial \theta} \right].

Algorithm summary

Putting the target update and sampler update together, we get the following algorithm:

Algorithm: Soft Q-learningInitialize Qϕ,fθ and target network QϕInitialize replay buffer DrepeatξN(0,I)afθ(ξ;s)Take action a and observe (r,s)DD{(s,a,r,s)}for each sampled minibatch BD doFor each iB, sample aijq(si), j=1,,MV^iβlog(1Mj=1Mexp(1βQϕ(si,aij))q(aijsi))yiri+γV^iϕϕαQϕ1BiB12(Qϕ(si,ai)sg[yi])2For each iB, sample particles a~ik=fθ(ξik;si), k=1,,KCompute the SVGD field Δi using Qϕ and the particles a~ikCompute the sampler update direction Δθ^ from Δiθθ+απΔθ^Every C gradient updates, set ϕϕend foruntil convergence\begin{array}{l} \hline \textbf{Algorithm: Soft Q-learning} \\ \hline \text{Initialize } Q_\phi, f_\theta \text{ and target network } Q_{\phi^-} \\ \text{Initialize replay buffer } \mathcal{D} \\ \textbf{repeat} \\ \quad \xi \sim \mathcal{N}(0,I) \\ \quad a \leftarrow f_\theta(\xi;s) \\ \quad \text{Take action } a \text{ and observe } (r,s') \\ \quad \mathcal{D} \leftarrow \mathcal{D} \cup \{(s,a,r,s')\} \\ \quad \textbf{for each sampled minibatch } B \subset \mathcal{D} \textbf{ do} \\ \quad\quad \text{For each } i \in B,\text{ sample } a_{ij}' \sim q(\cdot \mid s_i'),\ j=1,\ldots,M \\ \quad\quad \widehat V_i \leftarrow \beta \log \left( \displaystyle \frac{1}{M} \sum_{j=1}^{M} \frac{ \exp \left( \frac{1}{\beta}Q_{\phi^-}(s_i',a_{ij}') \right) }{ q(a_{ij}' \mid s_i') } \right) \\ \quad\quad y_i \leftarrow r_i+\gamma \widehat V_i \\ \quad\quad \phi \leftarrow \phi -\alpha_Q \nabla_\phi \displaystyle \frac{1}{|B|} \sum_{i \in B} \frac{1}{2} \left( Q_\phi(s_i,a_i)-\operatorname{sg}[y_i] \right)^2 \\ \quad\quad \text{For each } i \in B,\text{ sample particles } \tilde a_{ik}=f_\theta(\xi_{ik};s_i),\ k=1,\ldots,K \\ \quad\quad \text{Compute the SVGD field } \Delta_i \text{ using } Q_\phi \text{ and the particles } \tilde a_{ik} \\ \quad\quad \text{Compute the sampler update direction } \widehat{\Delta\theta} \text{ from } \Delta_i \\ \quad\quad \theta \leftarrow \theta+\alpha_\pi \widehat{\Delta\theta} \\ \quad\quad \text{Every } C \text{ gradient updates, set } \phi^- \leftarrow \phi \\ \quad \textbf{end for} \\ \textbf{until convergence} \\ \hline \end{array}

The algorithm resembles actor-critic structurally, but fθf_\theta is better understood as an approximate sampler for the softmax policy defined by QϕQ_\phi, not as a separate policy being evaluated by QϕQ_\phi.

Why use an expressive sampler?

Why does Soft Q-learning keep the expressive sampler instead of using a simpler Gaussian policy that would avoid amortized SVGD altogether?

The paper motivates this choice by pointing to multimodality. The softmax policy defined by the Q-function can assign probability to multiple high-value action regions, while a deterministic policy or a simple Gaussian policy tends to represent only one behavior mode. The paper argues that this can help exploration, because the agent can keep several behaviors available before it knows which one is best, and can also help pretraining, because a broad maximum-entropy policy can contain several useful behaviors for later tasks. The swimmer, quadruped maze, and quadruped pretraining experiments illustrate these points.13

Soft Actor-Critic (SAC)

Note.

  • SAC is not Q-learning in the same sense as DQN or Soft Q-learning. I include it here because it belongs to the same maximum-entropy RL story, and it is easiest to understand right after Soft Q-learning.
  • There are two SAC papers.1415 The second paper refines the original SAC algorithm and is closer to the SAC implementation that is standard today. In this section, we will mainly follow that later version.

Soft Q-learning tries to learn the optimal soft Q-function directly. Soft Actor-Critic takes a slightly different view: it starts from policy iteration.

Policy iteration alternates two steps:

repeatQπkevaluate the current policy πkπk+1improve the policy using Qπkuntil convergence\begin{array}{l} \textbf{repeat} \\ \quad Q^{\pi_k} \leftarrow \text{evaluate the current policy } \pi_k \\ \quad \pi_{k+1} \leftarrow \text{improve the policy using } Q^{\pi_k} \\ \textbf{until convergence} \end{array}

In classical policy iteration, this improvement step is greedy. After evaluating πk\pi_k, we define the next policy by choosing the action with the largest current Q-value:

πk+1(s)=argmaxaQπk(s,a).\pi_{k+1}(s) = \arg\max_a Q^{\pi_k}(s,a).

The policy-gradient methods from the previous post can also be viewed as approximate versions of this evaluate-and-improve loop. The critic evaluates the current policy, and the actor update improves the policy using that estimate.

SAC uses the maximum-entropy version of the same idea. The policy evaluation step is the soft version of the critic update we saw in actor-critic methods. Given a sampled transition (si,ai,ri,si)(s_i,a_i,r_i,s_i'), we first sample the next action from the current policy:

aiπθk(si).a_i' \sim \pi_{\theta_k}(\cdot \mid s_i').

Then we compute the soft Bellman target:

yi=ri+γ[Qϕ(si,ai)βlogπθk(aisi)].y_i = r_i + \gamma \left[ Q_{\phi^-}(s_i',a_i') - \beta \log \pi_{\theta_k}(a_i' \mid s_i') \right].

The critic is trained to fit this target:

JQ(ϕ)=1BiB12(Qϕ(si,ai)sg[yi])2.J_Q(\phi) = \frac{1}{|B|} \sum_{i \in B} \frac{1}{2} \left( Q_\phi(s_i,a_i) - \operatorname{sg}[y_i] \right)^2.

For the policy improvement step, SAC uses the maximum-entropy version of the greedy update. We already showed that, for the maximum-entropy objective, the greedy policy for a given Q-function is the softmax policy

π1βQHπk(s).\pi^{\frac{1}{\beta}Q^{\pi_k}_{\mathcal{H}}}(\cdot \mid s).

SAC chooses the closest policy to this softmax policy inside a policy class Π\Pi by minimizing the KL divergence. We will make the policy class concrete below.

πk+1(s)=argminπΠDKL(π(s)π1βQHπk(s)).\pi_{k+1}(\cdot \mid s) = \arg\min_{\pi \in \Pi} D_{\mathrm{KL}} \left( \pi(\cdot \mid s) \,\|\, \pi^{\frac{1}{\beta}Q^{\pi_k}_{\mathcal{H}}}(\cdot \mid s) \right).

In the actor-critic methods from the previous post, the policy update came directly from a policy-gradient estimate:

θJ(θ)θlogπθ(atst)A^t(k).\nabla_\theta J(\theta) \approx \nabla_\theta \log \pi_\theta(a_t \mid s_t)\hat{A}_t^{(k)}.

So the update direction was explicitly tied to the objective J(θ)J(\theta). Here, the improvement step is written differently: we choose a new policy by a KL projection onto the softmax policy induced by QHπkQ^{\pi_k}_{\mathcal{H}}. It is not immediately obvious that this projection really improves the policy. The next theorem shows that it does, in the exact policy-iteration setting.

Theorem. Suppose QHπkQ^{\pi_k}_{\mathcal{H}} is the exact soft Q-function of the current policy πkΠ\pi_k \in \Pi. Define the next policy by

πk+1(s)=argminπΠDKL(π(s)π1βQHπk(s)).\pi_{k+1}(\cdot \mid s) = \arg\min_{\pi \in \Pi} D_{\mathrm{KL}} \left( \pi(\cdot \mid s) \,\|\, \pi^{\frac{1}{\beta}Q^{\pi_k}_{\mathcal{H}}}(\cdot \mid s) \right).

Repeating this improvement step converges to a policy πΠ\pi_\star \in \Pi that is optimal within Π\Pi:

QHπ(s,a)QHπ(s,a)πΠ, s,a.Q^{\pi_\star}_{\mathcal{H}}(s,a) \ge Q^{\pi}_{\mathcal{H}}(s,a) \quad \forall \pi \in \Pi,\ \forall s,a.
Proof

Since πk+1\pi_{k+1} minimizes the KL divergence and πk\pi_k is also feasible,

DKL(πk+1(s)π1βQHπk(s))DKL(πk(s)π1βQHπk(s)).D_{\mathrm{KL}} \left( \pi_{k+1}(\cdot \mid s) \,\|\, \pi^{\frac{1}{\beta}Q^{\pi_k}_{\mathcal{H}}}(\cdot \mid s) \right) \le D_{\mathrm{KL}} \left( \pi_k(\cdot \mid s) \,\|\, \pi^{\frac{1}{\beta}Q^{\pi_k}_{\mathcal{H}}}(\cdot \mid s) \right).

Now use the key identity from the soft Bellman optimality proof. For any policy π\pi,

(THπQ)(s,a)=(THQ)(s,a)γβEsp(s,a)[DKL(π(s)π1βQ(s))].\left(T^\pi_{\mathcal{H}}Q\right)(s,a) = \left(T^*_{\mathcal{H}}Q\right)(s,a) - \gamma\beta \mathbb{E}_{s' \sim p(\cdot \mid s,a)} \left[ D_{\mathrm{KL}} \left( \pi(\cdot \mid s') \,\|\, \pi^{\frac{1}{\beta}Q}(\cdot \mid s') \right) \right].

Apply this at Q=QHπkQ=Q^{\pi_k}_{\mathcal{H}}, once with πk\pi_k and once with πk+1\pi_{k+1}. Since QHπkQ^{\pi_k}_{\mathcal{H}} is the fixed point of THπkT^{\pi_k}_{\mathcal{H}},

QHπk(s,a)=(THπkQHπk)(s,a)=(THQHπk)(s,a)γβEsp(s,a)[DKL(πk(s)π1βQHπk(s))],Q^{\pi_k}_{\mathcal{H}}(s,a) = \left( T^{\pi_k}_{\mathcal{H}}Q^{\pi_k}_{\mathcal{H}} \right)(s,a) = \left( T^*_{\mathcal{H}}Q^{\pi_k}_{\mathcal{H}} \right)(s,a) - \gamma\beta \mathbb{E}_{s' \sim p(\cdot \mid s,a)} \left[ D_{\mathrm{KL}} \left( \pi_k(\cdot \mid s') \,\|\, \pi^{\frac{1}{\beta}Q^{\pi_k}_{\mathcal{H}}}(\cdot \mid s') \right) \right],

while

(THπk+1QHπk)(s,a)=(THQHπk)(s,a)γβEsp(s,a)[DKL(πk+1(s)π1βQHπk(s))].\left( T^{\pi_{k+1}}_{\mathcal{H}}Q^{\pi_k}_{\mathcal{H}} \right)(s,a) = \left( T^*_{\mathcal{H}}Q^{\pi_k}_{\mathcal{H}} \right)(s,a) - \gamma\beta \mathbb{E}_{s' \sim p(\cdot \mid s,a)} \left[ D_{\mathrm{KL}} \left( \pi_{k+1}(\cdot \mid s') \,\|\, \pi^{\frac{1}{\beta}Q^{\pi_k}_{\mathcal{H}}}(\cdot \mid s') \right) \right].

Subtracting the two equations cancels the THQHπkT^*_{\mathcal{H}}Q^{\pi_k}_{\mathcal{H}} term. Since the new policy has no larger KL divergence than the old policy, we get

THπk+1QHπkQHπk.T^{\pi_{k+1}}_{\mathcal{H}}Q^{\pi_k}_{\mathcal{H}} \ge Q^{\pi_k}_{\mathcal{H}}.

So one soft Bellman backup using the new policy already improves on the old Q-function. Then we apply the same new-policy Bellman operator repeatedly:

QHπkTHπk+1QHπk(THπk+1)2QHπkQHπk+1.Q^{\pi_k}_{\mathcal{H}} \le T^{\pi_{k+1}}_{\mathcal{H}}Q^{\pi_k}_{\mathcal{H}} \le \left(T^{\pi_{k+1}}_{\mathcal{H}}\right)^2Q^{\pi_k}_{\mathcal{H}} \le \cdots \to Q^{\pi_{k+1}}_{\mathcal{H}}.

Therefore,

QHπk+1QHπk.Q^{\pi_{k+1}}_{\mathcal{H}} \ge Q^{\pi_k}_{\mathcal{H}}.

So each exact soft policy improvement step produces a policy whose soft Q-function is no worse than before.

Repeating this argument gives a monotone sequence

QHπ0QHπ1QHπ2.Q^{\pi_0}_{\mathcal{H}} \le Q^{\pi_1}_{\mathcal{H}} \le Q^{\pi_2}_{\mathcal{H}} \le \cdots.

This monotone sequence is bounded above by the optimal soft Q-function, so it converges. Let the limiting policy be π\pi_\star.

At convergence, the policy improvement step is stationary. Therefore π\pi_\star is the KL minimizer for its own Q-function:

DKL(π(s)π1βQHπ(s))DKL(π(s)π1βQHπ(s))πΠ.D_{\mathrm{KL}} \left( \pi_\star(\cdot \mid s) \,\|\, \pi^{\frac{1}{\beta}Q^{\pi_\star}_{\mathcal{H}}}(\cdot \mid s) \right) \le D_{\mathrm{KL}} \left( \pi(\cdot \mid s) \,\|\, \pi^{\frac{1}{\beta}Q^{\pi_\star}_{\mathcal{H}}}(\cdot \mid s) \right) \quad \forall \pi \in \Pi.

Now compare π\pi_\star against any other policy πΠ\pi \in \Pi. Using the same key identity at Q=QHπQ=Q^{\pi_\star}_{\mathcal{H}} gives

THπQHπTHπQHπ=QHπ.T^\pi_{\mathcal{H}}Q^{\pi_\star}_{\mathcal{H}} \le T^{\pi_\star}_{\mathcal{H}}Q^{\pi_\star}_{\mathcal{H}} = Q^{\pi_\star}_{\mathcal{H}}.

Applying THπT^\pi_{\mathcal{H}} repeatedly preserves the inequality and converges to QHπQ^\pi_{\mathcal{H}}:

QHπ=limm(THπ)mQHπQHπ.Q^\pi_{\mathcal{H}} = \lim_{m\to\infty} \left(T^\pi_{\mathcal{H}}\right)^m Q^{\pi_\star}_{\mathcal{H}} \le Q^{\pi_\star}_{\mathcal{H}}.

Thus no policy in Π\Pi has a larger soft Q-function than π\pi_\star.

Like Soft Q-learning, SAC represents the policy by sampling through a network:

a=fθ(ξ;s),ξN(0,I).a=f_\theta(\xi;s), \qquad \xi \sim \mathcal{N}(0,I).

As we saw in Soft Q-learning, the gradient of the KL divergence in the actor update is

θDKL(πθ(s)π1βQϕ(s))=Eξ[θlogπθ(fθ(ξ;s)s)1βθQϕ(s,fθ(ξ;s))].\nabla_\theta D_{\mathrm{KL}}\left(\pi_\theta(\cdot \mid s)\,\|\,\pi^{\frac{1}{\beta}Q_\phi}(\cdot \mid s)\right)=\mathbb{E}_{\xi}\left[\nabla_\theta\log \pi_\theta(f_\theta(\xi;s) \mid s)-\frac{1}{\beta}\nabla_\theta Q_\phi(s,f_\theta(\xi;s))\right].

As before, the issue is whether we can evaluate the density term logπθ(as)\log \pi_\theta(a \mid s). Unlike Soft Q-learning, SAC chooses a policy class whose density can be evaluated explicitly.

For example, SAC can use a squashed Gaussian policy:

a=tanh(μθ(s)+exp(τθ(s))ξ),ξN(0,I).a = \tanh \left( \mu_\theta(s) + \exp(\tau_\theta(s))\odot \xi \right), \qquad \xi \sim \mathcal{N}(0,I). logπθ(as)=j=1dτθ,j(s)12j=1d(tanh1(aj)μθ,j(s)exp(τθ,j(s)))2j=1dlog(1aj2)+C.\log \pi_\theta(a \mid s) = -\sum_{j=1}^{d} \tau_{\theta,j}(s) - \frac{1}{2} \sum_{j=1}^{d} \left( \frac{ \tanh^{-1}(a_j)-\mu_{\theta,j}(s) }{ \exp(\tau_{\theta,j}(s)) } \right)^2 - \sum_{j=1}^{d} \log(1-a_j^2) + C.

With this explicit density, SAC can optimize the actor objective directly by backpropagation, without using amortized SVGD.

Putting these pieces together gives the SAC update:

Algorithm: Soft Actor-CriticInitialize Qϕ, πθ, target network QϕInitialize replay buffer Drepeataπθ(s)Take action a and observe (r,s)DD{(s,a,r,s)}for each sampled minibatch BD doaiπθ(si)yiri+γ[Qϕ(si,ai)βlogπθ(aisi)]ϕϕαQϕ1BiB12(Qϕ(si,ai)sg[yi])2a~ifθ(ξi;si),ξiN(0,I)θθαπθ1BiB[βlogπθ(a~isi)Qϕ(si,a~i)]ϕτϕ+(1τ)ϕend foruntil convergence\begin{array}{l} \hline \textbf{Algorithm: Soft Actor-Critic} \\ \hline \text{Initialize } Q_\phi,\ \pi_\theta,\ \text{target network } Q_{\phi^-} \\ \text{Initialize replay buffer } \mathcal{D} \\ \textbf{repeat} \\ \quad a \sim \pi_\theta(\cdot \mid s) \\ \quad \text{Take action } a \text{ and observe } (r,s') \\ \quad \mathcal{D} \leftarrow \mathcal{D} \cup \{(s,a,r,s')\} \\ \quad \textbf{for each sampled minibatch } B \subset \mathcal{D} \textbf{ do} \\ \quad\quad a_i' \sim \pi_\theta(\cdot \mid s_i') \\ \quad\quad y_i \leftarrow r_i + \gamma \left[ Q_{\phi^-}(s_i',a_i') - \beta \log \pi_\theta(a_i' \mid s_i') \right] \\ \quad\quad \phi \leftarrow \phi - \alpha_Q \nabla_\phi \frac{1}{|B|} \sum_{i \in B} \frac{1}{2} \left( Q_\phi(s_i,a_i) - \operatorname{sg}[y_i] \right)^2 \\ \quad\quad \tilde a_i \leftarrow f_\theta(\xi_i;s_i), \qquad \xi_i \sim \mathcal{N}(0,I) \\ \quad\quad \theta \leftarrow \theta - \alpha_\pi \nabla_\theta \frac{1}{|B|} \sum_{i \in B} \left[ \beta \log \pi_\theta(\tilde a_i \mid s_i) - Q_\phi(s_i,\tilde a_i) \right] \\ \quad\quad \phi^- \leftarrow \tau \phi^- + (1-\tau)\phi \\ \quad \textbf{end for} \\ \textbf{until convergence} \\ \hline \end{array}

The SAC update we derived has a form very similar to DDPG:

TargetDDPG:yi=ri+γQϕ(si,μθ(si))SAC:yi=ri+γ[Qϕ(si,ai)βlogπθ(aisi)],ai=fθ(ξi;si),ξiN(0,I)ActionDDPG:ai=μθ(si)SAC:ai=fθ(ξi;si),ξiN(0,I)ActorDDPG:θθαπθ1Bi[Qϕ(si,μθ(si))]SAC:θθαπθ1Bi[βlogπθ(aisi)Qϕ(si,ai)]\begin{alignedat}{3} &\textit{Target} \qquad &&\text{DDPG:} \qquad && y_i = r_i + \gamma Q_{\phi^-}(s_i',\mu_{\theta^-}(s_i')) \\ & &&\text{SAC:} && y_i = r_i + \gamma \left[ Q_{\phi^-}(s_i',a_i') - \beta \log \pi_\theta(a_i'\mid s_i') \right], \quad a_i'=f_\theta(\xi_i';s_i'), \quad \xi_i'\sim\mathcal{N}(0,I) \\[2mm] &\textit{Action} &&\text{DDPG:} && a_i=\mu_\theta(s_i) \\ & &&\text{SAC:} && a_i=f_\theta(\xi_i;s_i), \quad \xi_i\sim\mathcal{N}(0,I) \\[2mm] &\textit{Actor} &&\text{DDPG:} && \theta \leftarrow \theta - \alpha_\pi \nabla_\theta \frac{1}{|B|} \sum_i \left[ - Q_\phi(s_i,\mu_\theta(s_i)) \right] \\ & &&\text{SAC:} && \theta \leftarrow \theta - \alpha_\pi \nabla_\theta \frac{1}{|B|} \sum_i \left[ \beta \log \pi_\theta(a_i\mid s_i) - Q_\phi(s_i,a_i) \right] \end{alignedat}

When β=0\beta=0, the SAC actor update is almost identical to the DDPG actor update. So the same overestimation issue can appear here as well. Recall that TD3 addressed this by using clipped double Q-learning. Practical SAC also uses clipped double Q-learning:

yi=ri+γ[minj=1,2Qϕj(si,ai)βlogπθ(aisi)],ai=fθ(ξi;si).y_i = r_i + \gamma \left[ \min_{j=1,2} Q_{\phi_j^-}(s_i',a_i') - \beta \log \pi_\theta(a_i'\mid s_i') \right], \qquad a_i'=f_\theta(\xi_i';s_i'). θθαπθ1Bi[βlogπθ(aisi)minj=1,2Qϕj(si,ai)],ai=fθ(ξi;si).\theta \leftarrow \theta - \alpha_\pi \nabla_\theta \frac{1}{|B|} \sum_i \left[ \beta \log \pi_\theta(a_i\mid s_i) - \min_{j=1,2} Q_{\phi_j}(s_i,a_i) \right], \qquad a_i=f_\theta(\xi_i;s_i).

The authors note that SAC can still learn hard tasks with a single Q-function, but two soft Q-functions significantly speed up training, especially on harder tasks.


This is the end of the third post. We started from the Bellman optimality equation, extended Q-learning to continuous actions, and then studied maximum entropy RL and SAC. For now, this is the end of the series, although I may add another post on offline or model-based reinforcement learning in the future.

References

Footnotes

  1. Volodymyr Mnih, Koray Kavukcuoglu, David Silver, Andrei A. Rusu, Joel Veness, Marc G. Bellemare, Alex Graves, Martin Riedmiller, Andreas K. Fidjeland, Georg Ostrovski, Stig Petersen, Charles Beattie, Amir Sadik, Ioannis Antonoglou, Helen King, Dharshan Kumaran, Daan Wierstra, Shane Legg, and Demis Hassabis, “Human-level control through deep reinforcement learning”, Nature, 2015. 2 3

  2. Leemon Baird, “Residual Algorithms: Reinforcement Learning with Function Approximation”, ICML, 1995.

  3. Shuyu Yin, Tao Luo, Peilin Liu, and Zhi-Qin John Xu, “An Experimental Comparison Between Temporal Difference and Residual Gradient with Neural Network Approximation”, arXiv, 2022.

  4. Shangtong Zhang, Wendelin Boehmer, and Shimon Whiteson, “Deep Residual Reinforcement Learning”, AAMAS, 2020.

  5. Meire Fortunato, Mohammad Gheshlaghi Azar, Bilal Piot, Jacob Menick, Ian Osband, Alex Graves, Vlad Mnih, Rémi Munos, Demis Hassabis, Olivier Pietquin, Charles Blundell, and Shane Legg, “Noisy Networks for Exploration”, ICLR, 2018.

  6. Hado van Hasselt, Arthur Guez, and David Silver, “Deep Reinforcement Learning with Double Q-learning”, AAAI, 2016.

  7. Hado van Hasselt, “Double Q-learning”, NeurIPS, 2010.

  8. Dmitry Kalashnikov, Alex Irpan, Peter Pastor, Julian Ibarz, Alexander Herzog, Eric Jang, Deirdre Quillen, Ethan Holly, Mrinal Kalakrishnan, Vincent Vanhoucke, and Sergey Levine, “QT-Opt: Scalable Deep Reinforcement Learning for Vision-Based Robotic Manipulation”, CoRL, 2018.

  9. Shixiang Gu, Timothy Lillicrap, Ilya Sutskever, and Sergey Levine, “Continuous Deep Q-Learning with Model-Based Acceleration”, ICML, 2016.

  10. Timothy P. Lillicrap, Jonathan J. Hunt, Alexander Pritzel, Nicolas Heess, Tom Erez, Yuval Tassa, David Silver, and Daan Wierstra, “Continuous Control with Deep Reinforcement Learning”, ICLR, 2016.

  11. David Silver, Guy Lever, Nicolas Heess, Thomas Degris, Daan Wierstra, and Martin Riedmiller, “Deterministic Policy Gradient Algorithms”, ICML, 2014.

  12. Scott Fujimoto, Herke van Hoof, and David Meger, “Addressing Function Approximation Error in Actor-Critic Methods”, ICML, 2018.

  13. Tuomas Haarnoja, Haoran Tang, Pieter Abbeel, and Sergey Levine, “Reinforcement Learning with Deep Energy-Based Policies”, ICML, 2017. 2 3

  14. Tuomas Haarnoja, Aurick Zhou, Pieter Abbeel, and Sergey Levine, “Soft Actor-Critic: Off-Policy Maximum Entropy Deep Reinforcement Learning with a Stochastic Actor”, ICML, 2018. 2

  15. Tuomas Haarnoja, Aurick Zhou, Kristian Hartikainen, George Tucker, Sehoon Ha, Jie Tan, Vikash Kumar, Henry Zhu, Abhishek Gupta, Pieter Abbeel, and Sergey Levine, “Soft Actor-Critic Algorithms and Applications”, arXiv, 2018.

Comments