All courses

Policy gradients (REINFORCE) and baselines

For a policy π that samples actions a and then receives a reward R, derive ∇E[R] = E[R ∇log π(a)] and turn it into a sampled update. Explain why subtracting a baseline from R reduces variance without adding bias.

Builds on: Gradient descent and the learning rate, From base model to assistant

▶ Watch this lesson Start the whole course

What the animation says

A gradient through a dice roll?

In reinforcement learning, the model rolls a die: it samples an action, say a token, from its policy. Then a black box scores it: a test suite, or a human rating. We want to climb the expected reward with gradient ascent. But you can't differentiate through 'pick option three', or through the grader. So where does a usable gradient come from?

Push up what you sampled

Here's the idea: try an action and look at its reward. Then nudge the parameters so that action becomes more likely, in proportion to how good it was. The gradient of the log probability is exactly that direction: it raises this action's chance. Two actions, A and B, start at fifty-fifty. A pays eleven, B pays nine. We sample B and step by nine times its arrow. What happens to B's probability?

The log-derivative trick

B went up. Every positive reward pushes its sample up. The math agrees. Expected reward is a sum over actions of probability times reward. The reward doesn't depend on the parameters, so the gradient lands only on the probability. Multiply and divide by the probability. Gradient of pi over pi is the gradient of log pi. So the sum becomes an average over samples: reward times the gradient of log probability. In practice: sample a few actions and step along that average, scaled by the learning rate.

Subtract a baseline, for free

Only relative quality matters, so subtract a baseline: a typical reward. Each sample is now scaled by reward minus baseline. Below the baseline, that factor is negative, and the action is pushed down. Does this bias the average? The extra term is b times the sum of the gradients of the probabilities. That's b times the gradient of one, which is zero. But that only works if b can be pulled out of the sum. Which of these may the baseline depend on?

Two actions, one parameter

Right: anything fixed before sampling, like the prompt. Never the sampled action. Now see why it helps. One parameter, starting at zero: A and B are fifty-fifty. A's log-gradient is plus a half, B's is minus a half. The true gradient is a half. With no baseline, an A sample gives five and a half, a B sample gives minus four and a half, pointing the wrong way. Variance: twenty-five. Drag the baseline until every sample gives the true gradient.

How do you take a gradient through a dice roll?

In RL the model samples an action $a$ (say, a token) from its policy $\pi_\theta$, then gets a reward $R$ from a black box: a test suite or a human score. We want gradient ascent on the expected reward $\mathbb{E}[R]$. But you can't differentiate through "pick option 3" or through the grader. So where does a usable $\nabla_\theta \mathbb{E}[R]$ come from?

Make good samples more likely

Try an action and see its reward. Then nudge $\theta$ so that action becomes more likely, in proportion to how good it was. $\nabla\log\pi_\theta(a)$ is the direction in parameter space that raises the probability of $a$. Averaged over many tries, better actions get pushed up harder. One catch: if every reward is positive, every sample gets pushed up. Only *relative* quality matters, so compare each reward to a baseline ("typical").

The REINFORCE gradient

$$\nabla\mathbb{E}[R]=\nabla\sum_a\pi_\theta(a)R(a)=\sum_a\nabla\pi_\theta(a)\,R(a)=\sum_a\pi_\theta(a)\frac{\nabla\pi_\theta(a)}{\pi_\theta(a)}R(a)=\mathbb{E}_{a\sim\pi_\theta}\big[R\,\nabla\log\pi_\theta(a)\big]$$ $R(a)$ doesn't depend on $\theta$. The third step multiplies and divides by $\pi_\theta(a)$. That is the log-derivative trick $\nabla\log\pi=\nabla\pi/\pi$, and it turns the sum back into an average over samples. Sampled update: draw $a_1,\dots,a_n$ and step $$\theta\leftarrow\theta+\eta\,\tfrac1n\textstyle\sum_i (R_i-b)\,\nabla\log\pi_\theta(a_i)$$ The sign is $+$ because we maximize. $\eta$ is the learning rate and $b$ the baseline.

Why a baseline adds no bias

The baseline adds the extra term $$\mathbb{E}[b\,\nabla\log\pi_\theta(a)]=b\sum_a\nabla\pi_\theta(a)=b\,\nabla\!\sum_a\pi_\theta(a)=b\,\nabla 1=0.$$ So the average update is unchanged: it is still unbiased. Each sample is now scaled by $R-b$ instead of $R$. With $b\approx$ the typical reward, samples swing less, so the variance usually drops. A badly chosen $b$ can raise it. $b$ may depend on anything fixed before the action is sampled (for an LM, the prompt), but **not** on the sampled action. Otherwise it can't be pulled out of the sum, and the proof breaks.

Two actions, one parameter

$\pi(A)=\sigma(\theta)$, $\pi(B)=1-\sigma(\theta)$, $\theta=0$, so each has probability 0.5. $\nabla\log\pi(A)=1-\sigma=0.5$ and $\nabla\log\pi(B)=-\sigma=-0.5$. Rewards: $R(A)=11$, $R(B)=9$. True gradient: $2\sigma(1-\sigma)=0.5$. - $b=0$: A gives $11(0.5)=5.5$, B gives $9(-0.5)=-4.5$. The mean is $0.5$, but a B sample points the wrong way. The variance of the single-sample estimate is 25. - $b=10$: A gives $1(0.5)=0.5$, B gives $(-1)(-0.5)=0.5$. Every sample equals the true gradient (variance 0).

Remember this

$\nabla\mathbb{E}[R]=\mathbb{E}[R\,\nabla\log\pi_\theta(a)]$: sample actions, then raise each one's log-probability in proportion to its reward. Subtracting a baseline that doesn't depend on the action keeps the average unchanged, because $\mathbb{E}[\nabla\log\pi]=0$. It cuts the noise when $b$ is near the typical reward.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.