5.1 Why We Need Policy Gradients
Section Overview
Key takeaways
- Review the core idea of DQN from Chapter 4: learn , then use to select actions.
- Understand the fundamental limitation of value-based methods: they can only handle a finite set of discrete actions.
- Understand why policy-based methods learn directly, and the essential differences between the two routes in terms of action spaces, exploration mechanisms, and data efficiency.
What DQN Got Right
The DQN from Chapter 4 follows a clear recipe: approximate with a neural network, assign a score to each action, then pick the highest-scoring one with . The underlying logic is: rather than learning "what to do" directly, first learn "how much each action is worth" and then select the best. The policy is implicit -- it is hidden inside the over the -value table.
Consider a concrete moment in CartPole. Suppose the current state is (position, velocity, pole angle, angular velocity). The DQN network performs one forward pass on this state and outputs two values:
| Action | |
|---|---|
| Push left | |
| Push right |
simply compares them and returns the action with the highest value:
The key prerequisite is that the set of actions is finite and small, so we can compute a value for each one and compare them all. CartPole has only 2 actions, so we compare 2 numbers; LunarLander has 4 actions, so we compare 4 numbers. Scale up to 10, 100, or even 1000 actions, and still works -- it just requires computing a few more values and doing more comparisons, but the cost grows linearly with no fundamental difficulty.
| Number of actions | values to compute | comparisons | Feasibility |
|---|---|---|---|
| 2 | 2 | 1 | Easy |
| 4 | 4 | 3 | Easy |
| 1000 | 1000 | 999 | Feasible |
| Feasible but slow | |||
| Impossible |
The last row is where the problem lies. When the action space is continuous, there are infinitely many actions. It is impossible to compute a value for each one, let alone find the maximum among infinitely many values.
Where Breaks Down
requires comparing the values of all actions. As long as the number of actions is finite, this poses no problem. But many real-world tasks have continuous action spaces with infinitely many actions.
Robot Arm: Curse of Dimensionality
Controlling a robot arm is a canonical example. The shoulder, elbow, and wrist joints each have multiple degrees of freedom, and each degree of freedom applies a continuous torque . With 6 joints, the action space is -- infinitely many points in a six-dimensional continuous space. It is impossible to compute a value for every point, let alone find the over infinitely many points.
A natural idea is to discretize the continuous space and then apply . For instance, allow only 100 torque values per joint, approximating the continuous space with a finite grid. With 6 joints each taking 100 values, the total number of actions is:
actions, each requiring one -value computation. If a single forward pass of the neural network takes ( seconds), then a single action selection requires:
11.6 days just to choose one action. By contrast, a policy network needs only one forward pass -- feed the state into the network and directly output the action vector, taking roughly :
| Method | Computation per action selection | Time |
|---|---|---|
| DQN + discretization | forward passes | days |
| Policy network | 1 forward pass, directly outputs |
And this is only 6 joints with each joint discretized to 100 values. With more joints or finer precision requirements, the number of discretized actions grows exponentially. This is the curse of dimensionality: each additional joint multiplies the total number of actions by a constant factor.
LLM Generation: Probability Distributions Beat Greedy Decoding
Text generation in large language models faces a related issue. At each step, the model must select one token from tens of thousands. Suppose the vocabulary size is 50,000. itself is not difficult here -- comparing 50,000 numbers is computationally tractable. The problem is not computational feasibility, but generation quality.
Suppose the model is generating the next token and has output the following probabilities:
| token | |
|---|---|
| "is" | |
| "are" | |
| "was" | |
| "be" | |
| ... | ... |
(greedy decoding) always selects the highest-probability "is". If the probability distributions at several subsequent positions are similar, greedy decoding will repeatedly output the same token. Sampling from the distribution, on the other hand, gives a 25% chance of selecting "are", a 15% chance of selecting "was" -- this randomness is precisely what fluent text generation requires. A policy network naturally outputs a probability distribution , and sampling is the generation process itself.
Learning the Policy Directly
Since "score first, then select" does not work, take a different route: skip values and learn the policy directly. Instead of asking "how much is each action worth?", learn directly "what to do in which situation."
This is exactly the core idea of Route 2: the policy objective from Chapter 3 -- define a policy objective function , then directly optimize the parameters to maximize .
The difference between the two routes can be clarified with an analogy: value-based methods are like a food critic who scores every dish and then picks the highest-rated one; policy-based methods are like an experienced chef who, without scoring, simply knows what dish to make given the ingredients and the occasion.
What the Policy Network Outputs
The policy network does not output an action score, but a probability distribution. Consider CartPole: given the input state , the network performs a forward pass and finally outputs the probability of each action through a Softmax layer:
| Symbol | Meaning |
|---|---|
| Policy network parameterized by | |
| Probability of selecting action in state | |
| Current state (position, velocity, pole angle, angular velocity) | |
| Action probability vector output by the network |
Action selection is done by sampling, not comparison. From the distribution , draw a sample: generate a uniform random number ; if , push left; otherwise, push right. For example, with , since , the action is "push right."
Comparison with DQN
Given the same state, the two methods follow completely different paths:
DQN path: Network outputs values deterministic action
Policy network path: Network outputs probabilities sampling stochastic action
The most important distinction: once trained, DQN always outputs the same action for the same state (deterministic policy); a policy network may output different actions for the same state (stochastic policy). This stochasticity is not a flaw but a feature -- it naturally incorporates exploration without requiring a separate -greedy mechanism.
For continuous action spaces, the policy network switches to outputting the parameters of a Gaussian distribution. For example, a robot arm that needs to output torques for 6 joints: the policy network outputs a mean vector and a standard deviation , then samples the action from . No discretization, no , one forward pass.
Differences Between the Two Routes
| Value-Based (DQN) | Policy-Based (Policy Gradient) | |
|---|---|---|
| What it learns | : how much each action is worth | : what probability to assign each action |
| Action selection | (pick the highest score) | Sample from |
| Policy form | Deterministic (always pick the highest score) | Stochastic (outputs a probability distribution) |
| Action space | Discrete only | Discrete + continuous |
| Exploration | External (-greedy) | Built-in (probability distribution naturally includes exploration) |
| Data reuse | Off-policy (replay buffer can reuse old data) | On-policy (must use fresh data from the current policy) |
| Variance | Low (TD targets are relatively stable) | High (Monte Carlo returns fluctuate widely) |
| Representative algorithm | DQN (Chapter 4) | REINFORCE (this chapter) PPO (Chapter 7) |
Key differences explained row by row.
Action space -- This is the primary criterion for choosing between the routes. DQN's is simply not computable in continuous spaces. Policy gradients output a probability distribution directly -- Softmax for discrete actions, Gaussian for continuous actions -- just swap the output layer.
Exploration -- DQN's policy is deterministic (always pick ), so exploration must be injected via -greedy (review: the three components of DQN). The schedule must be tuned by hand: too large wastes experience, too small under-explores. Policy gradients naturally output a probability distribution, so exploration is built in -- if the network believes an action has a 30% chance of being worth trying, it will try it 30% of the time.
Data reuse -- This is the most practical engineering difference between the two routes. DQN is off-policy: the replay buffer stores old data that can be reused repeatedly for training. Policy gradients are on-policy: the in the gradient estimator requires data generated by the current policy . Once the policy updates, old data is invalidated. Data efficiency is inherently lower than DQN, and this is the biggest engineering weakness of policy gradients.
Numerical Comparison on the Same Scenario
Walk through both routes on a concrete scenario. Setup: 3 states , 2 actions , discount factor .
DQN route: learn values, select with
Suppose after training, DQN has learned the following table:
| State | ||
|---|---|---|
Apply at each state:
The result is a deterministic policy table: every state always selects the same action. If exploration is needed, -greedy must be added on top. For example, with , there is a 10% probability of choosing randomly:
| State | Probability of | Probability of |
|---|---|---|
-greedy exploration is uniform: the 10% random exploration is split equally between and . Even though is very close to (the two actions are nearly equally good), the exploration probability allocation is identical to where the gap is large.
Policy gradient route: learn , select by sampling
Suppose the policy network has learned the following probability distributions:
| State | ||
|---|---|---|
At , the policy network considers the two actions nearly equally good ( vs ), so the exploration ratio is naturally high; at and , one action clearly dominates, so exploration is naturally low. No manual tuning is needed -- the probability distribution itself encodes "how much to explore."
Placing the key numbers from both routes side by side:
| Dimension | DQN at | Policy gradient at |
|---|---|---|
| Network output | , | , |
| Action selection | Sampling: 55% chance , 45% chance | |
| Exploration | External -greedy (uniform random) | Built-in (adaptive probability distribution) |
| Continuous action space | Not applicable (requires discretized -value grid) | Applicable (directly outputs Gaussian parameters) |
The core difference is in the last row: DQN's confines the action space to a finite discrete set; policy gradients skip the step of "scoring every action" and directly output a probability distribution over "how to choose actions," removing the barrier of continuous action spaces entirely.
The Two Routes Are Not Opposed
Each route has its strengths and weaknesses, but they are not mutually exclusive. Chapter 6's Actor-Critic will merge the two: a policy network for decision-making, a value network for variance reduction. Before that, however, we need to establish the mathematical foundations of the policy-based route.
The next section starts from the policy objective function, derives the policy gradient theorem, and introduces the REINFORCE algorithm: REINFORCE Algorithm.