Policy Gradient Methods › From Sequences to Tokens

Michael BrenndoerferDecember 26, 20254 min read
Language AI HandbookAll chapters
Part 37 of 67Alignment and RLHF
Chapter 277Policy Gradient Methods › From Sequences to Tokens
Reading section 5 of 14 · From Sequences to Tokens · 4 minSection 5 of 14 · 4 min

Book contents

Section 5 of 14 · 4 min read

Policy Gradient Methods › From Sequences to TokensOne section · Sections in this chapter5 / 14 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

For language models, we need to express the log-probability of an entire sequence in terms of individual token log-probabilities. This connection is needed because our models compute token-level probabilities, not sequence-level probabilities directly. Fortunately, the autoregressive structure of language models makes this conversion straightforward.

Using the autoregressive factorization from the previous section, we can apply the logarithm to convert the product of probabilities into a sum of log-probabilities:

log⁡πθ(y∣x)=log⁡∏t=1Tπθ(yt∣x,y<t)=∑t=1Tlog⁡πθ(yt∣x,y<t)\log \pi_\theta(y|x) = \log \prod_{t=1}^{T} \pi_\theta(y_t | x, y_{<t}) = \sum_{t=1}^{T} \log \pi_\theta(y_t | x, y_{<t})

where:

  • ∑t=1T\sum_{t=1}^{T}: sum over all timesteps in the sequence
  • log⁡πθ(yt∣x,y<t)\log \pi_\theta(y_t | x, y_{<t}): log-probability of the specific token yty_t given context

The logarithm converts the product of probabilities into a sum of log-probabilities. This is where the log-derivative trick pays off: instead of dealing with products of many small numbers (which can underflow numerically when sequence lengths are in the hundreds), we work with sums of log-probabilities, which remain in a numerically stable range.

Each term log⁡πθ(yt∣st)\log \pi_\theta(y_t | s_t) is simply the log-softmax output of our model at position tt, evaluated at the selected token. This is exactly what we compute during the forward pass when training language models with cross-entropy loss. When the model processes a sequence, it produces logits for every position, and applying log-softmax gives us log-probabilities. To compute the log-probability of a specific token choice, we simply index into this log-probability vector at the chosen token's index.

The gradient of the full sequence log-probability then follows from the linearity of differentiation. The gradient of a sum is the sum of gradients:

∇θlog⁡πθ(y∣x)=∑t=1T∇θlog⁡πθ(yt∣st)\nabla_\theta \log \pi_\theta(y|x) = \sum_{t=1}^{T} \nabla_\theta \log \pi_\theta(y_t | s_t)

where:

  • sts_t: the state (context) at timestep tt, equivalent to (x,y<t)(x, y_{<t})
  • ∇θlog⁡πθ(yt∣st)\nabla_\theta \log \pi_\theta(y_t | s_t): gradient of the log-probability for a single token decision

Each term in the sum corresponds to one token's contribution to the overall sequence probability. We compute each term by running a standard backward pass through the model at that timestep.

Putting this together, the policy gradient becomes a sum over token-level contributions, all weighted by the same sequence-level reward:

∇θJ(θ)=Ey∼πθ[∑t=1T∇θlog⁡πθ(yt∣st)⋅R(x,y)]\nabla_\theta J(\theta) = \mathbb{E}_{y \sim \pi_\theta}\left[\sum_{t=1}^{T} \nabla_\theta \log \pi_\theta(y_t | s_t) \cdot R(x, y)\right]

where:

  • ∑t=1T∇θlog⁡πθ(yt∣st)\sum_{t=1}^{T} \nabla_\theta \log \pi_\theta(y_t | s_t): the accumulated gradient from all tokens in the sequence
  • R(x,y)R(x, y): the final reward, which scales the gradient of every token choice equally

The same reward R(x,y)R(x, y) multiplies every token's gradient. This is the basic credit assignment mechanism of REINFORCE: even though we only receive reward at the end, it propagates back to influence all token choices. Every token that contributed to the response receives the same credit or blame for the final outcome. This is both a strength and a weakness of the basic approach. It allows learning from sparse rewards, but it does not distinguish between tokens that were important for the reward and tokens that were irrelevant to it.

Out[6]:
Visualization
Line plot of per-token log-probabilities showing negative values at each timestep position in the sequence.
Per-token log-probabilities for a single generated sequence, showing individual $\log \pi(y_t|s_t)$ values at each position. All values are negative since probabilities lie in (0,1), and more surprising token choices (lower probability) produce more negative values.
Line plot of cumulative log-probability decreasing as more tokens are added, with annotation of the final value.
Cumulative log-probability accumulating across token positions. The final cumulative value equals the log-probability of the entire sequence and determines the magnitude of the gradient update in the REINFORCE algorithm.