ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing
ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing

All Courses

Introduction to Reinforcement Learning
Chapter 1: Foundations of Reinforcement Learning
What is Reinforcement Learning?
Agents and Environments
States, Actions, and Rewards
Policies: Mapping States to Actions
The RL Workflow: Interaction Loops
Types of RL Tasks: Episodic vs Continuing
Comparing RL with Other Learning Types
Setting up Your Python Environment for RL
Quiz for Chapter 1
Chapter 2: Markov Decision Processes (MDPs)
Modeling Sequential Decision Making
Formal Definition of an MDP
State Transition Probabilities
Reward Functions
Return: Cumulative Future Rewards
Discounting Future Rewards
Policies and Value Functions (Vπ, Qπ)
Finding Optimal Policies
Quiz for Chapter 2
Chapter 3: Estimating Value Functions
The Bellman Expectation Equation
The Bellman Optimality Equation
Solving Bellman Equations (Overview)
Dynamic Programming: Policy Iteration
Dynamic Programming: Value Iteration
Limitations of Dynamic Programming
Quiz for Chapter 3
Chapter 4: Monte Carlo Methods
Learning from Complete Episodes
Monte Carlo Prediction: Estimating Vπ
Monte Carlo Control: Estimating Qπ
On-Policy vs Off-Policy Learning
MC Control without Exploring Starts
On-Policy First-Visit MC Control Implementation
Off-Policy MC Prediction and Control Intro
Practice: Implementing MC Prediction
Quiz for Chapter 4
Chapter 5: Temporal-Difference Learning
Learning from Incomplete Episodes
TD(0) Prediction: Estimating Vπ
Advantages of TD Learning over MC
SARSA: On-Policy TD Control
Q-Learning: Off-Policy TD Control
Comparing SARSA and Q-Learning
Expected SARSA
Hands-on Practical: Implementing Q-Learning
Quiz for Chapter 5
Chapter 6: Function Approximation in RL
Handling Large State Spaces
Value Function Approximation (VFA)
Feature Vectors for State Representation
Linear Methods for VFA
Gradient Descent for Parameter Learning
Semi-gradient TD Methods
Using Neural Networks for VFA
Practice: Applying Linear VFA
Quiz for Chapter 6
Chapter 7: Introduction to Deep Q-Networks (DQN)
Combining Q-Learning with Deep Learning
Challenges with Neural Networks in RL
Experience Replay Mechanism
Fixed Q-Targets (Target Networks)
The DQN Algorithm Structure
Architectural Considerations for DQNs
Hands-on Practical: Building a Basic DQN
Quiz for Chapter 7
Chapter 8: Introduction to Policy Gradient Methods
Learning Policies Directly
Policy Gradient Theorem (Concept)
REINFORCE Algorithm
Baselines for Variance Reduction
Actor-Critic Methods Overview
Comparing Value-Based and Policy-Based Methods
Practice: Implementing REINFORCE
Quiz for Chapter 8

REINFORCE Algorithm

REINFORCE is a specific algorithm designed for directly optimizing a parameterized policy πθ(a∣s)\pi_\theta(a|s)πθ​(a∣s). Also known as Monte Carlo Policy Gradient, REINFORCE provides a straightforward way to adjust the policy parameters θ\thetaθ based on the outcomes of interactions with the environment.

The core principle of REINFORCE is intuitive: increase the probability of actions that lead to good outcomes and decrease the probability of actions that lead to poor outcomes. Since we're dealing with parameterized policies, "increasing the probability" means adjusting the parameters θ\thetaθ in a direction suggested by the gradient of the policy's performance.

REINFORCE is a Monte Carlo method because it learns only after observing the complete return from an entire episode. It doesn't update parameters mid-episode like Temporal Difference methods (SARSA, Q-learning). Instead, it waits until the episode finishes, calculates the return (GtG_tGt​) obtained from each state StS_tSt​ onwards, and then updates the policy parameters based on these returns.

The REINFORCE Update Rule

