ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing
ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing

All Courses

Intermediate Reinforcement Learning Techniques
Chapter 1: Revisiting Reinforcement Learning Fundamentals
The Reinforcement Learning Problem Setup
Markov Decision Processes (MDPs) Recap
Value Functions and Bellman Equations
Tabular Solution Methods: Q-Learning and SARSA
Limitations of Tabular Methods
Quiz for Chapter 1
Chapter 2: Deep Q-Networks (DQN)
Introduction to Function Approximation
Using Neural Networks for Q-Value Approximation
The DQN Algorithm Architecture
Experience Replay Mechanism
Fixed Q-Targets (Target Networks)
Loss Function for DQN Training
Hands-on Practical: Implementing DQN for CartPole
Quiz for Chapter 2
Chapter 3: Improvements and Variants of DQN
The Overestimation Problem in Q-Learning
Double DQN (DDQN)
Dueling Network Architectures
Combining DQN Improvements
Prioritized Experience Replay (Brief Overview)
Practice: Implementing Double DQN
Quiz for Chapter 3
Chapter 4: Policy Gradient Methods
Limitations of Value-Based Methods
Direct Policy Parameterization
The Policy Gradient Theorem
REINFORCE Algorithm
Understanding Variance in Policy Gradients
Baselines for Variance Reduction
Hands-on Practical: Implementing REINFORCE
Quiz for Chapter 4
Chapter 5: Actor-Critic Methods
Combining Policy and Value Estimation
Actor-Critic Architecture Overview
Advantage Actor-Critic (A2C)
Asynchronous Advantage Actor-Critic (A3C)
Implementation Considerations for Actor-Critic
Comparison: REINFORCE vs A2C/A3C
Practice: Developing an A2C Implementation
Quiz for Chapter 5

The Policy Gradient Theorem

Value-based methods like DQN have limitations, especially with continuous actions or when stochastic policies are needed. Policy gradient methods offer a different path: directly optimizing the parameters of the policy itself. But how do we actually do that? How do we know which way to adjust the policy parameters θ\thetaθ to improve performance?

Our goal is to find policy parameters θ\thetaθ that maximize the expected total return. We can define an objective function, often denoted as J(θ)J(\theta)J(θ), which represents the expected return when following policy πθ\pi_\thetaπθ​. For instance, in an episodic task, this could be the expected cumulative discounted reward starting from an initial state distribution:

J(θ)=Eτ∼πθ[∑t=0T−1γtrt+1]=Eτ∼πθ[G0]J(\theta) = \mathbb{E}_{\tau \sim \pi_\theta} [ \sum_{t=0}^{T-1} \gamma^t r_{t+1} ] = \mathbb{E}_{\tau \sim \pi_\theta} [ G_0 ]J(θ)=Eτ∼πθ​​[t=0∑T−1​γtrt+1​]=Eτ∼πθ​​[G0​]

Here, τ\tauτ represents a full trajectory (s0,a0,r1,s1,...)(s_0, a_0, r_1, s_1, ...)(s0​,a0​,r1​,s1​,...) generated by following the policy πθ(a∣s)\pi_\theta(a|s)πθ​(a∣s), γ\gammaγ is the discount factor, rt+1r_{t+1}rt+1​ is the reward received after taking action ata_tat​ in state sts_tst​, and G0G_0G0​ is the total discounted return for the entire episode.

To maximize J(θ)J(\theta)J(θ), we can use gradient ascent. The update rule for the policy parameters would look like this:

θ←θ+α∇θJ(θ)\theta \leftarrow \theta + \alpha \nabla_\theta J(\theta)θ←θ+α∇θ​J(θ)

where α\alphaα is the learning rate and ∇θJ(θ)\nabla_\theta J(\theta)∇θ​J(θ) is the gradient of the objective function with respect to the policy parameters. The core challenge lies in calculating this gradient, ∇θJ(θ)\nabla_\theta J(\theta)∇θ​J(θ). How does changing the policy parameters θ\thetaθ affect the expected total return? This involves not only how the policy chooses actions but also how the resulting trajectories and reward distributions change, which seems complicated to compute directly.

This is where the Policy Gradient Theorem comes in. It provides a way to compute this gradient without needing to know or differentiate the environment's dynamics (the transition probabilities p(s′∣s,a)p(s'|s,a)p(s′∣s,a)). This theorem is foundational for all policy gradient algorithms.

One common form of the Policy Gradient Theorem states that the gradient of the objective function is given by:

∇θJ(θ)=Eτ∼πθ[∑t=0T−1∇θlog⁡πθ(at∣st)Gt]\nabla_\theta J(\theta) = \mathbb{E}_{\tau \sim \pi_\theta} [ \sum_{t=0}^{T-1} \nabla_\theta \log \pi_\theta(a_t|s_t) G_t ]∇θ​J(θ)=Eτ∼πθ​​[t=0∑T−1​∇θ​logπθ​(at​∣st​)Gt​]

