Policy Gradients
We can run a policy, observe a trajectory, and calculate its return. To improve the policy, we’d like to differentiate that return with respect to the network’s weights. But the environment may give us no usable derivative. A cart-pole simulator accepts a discrete push direction. A code evaluator runs a program and reports whether it passed. Neither supplies the chain of derivatives we used in supervised learning.
The policy still gives us something we can differentiate: the probability of each action it takes. Changing those probabilities changes which trajectories we are likely to observe. A policy gradient uses this dependence to improve expected return from sampled experience.
We’ll differentiate an expectation through its probabilities, apply the result to trajectories, and turn the resulting gradient into a learning algorithm.
Expected Return Gradient
Let \(\mathcal A\) be a fixed finite set of actions. A policy network with parameters \(\boldsymbol\theta\in\mathbb R^p\) assigns a probability \(\pi_{\boldsymbol\theta}(a\mid s)\) to action \(a\) in state \(s\). We sample \(a_t\sim\pi_{\boldsymbol\theta}(\cdot\mid s_t)\). A small change to a probability does not give an ordinary derivative of this discrete sampled action, so we need to differentiate the expected outcome instead.
For this derivation, fix an integer horizon \(T\geq1\) and consider trajectories
\[ \tau=(s_0,a_0,r_0,\ldots,s_{T-1},a_{T-1},r_{T-1},s_T). \]
If an episode terminates sooner, extend it to \(T\) decisions with an absorbing terminal state and zero rewards after termination. For \(0\leq\gamma\leq1\), define the trajectory return and objective by \[ G(\tau)=\sum_{t=0}^{T-1}\gamma^t r_t, \qquad J(\boldsymbol\theta) =\mathbb E_{\tau\sim p_{\boldsymbol\theta}}[G(\tau)]. \] The parameters affect \(J\) by changing the distribution \(p_{\boldsymbol\theta}\) over trajectories. For a fixed trajectory, its observed rewards and return stay fixed. The parameters change how likely that trajectory is. This is the dependence we will differentiate. The following identity applies to any expectation with the same structure.
Claim: Let \(p_{\boldsymbol\theta}\) be a differentiable probability mass function or density whose support does not depend on \(\boldsymbol\theta\), and let \(X\) have this distribution. If \(f(x)\) has no additional dependence on \(\boldsymbol\theta\) and differentiation may pass through the sum or integral, then \[ \nabla_{\boldsymbol\theta}\mathbb E[f(X)] =\mathbb E\!\left[ f(X)\nabla_{\boldsymbol\theta}\log p_{\boldsymbol\theta}(X) \right]. \]
Proof
For a discrete random variable, let \(\mathcal X=\{x:p_{\boldsymbol\theta}(x)>0\}\) denote its fixed support. Then \[ \begin{aligned} \nabla_{\boldsymbol\theta}\mathbb E[f(X)] &=\nabla_{\boldsymbol\theta}\sum_{x\in\mathcal X} p_{\boldsymbol\theta}(x)f(x)\\ &=\sum_{x\in\mathcal X} f(x)\nabla_{\boldsymbol\theta}p_{\boldsymbol\theta}(x)\\ &=\sum_{x\in\mathcal X} p_{\boldsymbol\theta}(x)f(x) \frac{\nabla_{\boldsymbol\theta}p_{\boldsymbol\theta}(x)} {p_{\boldsymbol\theta}(x)}\\ &=\mathbb E\!\left[ f(X)\nabla_{\boldsymbol\theta}\log p_{\boldsymbol\theta}(X) \right]. \end{aligned} \] The density case replaces the sum by an integral.
The key step is multiplying and dividing by the same probability. That turns a sum of derivatives into an expectation we can estimate by sampling. The factor \(f(X)\) evaluates the sampled outcome, and \(\nabla_{\boldsymbol\theta}\log p_{\boldsymbol\theta}(X)\) describes how its log-probability changes.
The evaluation \(f(x)\) can come from a black box. We only need derivatives of the probabilities, and we need to sample from the distribution whose parameters we are updating.
Trajectory Probabilities
A trajectory is generated one decision at a time. First the environment supplies an initial state, then the policy chooses an action, and then the environment supplies a reward and next state. The probability chain rule multiplies these factors.
Write \(P(s_0)\) for the initial-state probability and \(P(r_t,s_{t+1}\mid s_t,a_t)\) for the conditional reward and next-state probability. Use densities when the relevant laws admit them. To write the factorization concretely, assume these probabilities or densities exist on a fixed support. Then \[ p_{\boldsymbol\theta}(\tau) =P(s_0)\prod_{t=0}^{T-1} \pi_{\boldsymbol\theta}(a_t\mid s_t) P(r_t,s_{t+1}\mid s_t,a_t). \]
For deterministic dynamics, conditioning on the initial state makes the trajectory a function of the sampled actions. Its probability is then the product of the policy’s action probabilities. Differentiating leaves only these policy factors, so the same argument applies even without an ordinary density over continuous trajectories.
fixed distribution
depend on θ
fixed conditional laws
Claim: If the initial-state and environment laws do not depend on \(\boldsymbol\theta\), and the log-derivative identity’s assumptions hold for the trajectory distribution, then \[ \nabla_{\boldsymbol\theta}J =\mathbb E\!\left[ G(\tau)\sum_{t=0}^{T-1}\nabla_{\boldsymbol\theta} \log\pi_{\boldsymbol\theta}(a_t\mid s_t) \right]. \]
Proof
Apply the log-derivative identity to the trajectory: \[ \nabla_{\boldsymbol\theta}J =\mathbb E\!\left[ G(\tau)\nabla_{\boldsymbol\theta}\log p_{\boldsymbol\theta}(\tau) \right]. \] Taking the logarithm of the factorization gives \[ \begin{aligned} \log p_{\boldsymbol\theta}(\tau) &=\log P(s_0) +\sum_{t=0}^{T-1}\log\pi_{\boldsymbol\theta}(a_t\mid s_t)\\ &\quad+\sum_{t=0}^{T-1}\log P(r_t,s_{t+1}\mid s_t,a_t). \end{aligned} \] Here we differentiate while holding the sampled trajectory fixed. The states were produced by the policy during sampling, but in this likelihood calculation they are the observed arguments. The initial-state and environment factors have no direct dependence on the policy parameters, so \[ \nabla_{\boldsymbol\theta}\log p_{\boldsymbol\theta}(\tau) =\sum_{t=0}^{T-1}\nabla_{\boldsymbol\theta} \log\pi_{\boldsymbol\theta}(a_t\mid s_t). \] Substitution proves the claim.
The environment factors disappear from the log derivative because their conditional laws are fixed. We therefore do not need to know their numerical probabilities to compute the gradient estimate. We need their sampled outcomes and the action probabilities supplied by our policy.
The change in the distribution of visited states has not been ignored. It is included in the trajectory distribution and its policy factors. We have avoided differentiating a sampled state through the environment.
REINFORCE
For \(N\geq1\) independent trajectories \(\tau_1,\ldots,\tau_N\) sampled from the current policy, write \(s_{i,t}\) and \(a_{i,t}\) for the state and action at time \(t\in\{0,\ldots,T-1\}\) in trajectory \(i\in\{1,\ldots,N\}\). The policy-gradient identity gives the estimator \[ \widehat{\nabla J} =\frac1N\sum_{i=1}^N G(\tau_i) \sum_{t=0}^{T-1}\nabla_{\boldsymbol\theta} \log\pi_{\boldsymbol\theta}(a_{i,t}\mid s_{i,t}). \] Each sampled term has the expectation in our policy-gradient identity, so \(\mathbb E[\widehat{\nabla J}]=\nabla_{\boldsymbol\theta}J\). As with earlier Monte Carlo estimators, an individual batch can differ substantially from that expectation. Gradient ascent updates \[ \boldsymbol\theta \leftarrow \boldsymbol\theta+\alpha\widehat{\nabla J}. \] This sample, evaluate, and update procedure is REINFORCE. We use ascent because the objective is a reward to maximize. To implement it with software that minimizes a loss, hold the sampled returns fixed and minimize
\[ \mathcal L_{\mathrm{policy}}=-\frac1N\sum_{i=1}^N G(\tau_i)\sum_{t=0}^{T-1}\log\pi_{\boldsymbol\theta}(a_{i,t}\mid s_{i,t}). \]
Its gradient is the negative of our estimate. Backpropagation runs through the policy network, with the sampled states and actions treated as data.
To interpret the update, suppose the policy produces one logit \(z_a(s)\) per action and uses softmax:
\[ \pi_{\boldsymbol\theta}(a\mid s)=\frac{e^{z_a(s)}}{\sum_{j\in\mathcal A}e^{z_j(s)}}. \]
The probability vector \(\mathbf p_t\) and one-hot sampled action \(\mathbf y_t\) each have \(|\mathcal A|\) coordinates, one per action, matching the logit vector \(\mathbf z\). Then \[ \nabla_{\mathbf z}\log\pi_{\boldsymbol\theta}(a_t\mid s_t) =\mathbf y_t-\mathbf p_t. \] This is the negative of the cross-entropy gradient from logistic regression. For a direct step in these logits, multiplying by a positive return increases the sampled action’s probability. A negative return decreases it. A policy network passes this score gradient through its shared weights, so contributions from different decisions can compete. The sign is the opposite of the loss gradient because we maximize expected return.
The explorer takes one step directly in a fixed vector of action logits.
A softmax action score is one-hot action minus probability. A positive return increases the selected action’s probability in a direct logit ascent step. A negative return reverses that step.
The sampled return scales the correction. This resembles the pre-training update in the TL;DR reading, where we increase the probability of an observed target. Here the action comes from the policy itself, and the observed return determines the sign and size of its contribution. A high reward supplies useful feedback only after the policy has sampled the corresponding behavior.
Reward to Go
The estimator above uses the full trajectory return at every decision. That includes rewards earned before the action was taken. Those earlier rewards can change the sampled update, even though the current action could not have caused them.
We can remove these terms without changing the expected gradient. The reason is that the gradient of a policy’s log-probability averages to zero over its own actions. We will use this same fact again when introducing baselines.
Claim: For the fixed horizon \(T\), define the return from time \(t\in\{0,\ldots,T\}\) by \[ G_t=\sum_{k=0}^{T-t-1}\gamma^k r_{t+k}. \] An empty sum is zero, so \(G_T=0\). For \(J=\mathbb E[G_0]\), the gradient is \[ \nabla_{\boldsymbol\theta}J =\mathbb E\!\left[ \sum_{t=0}^{T-1}\gamma^tG_t \nabla_{\boldsymbol\theta}\log \pi_{\boldsymbol\theta}(a_t\mid s_t) \right]. \]
Proof
For \(t=0,\ldots,T-1\), write \[ \mathbf u_t =\nabla_{\boldsymbol\theta}\log \pi_{\boldsymbol\theta}(a_t\mid s_t). \] The history contains everything observed before choosing \(a_t\):
\[ \mathcal H_t=(s_0,a_0,r_0,\ldots,s_{t-1},a_{t-1},r_{t-1},s_t),\qquad \mathcal H_0=(s_0). \]
Conditional on this history, \[ \begin{aligned} \mathbb E[\mathbf u_t\mid\mathcal H_t] &=\sum_{a\in\mathcal A}\pi_{\boldsymbol\theta}(a\mid s_t) \nabla_{\boldsymbol\theta}\log \pi_{\boldsymbol\theta}(a\mid s_t)\\ &=\sum_{a\in\mathcal A}\nabla_{\boldsymbol\theta} \pi_{\boldsymbol\theta}(a\mid s_t)\\ &=\nabla_{\boldsymbol\theta}1 =\mathbf0. \end{aligned} \] The earlier discounted reward \[ B_t=\sum_{k=0}^{t-1}\gamma^k r_k \] is fixed given \(\mathcal H_t\) and equals zero at \(t=0\) by the empty-sum convention, so \[ \mathbb E[B_t\mathbf u_t] =\mathbb E\!\left[ B_t\mathbb E[\mathbf u_t\mid\mathcal H_t] \right] =\mathbf0. \] Because \(G_0=B_t+\gamma^tG_t\), \[ \mathbb E[G_0\mathbf u_t] =\mathbb E[\gamma^tG_t\mathbf u_t]. \] Replacing each term in the trajectory policy gradient proves the result.
The outer factor \(\gamma^t\) matters: \(G_t\) discounts from the current decision, while our objective \(J=\mathbb E[G_0]\) discounts from the start. Multiplying by \(\gamma^t\) puts each remaining reward back on that original scale. For \(\gamma=1\), the factor is one.
REINFORCE is unbiased under the stated assumptions, but its estimates can be noisy. A single trajectory combines random actions, transitions, and rewards. Even after removing past rewards, the update multiplies a random future return by a random score vector.
Adding a constant to every return leaves the exact gradient unchanged because the expected policy score is zero. It can nevertheless increase the variance of sampled gradients.
We can use the same control-variate idea we saw in Monte Carlo estimation. Subtract an action-independent baseline \(b(s_t)\) from the return. The expected policy score is zero conditional on \(s_t\), so
\[ \mathbb E[b(s_t)\mathbf u_t\mid s_t]=b(s_t)\mathbb E[\mathbf u_t\mid s_t]=\mathbf0. \]
Replacing \(G_t\) by \(G_t-b(s_t)\) therefore leaves the expected update unchanged and can reduce its variance. A common choice is the value function, the expected return from the current state under the policy. Subtracting it compares the sampled return with what we expected from that state. This centered return estimates the action’s advantage, or how much better it is than the policy’s usual outcome there. The baseline is treated as fixed when computing the policy update; its purpose is to reduce noise in that update.
Sampled rewards tell us which trajectories to favor, and the policy’s log-probability gradients tell us how to change their likelihood. Together they give an update even when the environment supplies no derivative.