Let J(θ)J(\theta)J(θ) represent the objective function we want to maximize, typically the expected return from the start state. The Policy Gradient Theorem provides a way to calculate the gradient of this objective with respect to the policy parameters θ\thetaθ. REINFORCE uses a sampled version of this gradient based on experiences from episodes.

For a single episode, the update applied to the parameters θ\thetaθ after the episode concludes, examining each time step ttt, is:

θ←θ+α∑t=0T−1Gt∇θlog⁡πθ(At∣St)\theta \leftarrow \theta + \alpha \sum_{t=0}^{T-1} G_t \nabla_\theta \log \pi_\theta(A_t|S_t)θ←θ+αt=0∑T−1​Gt​∇θ​logπθ​(At​∣St​)

Alternatively, updates can happen incrementally at each step ttt of the episode, using the return GtG_tGt​ calculated after the episode finishes:

θ←θ+αGt∇θlog⁡πθ(At∣St)\theta \leftarrow \theta + \alpha G_t \nabla_\theta \log \pi_\theta(A_t|S_t)θ←θ+αGt​∇θ​logπθ​(At​∣St​)

Let's break down the components of this update rule:

  • α\alphaα: The learning rate, a small positive scalar determining the step size.
  • GtG_tGt​: The total discounted return starting from time step ttt until the end of the episode. It's calculated as Gt=Rt+1+γRt+2+γ2Rt+3+...+γT−t−1RTG_t = R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + ... + \gamma^{T-t-1} R_TGt​=Rt+1​+γRt+2​+γ2Rt+3​+...+γT−t−1RT​, where TTT is the terminal time step and γ\gammaγ is the discount factor. This GtG_tGt​ value represents the actual outcome experienced after taking action AtA_tAt​ in state StS_tSt​ (and following policy πθ\pi_\thetaπθ​ thereafter).
  • ∇θlog⁡πθ(At∣St)\nabla_\theta \log \pi_\theta(A_t|S_t)∇θ​logπθ​(At​∣St​): This is the gradient of the logarithm of the probability of taking the specific action AtA_tAt​ in state StS_tSt​, according to the current policy parameterization θ\thetaθ. This term is often called the "eligibility vector". It points in the direction in parameter space that most rapidly increases the log-probability of the action AtA_tAt​ being chosen in state StS_tSt​.

The update rule essentially pushes the parameters θ\thetaθ in the direction specified by ∇θlog⁡πθ(At∣St)\nabla_\theta \log \pi_\theta(A_t|S_t)∇θ​logπθ​(At​∣St​), scaled by the return GtG_tGt​.

  • If GtG_tGt​ is positive (better than average outcome, loosely speaking), the update increases the probability of taking action AtA_tAt​ in state StS_tSt​ in the future.
  • If GtG_tGt​ is negative (worse than average outcome), the update decreases the probability of taking action AtA_tAt​ in state StS_tSt​ in the future.

The magnitude of the update is proportional to the return GtG_tGt​, so actions leading to significantly better or worse outcomes result in larger parameter adjustments.

The REINFORCE Algorithm Steps

