Recommended resources
The freely available resources this lesson will direct you to.
- RL Course by David Silver - Lecture 7: Policy Gradient Methodswww.youtube.com
- L3 Policy Gradients and Advantage Estimation (Foundations of Deep RL Series)www.youtube.com
- [PDF] 5) Policy-Gradient Methods - Melih Kandemirmelihkandemir.github.io
- Policy Gradient Methods for Control Applicationswww.cs.ubc.ca
Introduction
In our last lesson, we explored Q-learning, a canonical value-based method. The core strategy was to learn an action-value function, , which estimates the long-term reward for taking action in state , and then use this function to derive a policy, typically by acting greedily. This approach is powerful, but it relies on an indirect method for finding the best policy: first find the values, then infer the actions. What if we could learn the policy directly?
This lesson introduces a fundamentally different and powerful class of algorithms: policy gradient methods. Instead of learning value functions, we will directly parameterize the policy itself, , and use optimization techniques to find the parameters that maximize the expected return. You will learn the theoretical motivation for this approach, its advantages over value-based methods, especially in complex scenarios, and the core mathematical principle—the Policy Gradient Theorem—that makes it possible. We will then see how this theorem is put into practice, culminating in an understanding of Actor-Critic methods, a hybrid approach that combines the strengths of both policy-based and value-based learning.
The Limits of Value-Based Methods
Value-based methods like Q-learning are highly effective, but they face challenges in certain situations. Considering these challenges provides the motivation for exploring a direct policy-based approach.
-
Continuous or High-Dimensional Action Spaces: The core of control in Q-learning involves finding the action that maximizes the Q-value: . If the action space is discrete and small, this is a simple lookup. However, if the action space is continuous (e.g., the precise torque to apply to a robotic arm) or exceptionally large, this maximization step becomes a complex optimization problem in its own right, which must be solved at every single step.
-
Stochastic Policies: Value-based methods typically produce deterministic policies (or -greedy variants). In some environments, the optimal policy is genuinely stochastic. For instance, in a game of rock-paper-scissors, the optimal policy is to play each action with a probability of 1/3. A deterministic policy would be easily exploited. Policy-based methods can naturally learn truly stochastic policies.
-
Policy Complexity vs. Value Function Complexity: In some problems, the optimal policy might be much simpler to represent than its corresponding value function. Imagine a robot needing to move left when a specific light turns on. The policy ("if light, go left") is simple. The value function, which must quantify the long-term reward of every possible state-action pair, might be incredibly complex.
RL Course by David Silver - Lecture 7: Policy Gradient Methods
David Silver's classic lecture provides an excellent overview of the motivations for policy-based methods.
Please watch the segment from "come back to this". Pay close attention to the three main advantages he outlines: better convergence properties, effectiveness in continuous action spaces, and the ability to learn stochastic policies.
The Core Idea: Direct Policy Parameterization
The central idea of policy gradient methods is to treat reinforcement learning as a direct optimization problem. We define the policy as a parameterized function , where is a vector of parameters (e.g., the weights of a neural network). This function outputs the probability of taking action in state .
Our goal is to find the parameters that maximize a performance objective, , which is typically the expected total return. We can then use gradient ascent to update the parameters in the direction that increases this objective:
The entire challenge now boils down to one question: How do we compute the policy gradient, ? The environment's dynamics and the reward function are unknown, so we can only rely on experience sampled by following the policy.
The Policy Gradient Theorem
The solution comes from a fundamental result known as the Policy Gradient Theorem. This theorem provides a way to calculate an analytical expression for the policy gradient that we can estimate from samples. While the full derivation is involved, the final result is elegant and intuitive. For any differentiable policy , the gradient of the objective is given by:
Let's break down this crucial equation:
- indicates that we are taking an expectation over trajectories of states and actions generated by following our current policy .
- is the score function. This is the gradient of the log-probability of the action we actually took. From a mathematical perspective (using the "log-derivative trick"), this term allows us to express the gradient of an expectation as the expectation of a gradient. Intuitively, this vector tells us which direction to move in to make the action more likely.
- is the familiar action-value function: the expected return for taking action in state and then following policy thereafter. It acts as a weighting term, telling us the long-term consequence of that action.
The intuition is powerful: We sample an action from our policy in state . We then see how "good" that action was by estimating its Q-value. If is high (a good action), we update our parameters in the direction of to make that action more probable. If is low or negative (a bad action), the update moves in the opposite direction, making that action less probable.
L3 Policy Gradients and Advantage Estimation (Foundations of Deep RL Series)
In this video, Pieter Abbeel provides a clear, step-by-step derivation of the policy gradient, starting from the objective function and applying the likelihood ratio trick.
Watch the segments from the likelihood ratio policy gradient to understand the formal setup and the core mathematical trick. Then, watch from the intuition behind this equation to grasp what the formula means in practice. Finally, the segment from decompose the pattern shows how the gradient calculation simplifies to depend only on the policy, not the environment dynamics.
REINFORCE: Monte Carlo Policy Gradient
The simplest algorithm that stems directly from the Policy Gradient Theorem is called REINFORCE. It's a Monte Carlo method, meaning it learns from complete episodes.
The algorithm works as follows:
- Initialize the policy parameters arbitrarily.
- Generate a full episode by following the current policy : .
- For each time step in the episode, calculate the return from that point onward: .
- This return is used as an unbiased, sampled estimate of .
- Update the policy parameters by taking a step in the direction of the estimated gradient:
While simple and unbiased, REINFORCE suffers from the classic problem of Monte Carlo methods: high variance. The return is the sum of many random reward variables, making it a very noisy estimate of the true Q-value. An action that was good in the long run might, by chance, be part of an episode that ended poorly, and it would be incorrectly penalized. This high variance leads to slow and unstable learning.
Policy Gradient Methods for Control Applications
This technical report offers a historical and theoretical perspective on why early policy gradient methods like REINFORCE, despite being unbiased, were initially abandoned.
Read the section titled "Early policy gradients methods..." on page 5. This will give you a deeper appreciation for the high variance problem that plagued these initial approaches.
Reducing Variance: Baselines and Actor-Critic Methods
The key to making policy gradients practical is to reduce the variance of the gradient estimate. A common and powerful technique is to introduce a baseline, , which is subtracted from the Q-value term. The policy gradient can be rewritten as:
As long as the baseline does not depend on the action , this transformation is exact and does not change the expectation of the gradient (it introduces no bias). However, a well-chosen baseline can dramatically reduce the variance of the estimate.
A natural and effective choice for the baseline is the state-value function, . This leads to a new term in our update:
This is called the Advantage Function. It measures how much better taking action is compared to the average action from state . Intuitively, we don't care how good a state is in absolute terms; we only care about whether an action we took was better or worse than average for that state. Using the advantage leads to much more stable learning.
This brings us to Actor-Critic methods. These methods explicitly learn both a policy and a value function. They are a hybrid that combines the best of policy-based and value-based approaches.
- The Actor is the policy . It decides which actions to take. Its parameters are updated using the policy gradient, guided by feedback from the critic.
- The Critic is a value function estimator, for example with parameters . It evaluates the actions taken by the actor by learning the value function of the current policy. It doesn't take actions; it only "criticizes" the actor.
In a typical Actor-Critic loop:
- The actor takes an action in state . The environment returns a reward and next state .
- The critic evaluates the "surprise" or TD-error: . This TD-error is an unbiased estimate of the advantage function!
- The critic's parameters are updated to minimize this error (e.g., using TD learning).
- The actor's parameters are updated using the TD-error as the advantage signal:
This structure allows for online, step-by-step learning and is far more sample-efficient and stable than the vanilla REINFORCE algorithm. The diagram below illustrates the conceptual difference between the direct optimization loop of policy gradients and the value-fitting loop of value-based methods. Actor-Critic effectively merges these two paradigms.

Conclusion
This lesson introduced a new perspective on reinforcement learning, shifting from learning values to learning policies directly. This approach, grounded in the Policy Gradient Theorem, offers compelling advantages, particularly for problems with continuous action spaces or where stochastic policies are optimal.
Here are the key takeaways:
- Policy Gradient (PG) methods directly parameterize a policy and optimize the parameters to maximize expected return using gradient ascent.
- They differ from value-based methods (like Q-learning) which learn a value function and derive a policy implicitly. PG methods are often more stable and better suited for continuous action spaces.
- The Policy Gradient Theorem provides a theoretical foundation, showing that the policy gradient is the expected product of the score function () and the action-value function ().
- REINFORCE is the basic Monte Carlo implementation of this theorem but suffers from high variance.
- Actor-Critic methods significantly improve upon this by using a learned value function (the critic) to provide a low-variance estimate of the advantage (the advantage function), which then guides the updates to the policy (the actor).
We have now covered the three main families of model-free RL algorithms: value-based (Q-learning), policy-based (REINFORCE), and the hybrid Actor-Critic approach. These form the building blocks for most advanced algorithms in use today.
In the next lessons, we will shift our focus from algorithms to the practical engineering challenges of deploying and managing machine learning models in production, starting with the principles of MLOps.