Equivalence Between Policy Gradients and Soft Q-Learning
John Schulman, Xi Chen, Pieter Abbeel
Introduction
Policy gradient methods (PG) and -learning (QL) methods perform updates that are qualitatively similar. In both cases, if the return following an action is high, then that action is reinforced: in policy gradient methods, the probability is increased; whereas in -learning methods, the -value is increased. The connection becomes closer when we add entropy regularization to these algorithms. With an entropy cost added to the returns, the optimal policy has the form ; hence policy gradient methods solve for the optimal -function, up to an additive constant (Ziebart (2010)). O’Donoghue et al. (2016) also discuss the connection between the fixed points and updates of PG and QL methods, though the discussion of fixed points is restricted to the tabular setting, and the discussion comparing updates is informal and shows an approximate equivalence. Going beyond past work, this paper shows that under appropriate conditions, the gradient of the loss function used in -step -learning is equal to the gradient of the loss used in an -step policy gradient method, including a squared-error term on the value function. Altogether, the update matches what is typically done in “actor-critic” policy gradient methods such as A3C, which explains why Mnih et al. (2016) obtained qualitatively similar results from policy gradients and -step -learning.
Section 2 uses the bandit setting to provide the reader with a simplified version of our main calculation. (The main calculation applies to the MDP setting.) Section 3 discusses the entropy-regularized formulation of RL, which is not original to this work, but is included for the reader’s convenience. Section 4 shows that the soft -learning loss gradient can be interpreted as a policy gradient term plus a baseline-error-gradient term, corresponding to policy gradient instantiations such as A3C (Mnih et al., 2016). Section 5 draws a connection between QL methods that use batch updates or replay-buffers, and natural policy gradient methods.
Some previous work on entropy regularized reinforcement learning (e.g., O’Donoghue et al. (2016); Nachum et al. (2017)) uses entropy bonuses, whereas we use a penalty on Kullback-Leibler (KL) divergence, which is a bit more general. However, in the text, we often refer to “entropy” terms; this refers to “relative entropy”, i.e., the KL divergence.
Bandit Setting
where is some “reference” policy, is a “temperature” parameter, and is the Kullback-Leibler divergence. Note that the temperature can be eliminated by rescaling the rewards. However, we will leave it so that our calculations are checkable through dimensional analysis, and to make the temperature-dependence more explicit.
First, let us calculate the policy that maximizes . We claim that is maximized by , defined as
To derive this, consider the KL divergence between and :
The preceding calculation gives us the optimal policy when is known, but in the entropy-regularized bandit problem, it is initially unknown, and the agent learns about it by sampling. There are two approaches for solving the entropy-regularized bandit problem:
A direct, policy-based approach, where we incrementally update the agent’s policy based on stochastic gradient ascent on .
An indirect, value-based approach, where we learn an action-value function that estimates and approximates , and we define based on our current estimate of .
For the policy-based approach, we can obtain unbiased estimates the gradient of . For a parameterized policy , the gradient is given by
We can obtain an unbiased gradient estimate using a single sample .
In the indirect, value-based approach approach, it is natural to use a squared-error loss:
Taking the gradient of this loss, with respect to the parameters of , we get
Soon, we will calculate the relationship between this loss gradient and the policy gradient from Equation 7.
In the indirect, value-based approach, a natural choice for policy is the one that would be optimal if . Let’s denote this policy, called the Boltzmann policy, by , where
It will be convenient to introduce a bit of notation for the normalizing factor; namely, we define the scalar
Then the Boltzmann policy can be written as
Hence, is an estimate of , plugging in for .
Now we shall show the connection between the gradient of the squared-error loss (Equation 9) and the policy gradient (Equation 7). Rearranging Equation 12, we can write in terms of and the Boltzmann policy :
Let’s substitute this expression for into the squared-error loss gradient (Equation 9).
Note that we have not yet decided on a sampling distribution . Henceforth, we’ll assume actions were sampled by . Also, note the derivative of the KL-divergence:
Continuing from Equation 17 but setting ,
Soon we will derive an equivalent interpretation of -function regression in the MDP setting, where we are approximating the state-value function . However, we first need to introduce an entropy-regularized version of the reinforcement learning problem.
Entropy-Regularized Reinforcement Learning
We are obliged to alter our definitions of value functions to include the new KL penalty terms. We shall define the state-value function as the expected return:
Note that this -function does not include the first KL penalty term, which does not depend on the action . This definition makes some later expressions simpler, and it leads to the following relationship between and :
which follows from matching terms in the sums in Equations 24 and 25.
2 Boltzmann Policy
In standard reinforcement learning, the “greedy policy” for is defined as . With entropy regularization, we need to alter our notion of a greedy policy, as the optimal policy is stochastic. Since omits the first entropy term, it is natural to define the following stochastic policy, which is called the Boltzmann policy, and is analogous to the greedy policy:
where the second equation is analogous to Equation 2 from the bandit setting.
Also analogously to the bandit setting, it is natural to define (a function of ) as
Under this definition, it also holds that
in analogy with Equation 13. Hence, can be interpreted as an estimate of the expected entropy-augmented return, under the Boltzmann policy .
Another way to interpret the Boltzmann policy is as the exponentiated advantage function. Defining the advantage function as , Equation 30 implies that .
3 Fixed-Policy Backup Operators
The operators (for and ) in standard reinforcement learning correspond to computing the expected return with a one-step lookahead: they take the expectation over one step of dynamics, and then fall back on the value function at the next timestep. We can easily generalize these operators to the entropy-regularized setting. We define
Repeatedly applying the operator (\mathcal{T}_{\pi}^{n}V=\underbrace{\mathcal{T}_{\pi}(\mathcal{T}_{\pi}(\dots\mathcal{T}_{\pi}}_{\text{ntimes}}(V)))) corresponds to computing the expected return with a multi-step lookahead. That is, repeatedly expanding the definition of , we obtain
As a sanity check, note that in both equations, the left-hand side and right-hand side correspond to estimates of the total discounted return .
The right-hand side of these backup formulas can be rewritten using “Bellman error” terms . To rewrite the state-value () backup, define
4 Boltzmann Backups
We can define another set of backup operators corresponding to the Boltzmann policy, . We define the following Boltzmann backup operator:
where the simplification from to follows from the same calculation that we performed in the bandit setting (Equations 11 and 13).
The -step operator for -functions also simplifies in the case that we are executing the Boltzmann policy. Starting with the equation for (Equation 35) and setting , and then using Equation 31 to rewrite the expected -function terms in terms of , we obtain
From now on, let’s denote this -step backup operator by . (Note , even though , because depends on .)
One can similarly define the TD() version of this backup operator
One can straightforwardly verify by comparing terms that it satisfies
5 Soft Q𝑄Q-Learning
The Boltzmann backup operators defined in the preceding section can be used to define practical variants of -learning that can be used with nonlinear function approximation. These methods, which optimize the entropy-augmented return, will be called soft -learning. Following Mnih et al. (2015), modern implementations of -learning, and -step -learning (see Mnih et al. (2016)) update the -function incrementally to compute the backup against a fixed target -function, which we’ll call . In the interval between each target network update, the algorithm is approximately performing the backup operation (-step) or (-step). To perform this approximate minimization, the algorithms minimize the least squares loss
In one-step -learning (Equation 45), is an unbiased estimator of , regardless of what behavior policy was used to collect the data. In -step -learning (Equation 46), for , is only an unbiased estimator of if actions are sampled using .
6 Policy Gradients
Entropy regularization is often used in policy gradient algorithms, with gradient estimators of the form
However, these are not proper estimators of the entropy-augmented return , since they don’t account for how actions affect entropy at future timesteps. Intuitively, one can think of the KL terms as a cost for “mental effort”. Equation 48 only accounts for the instantaneous effect of actions on mental effort, not delayed effects.
To compute proper gradient estimators, we need to include the entropy terms in the return. We will define the discounted policy gradient in the following two equivalent ways—first, in terms of the empirical return; second, in terms of the value functions and :
In the special case of a finite-horizon problem—i.e., for all —the undiscounted () return is finite, and it is meaningful to compute its gradient. In this case, equals the undiscounted policy gradient:
Since computes the gradient of the entropy-regularized return, one interpretation of is that it is an approximation of the undiscounted policy gradient , but that it allows for lower-variance gradient estimators by ignoring some long-term dependencies. A different interpretation of is that it gives a gradient flow such that is the (possibly unique) fixed point.
As in the standard MDP setting, one can define approximations to that use a value function to truncate the returns for variance reduction. These approximations can take the form of -step methods (Mnih et al. (2016)) or TD()-like methods (Schulman et al. (2015b)), though we will focus on -step returns here. Based on the definition of above, the natural choice of variance-reduced estimator is
where was defined in Equation 36.
The state-value function we use in the above formulas should approximate the entropy augmented return . We can fit iteratively by approximating the -step backup , by minimizing a squared-error loss
Soft Q𝑄Q-learning Gradient Equals Policy Gradient
This section shows that the gradient of the squared-error loss from soft -learning (Section 3.5) equals the policy gradient (in the family of policy gradients described in Section 3.6) plus the gradient of a squared-error term for fitting the value function. We will not make any assumption about the parameterization of the -function, but we define and as the following functions of the parameterized -function :
Here, is the Boltzmann policy for , and is the normalizing factor we described above. From these definitions, it follows that the -function can be written as
We will substitute this expression into the squared-error loss function. First, for convenience, let us define .
Now, let’s consider the gradient of the -step soft -learning objective:
Note that the equivalent policy gradient method multiplies the policy gradient by a factor of , relative to the value function error. Effectively, the value function error has a coefficient of , which is larger than what is typically used in practice (Mnih et al. (2016)). We will analyze this choice of coefficient in the experiments.
Soft Q𝑄Q-learning and Natural Policy Gradients
The previous section gave a first-order view on the equivalence between policy gradients and soft -learning; this section gives a second-order, coordinate-free view. As previous work has pointed out, the natural gradient is the solution to a regression problem; here we will explore the relation between that problem and the nonlinear regression in soft -learning.
Now let us interpret the least-squares problem in Equation 66. is the vector whose row is . According to the definition of the gradient, if we perform a parameter update with , the change in is as follows, to first order in :
Thus, we can interpret the least squares problem (Equation 66) as solving
That is, we are adjusting each log-probility by the advantage function , scaled by .
In entropy-regularized reinforcement learning, we have an additional term for the gradient of the KL-divergence:
Now let’s consider -learning. Let’s assume that the value function is unchanged by optimization, so . (Otherwise, the equivalence will not hold, since the value function will try to explain the measured advantage , shrinking the advantage update.)
Evidently, we are regressing towards . This loss is not equivalent to the natural policy gradient loss that we obtained above.
We can recover the natural policy gradient by instead solving a damped version of the -function regression problem. Define , i.e., we are interpolating between the old value and the backed-up value.
which exactly matches the expression in the least squares problem in Equation 71, corresponding to entropy-regularized natural policy gradient. Hence, the “damped” -learning update corresponds to a natural gradient step.
Experiments
To complement our theoretical analyses, we designed experiments to study the following questions:
Though one-step entropy bonuses are used in PG methods for neural network policies (Williams (1992); Mnih et al. (2016)), how do the entropy-regularized RL versions of policy gradients and -learning described in Section 3 perform on challenging RL benchmark problems? How does the “proper” entropy-regularized policy gradient method (with entropy in the returns) compare to the naive one (with one-step entropy bonus)? (Section 6.1)
How do the entropy-regularized versions of -learning (with logsumexp) compare to the standard DQN of Mnih et al. (2015)? (Section 6.2)
The equivalence between PG and soft -learning is established in expectation, however, the actual gradient estimators are slightly different due to sampling. Furthermore, soft -learning is equivalent to PG with a particular penalty coefficient on the value function error. Does the equivalence hold under practical conditions? (Section 6.3)
Here we investigated whether there is an empirical effect of including entropy terms when computing returns, as described in Section 3. In this section, we compare the naive and proper policy gradient estimators:
In the experiments on Atari, we take to be the uniform distribution, which gives a standard entropy bonus up to a constant.
We start with a well-tuned (synchronous, deterministic) version of A3C (Mnih et al. (2016)), henceforth called A2C (advantage actor critic), to optimize the entropy-regularized return. We use the parameter and train for million frames. We did not tune any hyperparameters for the “proper” algorithm—we used the same hyperparameters that had been tuned for the “naive” algorithm.
As shown in Figure 1, the “proper” version yields performance that is the same or possibly greater than the “naive” version. Hence, besides being attractive theoretically, the entropy-regularized formulation could lead to practical performance gains.
2 DQN on Atari: Standard vs Soft
Here we investigated whether soft -learning (which optimizes the entropy-augmented return) performs differently from standard “hard” -learning on Atari. We made a one-line change to a DQN implementation:
The difference between the entropy bonus and KL penalty (against uniform) is simply a constant, however, this constant made a big difference in the experiments, since a positive constant added to the reward encourages longer episodes. Note that we use the same epsilon-greedy exploration in all conditions; the only difference is the backup equation used for computing and defining the loss function.
The results of two runs on each game are shown in Figure 2. The entropy-bonus version with seems to perform a bit better than standard DQN, however, the KL-bonus version performs worse, so the benefit may be due to the effect of adding a small constant to the reward. We have also shown the results for -step -learning, where the algorithm is otherwise the same. The performance is better on Pong and -bert but worse on other games—this is the same pattern of performance found with -step policy gradients. (E.g., see the A2C results in the preceding section.)
3 Entropy Regularized PG vs Online Q𝑄Q-Learning on Atari
Next we investigate if the equivalence between soft -learning and PG is relevant in practice—we showed above that the gradients are the same in expectation, but their variance might be different, causing different learning dynamics. For these experiments, we modified the gradient update rule used in A2C while making no changes to any algorithmic component, i.e. parallel rollouts, updating parameters every steps, etc. The -function was represented as: , which can be seen as a form of dueling architecture with being the “advantage stream” (Wang et al. (2015)). are parametrized as the same neural network as A2C, where convolutional layers and the first fully connected layer are shared. is used as behavior policy.
A2C can be seen as optimizing a combination of a policy surrogate loss and a value function loss, weighted by hyperparameter :
In normal A2C, we have found to be a robust setting that works across multiple environments. On the other hand, our theory suggests that if we use this -function parametrization, soft -learning has the same expected gradient as entropy-regularized A2C with a specific weighting . Hence, for the usual entropy bonus coefficient setting , soft -learning is implicitly weighting value function loss a lot more than usual A2C setup ( versus ). We have found that such emphasis on value function () results in unstable learning for both soft -learning and entropy-regularized A2C. Therefore, to make -learning exactly match known good hyperparameters used in A2C, we scale gradients that go into advantage stream by and scale gradients that go into value function stream by .
With the same default A2C hyperparameters, learning curves of PG and QL are almost identical in most games (Figure 3), which indicates that the learning dynamics of both update rules are essentially the same even when the gradients are approximated with a small number of samples. Notably, the -learning method here demonstrates stable learning without the use of target network or schedule.
Related Work
Three recent papers have drawn the connection between policy-based methods and value-based methods, which becomes close with entropy regularization.
O’Donoghue et al. (2016) begin with a similar motivation as the current paper: that a possible explanation for -learning and SARSA is that their updates are similar to policy gradient updates. They decompose the -function into a policy part and a value part, inspired by dueling -networks (Wang et al. (2015)):
Nachum et al. (2017) also discuss the entropy-regularized reinforcement learning setting, and develop an off-policy method that applies in this setting. Their argument (modified to use our notation and KL penalty instead of entropy bonus) is as follows. The advantage function lets us define a multi-step consistency equation, which holds even if the actions were sampled from a different (suboptimal) policy. In the setting of deterministic dynamics, , hence
If is the optimal policy (for the discounted, entropy-augmented return), then it is the Boltzmann policy for , thus
This expression for the advantage can be substituted into Equation 88, giving the consistency equation
which holds when is optimal. The authors define a squared error objective formed from by taking LHS - RHS in Equation 90, and jointly minimize it with respect to the parameters of and . The resulting algorithm is a kind of Bellman residual minimization—it optimizes with respect to the future target values, rather than treating them as fixed Scherrer (2010).
Haarnoja et al. (2017) work in the same setting of soft -learning as the current paper, and they are concerned with tasks with high-dimensional action spaces, where we would like to learn stochastic policies that are multi-modal, and we would like to use -functions for which there is no closed-form way of sampling from the Boltzmann distribution . Hence, they use a method called Stein Variational Gradient Descent to derive a procedure that jointly updates the -function and a policy , which approximately samples from the Boltzmann distribution—this resembles variational inference, where one makes use of an approximate posterior distribution.
Conclusion
We study the connection between two of the leading families of RL algorithms used with deep neural networks. In a framework of entropy-regularized RL we show that soft -learning is equivalent to a policy gradient method (with value function fitting) in terms of expected gradients (first-order view). In addition, we also analyze how a damped -learning method can be interpreted as implementing natural policy gradient (second-order view). Empirically, we show that the entropy regularized formulation considered in our theoretical analysis works in practice on the Atari RL benchmark, and that the equivalence holds in a practically relevant regime.
Acknowledgements
We would like to thank Matthieu Geist for pointing out an error in the first version of this manuscript, Chao Gao for pointing out several errors in the second version, and colleagues at OpenAI for insightful discussions.