Skip to lesson
Exit
Deep Dive: RL for Coding Agents1 / 2

2 min lesson

The policy-gradient objective

Explain the order in "The policy-gradient objective", then say how you would verify the result.

Step 1 of 2

The policy-gradient objectiveREINFORCE

You want to maximize expected reward over sequences the policy generates. You can't differentiate through sampling, so the log-derivative trick rewrites the gradient as something you can estimate from samples.

REINFORCE: the gradient is a reward-weighted sum of log-probs
J(θ)   = E_{τ ~ π_θ} [ R(τ) ]            # expected reward over trajectories
∇J(θ) = E_{τ ~ π_θ} [ R(τ) · ∇ log π_θ(τ) ]

# In words: nudge each token's log-probability UP if the reward
# was high, DOWN if it was low. The reward does the supervising.

Read that gradient out loud in an interview: it is a reward-weighted log-likelihood. High-reward samples get reinforced, low-reward samples get suppressed and the magnitude scales with reward. The whole field is variations on making this estimator less noisy. Here is the whole loop in one picture; the next two subsections fill in the weighting and the leash.

THE CORE RL LOOP

Interactive diagram. Step through it with the Next and Previous controls below, or Tab to a region to read its detail.

diagram: flow

One pass of the loop. Each step feeds the next, and the KL leash ties the update back to the reference model.