Let's break down this important expression:

  1. Eτ∼πθ[...]\mathbb{E}_{\tau \sim \pi_\theta} [ ... ]Eτ∼πθ​​[...]: The expectation is taken over trajectories τ\tauτ generated by following the current policy πθ\pi_\thetaπθ​. This means we can estimate the gradient by running the policy in the environment and collecting sample trajectories.
  2. ∑t=0T−1...\sum_{t=0}^{T-1} ...∑t=0T−1​...: We sum over all time steps within a trajectory.
  3. ∇θlog⁡πθ(at∣st)\nabla_\theta \log \pi_\theta(a_t|s_t)∇θ​logπθ​(at​∣st​): This is the gradient of the log-probability of taking the specific action ata_tat​ in state sts_tst​ under the current policy parameters θ\thetaθ. This term tells us the direction in parameter space θ\thetaθ that most increases the probability of selecting action ata_tat​ when in state sts_tst​. It's calculable because we defined the policy πθ\pi_\thetaπθ​ (e.g., using a neural network whose output determines action probabilities) and can therefore compute its derivatives with respect to its parameters θ\thetaθ.
  4. Gt=∑k=tT−1γk−trk+1G_t = \sum_{k=t}^{T-1} \gamma^{k-t} r_{k+1}Gt​=∑k=tT−1​γk−trk+1​: This is the total discounted return received from time step ttt onwards in the trajectory τ\tauτ. It represents the long-term consequence of having taken action ata_tat​ in state sts_tst​.

The Log-Derivative Trick

You might wonder how we arrived at the ∇θlog⁡πθ(at∣st)\nabla_\theta \log \pi_\theta(a_t|s_t)∇θ​logπθ​(at​∣st​) term. It comes from a useful identity sometimes called the "log-derivative trick" or "likelihood ratio trick". The gradient of the policy ∇θπθ(a∣s)\nabla_\theta \pi_\theta(a|s)∇θ​πθ​(a∣s) can be rewritten as:

∇θπθ(a∣s)=πθ(a∣s)∇θπθ(a∣s)πθ(a∣s)=πθ(a∣s)∇θlog⁡πθ(a∣s)\nabla_\theta \pi_\theta(a|s) = \pi_\theta(a|s) \frac{\nabla_\theta \pi_\theta(a|s)}{\pi_\theta(a|s)} = \pi_\theta(a|s) \nabla_\theta \log \pi_\theta(a|s)∇θ​πθ​(a∣s)=πθ​(a∣s)πθ​(a∣s)∇θ​πθ​(a∣s)​=πθ​(a∣s)∇θ​logπθ​(a∣s)

This reformulation is significant because it allows us to express the gradient ∇θJ(θ)\nabla_\theta J(\theta)∇θ​J(θ) as an expectation involving πθ\pi_\thetaπθ​ itself. This means we can estimate the gradient using samples (actions ata_tat​) drawn from the current policy πθ\pi_\thetaπθ​, without needing to explicitly calculate how changes in θ\thetaθ affect the state visitation distribution.

Intuition Behind the Theorem

The Policy Gradient Theorem provides an intuitive update mechanism. The term ∇θlog⁡πθ(at∣st)\nabla_\theta \log \pi_\theta(a_t|s_t)∇θ​logπθ​(at​∣st​) indicates how to change θ\thetaθ to increase (or decrease) the probability of action ata_tat​ in state sts_tst​. The term GtG_tGt​ acts as a weighting factor for this update direction.

  • If the return GtG_tGt​ following action ata_tat​ was high (good outcome), the gradient update pushes θ\thetaθ in the direction indicated by ∇θlog⁡πθ(at∣st)\nabla_\theta \log \pi_\theta(a_t|s_t)∇θ​logπθ​(at​∣st​), effectively increasing the probability of taking action ata_tat​ again in state sts_tst​ in the future.
  • If the return GtG_tGt​ was low (bad outcome), the update pushes θ\thetaθ in the opposite direction, decreasing the probability of taking ata_tat​ in state sts_tst​.

Essentially, the policy gradient method reinforces actions proportionally to the total return they ultimately lead to.

The Policy Gradient Theorem provides the theoretical justification for algorithms like REINFORCE, which we will explore next. REINFORCE directly uses Monte Carlo sampling (running full episodes) to estimate the expectation in the theorem and update the policy parameters. As we'll see, while straightforward, this approach can suffer from high variance in the gradient estimates, motivating further refinements like using baselines.

  • Reinforcement Learning: An Introduction, Richard S. Sutton and Andrew G. Barto, 2018 (MIT Press) - Definitive textbook covering the theoretical foundations of reinforcement learning, including a detailed derivation and explanation of the Policy Gradient Theorem and its applications.
  • Simple statistical gradient-following algorithms for connectionist reinforcement learning, Ronald J. Williams, 1992 Machine Learning, Vol. 8 (Kluwer Academic Publishers, Boston) DOI: 10.1007/BF00992696 - Introduces the REINFORCE algorithm, a direct application of the Policy Gradient Theorem, and provides foundational insights into policy gradient methods.
  • CS285: Deep Reinforcement Learning, Lecture 5: Policy Gradients I, Sergey Levine, 2020 (UC Berkeley) - Lecture notes from a deep reinforcement learning course, offering a modern perspective on policy gradient methods, their derivation, and practical considerations.

© 2026 ApX Machine Learning