Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 41

REINFORCE: Policy Gradients with Sampled Returns

Recommended resources

Introduction

Welcome back! In our last lesson, we established the theoretical foundation for optimizing policies directly with the Policy Gradient Theorem. We saw that the gradient of our objective function can be expressed as an expectation:

This was a crucial breakthrough because it gives us a way to estimate the gradient by sampling from our policy, without needing to differentiate the environment's reward function.

Today, we will turn this theorem into a concrete algorithm. Our learning outcome is to describe the REINFORCE algorithm and how it uses sampled returns to estimate policy gradients. REINFORCE is the most direct implementation of the policy gradient theorem. While simple, it's a foundational algorithm that serves as the basis for the more advanced methods like PPO and GRPO that we will encounter later in the course.



1. The REINFORCE Algorithm

The core idea of REINFORCE is to use Monte Carlo estimation to approximate the expectation in the policy gradient theorem. In simple terms, we run our policy, see how well it did, and then "reinforce" the actions that led to good outcomes.

The following video provides a concise and powerful intuition for how this works.

Policy Gradient in 30 min

Let's start by seeing how the REINFORCE algorithm follows directly from the policy gradient theorem. This video explains the simple, yet powerful, update rule at the heart of the algorithm.

Watch from 10:29 to 11:48. Focus on the three main steps of the loop: roll out (generate a trajectory), calculate the reward, and perform the update step. Note how the reward is used to scale the 'score' (the gradient of the log-probability).

As the video explained, we iteratively update our policy based on the outcomes of sampled trajectories. This entire process can be visualized as a cycle.

REINFORCE Algorithm Cycle
This diagram shows the iterative cycle of the REINFORCE algorithm. We repeatedly (1) Collect Episodes by running our current policy, (2) Calculate the Returns from those episodes, (3) use those returns to Compute the Policy Gradient, and (4) Update the Policy Parameters before starting the next iteration.

Let's formalize this with the algorithm's pseudocode.

Policy Gradient Algorithms - REINFORCE

The video and diagram gave us the high-level flow. Now, let's examine the precise steps in pseudocode format.

Review the pseudocode in the section titled 'Policy Gradient Algorithms - REINFORCE'. Compare these steps to the cycle diagram above. Notice how the algorithm loops through episodes, and within each episode, it loops through the timesteps to accumulate the gradient before making a single update.

The algorithm is as follows:

  1. Initialize the policy network with random parameters .
  2. Loop for a number of episodes:
    a. Generate a full trajectory by running the current policy .
    b. For each timestep from 0 to :
    i. Calculate the return from that step onwards: . This is the sum of discounted future rewards.
    ii. Update the policy parameters using gradient ascent. The update for the entire episode is a sum of the updates for each timestep:


2. Using Sampled Returns for Gradient Estimation

The central part of our learning outcome is understanding how REINFORCE uses sampled returns. The key is the connection between the theoretical action-value function, , and the sampled return, .

The policy gradient theorem can also be written using the action-value function :

By definition, is the expected return if we start by taking action in state and then follow policy thereafter. The REINFORCE algorithm makes a simple but profound move: it uses the actual return that we observed in a single sampled trajectory as an unbiased Monte Carlo estimate of .

This is why it's called a Monte Carlo method: we wait until the end of an episode to collect the full sequence of rewards, calculate the returns, and then use these sampled returns to update our policy.

This reading clarifies this crucial substitution.

Policy Gradient Algorithms | Lil'Log

Let's formalize the connection between the theoretical action-value Q and the sampled return G. This is the heart of how REINFORCE works.

Read the short section 'REINFORCE'. Pay close attention to the equation that shows G_t is used as an estimate for Q^\pi(S_t, A_t). This substitution is what allows us to compute the gradient from sampled trajectories.

In essence, the learning process becomes:

  1. Try something: Generate a trajectory.
  2. See what happened: Calculate the return for each step.
  3. Update your strategy: If was high, increase the probability of taking action in state . If was low, decrease it. The term points in the direction to increase the probability of , and the return scales how far we step in that direction.


3. A Concrete Example and a Critical Flaw

Now that we understand the mechanics, let's see REINFORCE in action and uncover one of its major limitations. This video first applies the algorithm to a simple 1D problem and then maps the concepts to training a Large Language Model.

Policy Gradient in 30 min

Theory is one thing, but seeing the algorithm work provides deeper intuition. This video walks through a simple numerical example and then applies the same logic to the task of teaching an LLM to write better poetry.

Watch from 11:48 to 19:02. First, focus on the 1D example (11:48-14:17) to see a concrete update step with numbers. Notice how a large reward leads to a large update in the policy parameter. Next, observe the LLM poetry example (14:17-19:02). Pay close attention to the weakness it exposes: the credit assignment problem. The same reward is applied to every single token, which is inefficient and leads to high variance.

The video brilliantly highlights the core weakness of vanilla REINFORCE: high variance. Because a single return is used to evaluate every action in a trajectory, the gradient estimate can be very noisy. A single bad action can spoil an otherwise good trajectory, unfairly punishing all the good actions. Conversely, a single brilliant action in a mediocre trajectory gets less credit than it deserves.

This "poor credit assignment" makes learning slow and unstable. It's like grading a group project with a single mark—it doesn't reflect individual contributions well.



Conclusion

In this lesson, we translated the Policy Gradient Theorem into the practical REINFORCE algorithm. We saw how it uses Monte Carlo sampling of episode returns to provide an unbiased estimate of the policy gradient, allowing us to directly optimize our policy.

Key Takeaways:

  • REINFORCE is a Monte Carlo policy gradient algorithm that updates policy parameters after completing each episode.
  • It works by using the sampled return from a trajectory as an estimate of the action-value function .
  • The update rule, , effectively "reinforces" actions that led to high returns by making them more probable.
  • A major weakness of REINFORCE is high variance due to the credit assignment problem, where every action in a trajectory receives the same reward signal, regardless of its individual contribution.

Preview of the Next Lesson:
The high variance of REINFORCE is a significant problem. How can we fix it? The solution is to judge an action not by the absolute return it achieved, but by how much better or worse that return was compared to what we expected. This leads to the idea of subtracting a baseline. In our next lesson, we will explore how subtracting a baseline from the return significantly reduces variance and stabilizes learning, setting the stage for the powerful actor-critic methods used in modern LLM alignment.