Here's a typical outline of the REINFORCE algorithm:

  1. Initialize: Choose a policy parameterization πθ(a∣s)\pi_\theta(a|s)πθ​(a∣s) (e.g., a neural network with parameters θ\thetaθ) and initialize θ\thetaθ randomly or with predetermined weights. Set a learning rate α\alphaα.
  2. Loop (for each episode): a. Generate Episode: Run one full episode by following the current policy πθ\pi_\thetaπθ​. This means starting in S0S_0S0​, choosing A0∼πθ(⋅∣S0)A_0 \sim \pi_\theta( \cdot | S_0)A0​∼πθ​(⋅∣S0​), observing R1,S1R_1, S_1R1​,S1​, choosing A1∼πθ(⋅∣S1)A_1 \sim \pi_\theta( \cdot | S_1)A1​∼πθ​(⋅∣S1​), observing R2,S2R_2, S_2R2​,S2​, and so on, until a terminal state STS_TST​ is reached. Store the sequence of states, actions, and rewards: (S0,A0,R1,S1,A1,R2,...,ST−1,AT−1,RT)(S_0, A_0, R_1, S_1, A_1, R_2, ..., S_{T-1}, A_{T-1}, R_T)(S0​,A0​,R1​,S1​,A1​,R2​,...,ST−1​,AT−1​,RT​). b. Calculate Returns: For each time step ttt in the episode (from t=0t = 0t=0 to T−1T-1T−1): Calculate the return Gt=∑k=t+1Tγk−t−1RkG_t = \sum_{k=t+1}^{T} \gamma^{k-t-1} R_kGt​=∑k=t+1T​γk−t−1Rk​. c. Update Parameters: For each time step ttt in the episode (from t=0t = 0t=0 to T−1T-1T−1): Compute the gradient ∇θlog⁡πθ(At∣St)\nabla_\theta \log \pi_\theta(A_t|S_t)∇θ​logπθ​(At​∣St​). Update the policy parameters: θ←θ+αGt∇θlog⁡πθ(At∣St)\theta \leftarrow \theta + \alpha G_t \nabla_\theta \log \pi_\theta(A_t|S_t)θ←θ+αGt​∇θ​logπθ​(At​∣St​).
  3. Repeat: Go back to step 2 to generate and learn from more episodes.

The updates can be accumulated across all steps within an episode and applied once at the end, or applied incrementally after calculating each GtG_tGt​. Modern deep learning frameworks often facilitate calculating ∇θlog⁡πθ(At∣St)\nabla_\theta \log \pi_\theta(A_t|S_t)∇θ​logπθ​(At​∣St​) using automatic differentiation.

Representing the Policy

The policy πθ(a∣s)\pi_\theta(a|s)πθ​(a∣s) needs to be a function that takes the state sss and parameters θ\thetaθ as input and outputs probabilities for each possible action aaa. For discrete action spaces, a common approach is to use a neural network that outputs scores (logits) for each action, followed by a softmax function to convert these scores into probabilities:

πθ(a∣s)=softmax(NNθ(s))a=elogita∑belogitb\pi_\theta(a|s) = \text{softmax}(\text{NN}_\theta(s))_a = \frac{e^{\text{logit}_a}}{\sum_{b} e^{\text{logit}_b}}πθ​(a∣s)=softmax(NNθ​(s))a​=∑b​elogitb​elogita​​

Where NNθ(s)\text{NN}_\theta(s)NNθ​(s) is the output vector of the neural network for state sss, and logita\text{logit}_alogita​ is the element corresponding to action aaa. The gradient ∇θlog⁡πθ(a∣s)\nabla_\theta \log \pi_\theta(a|s)∇θ​logπθ​(a∣s) can then be computed with respect to the network parameters θ\thetaθ.

Advantages and Disadvantages

REINFORCE is simple and serves as a foundation for more advanced policy gradient methods.

Advantages:

  • Can learn stochastic policies naturally.
  • Can handle continuous action spaces by parameterizing the policy appropriately (e.g., outputting parameters of a Gaussian distribution).
  • Relatively easy to understand and implement compared to some other methods.

Disadvantages:

  • High Variance: The biggest drawback. The update depends on the Monte Carlo return GtG_tGt​, which can vary significantly between episodes even with the same policy, purely due to randomness in the environment or the policy itself. This high variance makes learning slow and unstable.
  • Sample Inefficiency: Learning only occurs at the end of an episode, potentially requiring many episodes to make substantial progress, especially for long episodes.
  • Convergence: Can sometimes converge to local optima rather than the global optimum.

The high variance issue is particularly important. Imagine an episode where most actions were good, but one random unlucky event near the end resulted in a very low overall return GtG_tGt​ for all preceding steps. REINFORCE would incorrectly penalize all actions taken earlier in that episode. This motivates the development of techniques like baselines and Actor-Critic methods, which we will discuss next, to mitigate this variance and improve learning stability and efficiency.

© 2026 ApX Machine Learning