Policy Gradient Methods › Language Models as Policies
Language AI HandbookAll chapters Policy Gradient Methods › Language Models as PoliciesOne section · Sections in this chapter2 / 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
In reinforcement learning terminology, a policy is a function that maps states to actions. For language models, this mapping has a natural interpretation that becomes clear once we examine how text generation works. When you ask a language model to complete a sentence, it does not produce the entire response instantaneously. Instead, it generates one token at a time, each choice depending on everything that came before. This sequential decision-making process is precisely what reinforcement learning was designed to handle.
Think of a language model as an author writing a letter one word at a time. At each moment, the author has a specific context in mind (the prompt and everything written so far), and from that context, must choose the next word. Some words are clearly more appropriate given the context; others would seem out of place. The author's internal sense of which words fit a context is exactly what a language model encodes as a probability distribution. That probability distribution is the policy.
A policy is a probability distribution over actions given state , parameterized by . In language models, the policy is the model itself: given a context (state), it produces a probability distribution over the next token (action).
To understand why this framing is so powerful, consider what happens during text generation. The model receives a prompt, which establishes the initial context. Based on this context, the model must decide which token to produce first. This decision changes the context, as the newly generated token becomes part of the history. The model then faces a new decision: given the original prompt plus the first generated token, what should the second token be? This process continues, with each token choice altering the state and presenting a fresh decision problem.
The reinforcement learning vocabulary for this process is precise and useful. At each timestep , the system occupies a state , the agent takes an action , the state transitions to , and eventually the agent receives a reward . For language generation, this maps directly:
- State : The current context, consisting of the prompt plus all tokens generated so far
- Action : The next token to generate
- Policy : The probability the model assigns to token given context
This framing reveals why language model alignment is fundamentally a reinforcement learning problem. At each timestep, the model takes an action (selects a token), the state changes (the context grows), and eventually the complete sequence receives a reward from our reward model. The challenge is learning which tokens led to that reward. Unlike a game where individual moves might receive immediate feedback, language generation only reveals whether the response was good or bad after the entire sequence is complete. This delayed reward structure is exactly the scenario where reinforcement learning excels.
One important property of this MDP (Markov Decision Process) formulation is that the state transition is deterministic given the action. Once we pick token , the new state is simply the old state with appended. There is no external randomness in the transition. All the stochasticity lives in the policy: the model samples tokens from its probability distribution. This simplification makes the mathematics more tractable than general RL settings where transitions are random.
import torch.nn as nn
import torch.nn.functional as F
# A simple language model acting as a policy
class LanguageModelPolicy(nn.Module):
def __init__(self, vocab_size, hidden_size=256, num_layers=2):
super().__init__()
self.embedding = nn.Embedding(vocab_size, hidden_size)
self.lstm = nn.LSTM(
hidden_size, hidden_size, num_layers, batch_first=True
)
self.output = nn.Linear(hidden_size, vocab_size)
def forward(self, input_ids):
# State: all tokens seen so far
embeds = self.embedding(input_ids)
lstm_out, _ = self.lstm(embeds)
# Action distribution: probability over next token
logits = self.output(lstm_out)
return logits
def get_action_probs(self, state):
"""Get probability distribution over actions (next tokens)"""
logits = self.forward(state)
# Policy: pi_theta(a|s) - probability of each action given state
return F.softmax(logits[:, -1, :], dim=-1)
The model's parameters define the policy. Our goal is to adjust these parameters so that the model generates responses that receive high rewards. This means we need to find parameter values that make high-reward sequences more probable and low-reward sequences less probable. The question is: how do we know which direction to adjust the parameters? This is the central technical question that policy gradient theory answers.