Policy gradients (part 1): what is it?
Cross-posted to Substack.
I missed doing math. So I played with policy gradients, and I found a result that surprised me, but it turns out is equivalent to a generalized advantage estimator.
Policy gradients explainers are a perennial genre in machine learning, similar to burrito monad tutorials in the Haskell world. I’m excited to add my take to the literature.
Problem setting for RL
The policy gradients setting is as follows. You have a neural network that is acting in an environment. Every time-step it observes the current state and takes an action. Then the environment changes, and the neural network receives a corresponding reward. You want to optimize the trajectories coming out of this neural network so they get as much reward as possible. The neural network is a ‘policy’, which is a function of the current state that outputs a probability distribution over actions.
We’ll call the neural network weights θ, and the policy π_θ, which is a distribution over the next action a_{t+1} which depends on all the previous states x_{1:t}. In LLMs, the action is just a new token, so we’ll call it x_t just like the states. To optimize the trajectories, we want to take gradient steps in the direction of increasing reward.
The policy gradients theorem
The objective we want to maximize is the expectation over trajectories of length T (x_{1:T}), of the reward obtained in that trajectory. We express this objective as the gradient (∇_θ) of the expectation, as follows:
This is a weird gradient to take, because the value inside the expectation does not depend on our parameters θ by itself. Instead, it only acquires this dependency because we’re taking the average reward over trajectories, and we can change the distribution of trajectories by changing π_θ.
In other words, the objective only depends on θ through the expectation. We’d like to estimate the gradient by sampling trajectories, because computing the exact expectation is prohibitive. Usually we would remove the dependence on θ from the expectation distribution, using the reparameterization trick. We cannot do this because the distribution we’re sampling is discrete.[1]
The policy gradients theorem rewrites the gradient of the expected sum of rewards, as an expectation over trajectories of things we can compute.
This is a much more convenient form, because we can just sample trajectories, compute the gradient of the states over the trajectory and the reward, and get an estimate of the gradient that we can follow.
The derivation uses two insights:
-
You can exchange gradient operators and expectations/integrals, most of the time.
-
By the chain rule, the gradient of log(f(x)) is 1/x * the gradient of f(x). Therefore the gradient of f(x) is f(x)*log(f(x)):
You might see the resemblance between this equation and the right-hand side of the previous one.
Deriving policy gradients
Now for the full derivation. We name the sum of rewards G(x[1:T]), for convenience, then the full derivation starts by expanding the expectation:
then we exchange the integral and gradient operators
and use the identity above about grad(f)(x) = f(x) * grad(log f)(x):
and this is again an expectation over π_θ, so we can express it like that again. We get the identity back!
Then, in practice, we estimate this using samples from the policy. For each sample we compute G and the gradient of the log-probabilities of the actions actually taken, and average them.
Next time, we take a look at how to make this estimate have a lower variance.
Applying the reparameterization trick to discrete distributions can sort of be done using the Gumbel-Softmax trick. Now that I think of it, nobody has tried this for sparse autoencoders yet… ↩︎