REINFORCE from Scratch in Python: Policy Gradients

The REINFORCE algorithm in pure NumPy on CartPole: forward pass, backprop, RMSProp, and why policy gradients optimise the policy instead of a value function.

Cover Image for REINFORCE from Scratch in Python: Policy Gradients

An agent can choose a move by scoring the available actions, as in our DQN example. Another approach is to learn the action choices directly, using rewards to adjust which moves are more likely. A continuous action space also makes enumerating every possible move impractical. REINFORCE is a small enough example to build and inspect one episode at a time.

To connect rewards to action choices, we calculate the forward pass and gradients by hand. Episode outcomes then adjust the probabilities of moving left or right. That NumPy agent reaches CartPole's 500-step maximum in about 3,000 episodes.

By the end of this post, you'll implement the REINFORCE algorithm entirely from scratch in NumPy (including the forward pass, backpropagation, and RMSProp optimiser) and train it to balance CartPole. The entire implementation is about 100 lines. No PyTorch, no TensorFlow, just NumPy and the chain rule.

Quick Win: Run the Algorithm

Let's see REINFORCE in action. Click the badge to open the interactive notebook:

Open In Colab

REINFORCE learning to balance CartPole — the 100-episode rolling average climbs from ~25 to ~490, converging within about 3,000 episodes

The animation shows the agent converging to the maximum score of 500 within about 3,000 episodes, using nothing but NumPy. No deep learning framework, no replay buffer: just a policy, a gradient, and RMSProp.

import numpy as np
import gymnasium as gym

# --- Hyperparameters ---
# Original code (CartPole-v0, max 200 steps): H=100, lr=1e-4, gamma=0.95, batch_size=5
# Adapted for CartPole-v1 (max 500 steps): higher gamma and learning rate
H = 100              # hidden layer neurons
batch_size = 5       # episodes per parameter update
learning_rate = 1e-3 # RMSProp learning rate (original: 1e-4, raised for longer episodes)
gamma = 0.99         # discount factor (original: 0.95, raised for longer horizon)
decay_rate = 0.99    # RMSProp decay
epsilon = 1e-5       # RMSProp epsilon

# --- Network functions ---
def sigmoid(x):
    return 1.0 / (1.0 + np.exp(-x))

def forward(x, model):
    h = np.dot(model['W1'], x)
    h[h < 0] = 0  # ReLU
    logp = np.dot(model['W2'], h)
    p = sigmoid(logp)
    return p, h

def backward(eph, epdlogp, epx, model):
    dW2 = np.dot(eph.T, epdlogp).ravel()
    dh = np.outer(epdlogp, model['W2'])
    dh[eph <= 0] = 0  # backprop through ReLU
    dW1 = np.dot(dh.T, epx)
    return {'W1': dW1, 'W2': dW2}

def discount_rewards(r, gamma):
    """Standard full-horizon discounting."""
    discounted_r = np.zeros_like(r)
    running_add = 0
    for t in reversed(range(r.size)):
        running_add = running_add * gamma + r[t]
        discounted_r[t] = running_add
    return discounted_r

# --- Initialisation ---
env = gym.make('CartPole-v1')
observation, _ = env.reset()
D = len(observation)  # input dimension (4 for CartPole)

model = {
    'W1': np.random.randn(H, D) / np.sqrt(D),  # Xavier init
    'W2': np.random.randn(H) / np.sqrt(H),
}
grad_buffer = {k: np.zeros_like(v) for k, v in model.items()}
rmsprop_cache = {k: np.zeros_like(v) for k, v in model.items()}

# --- Training loop ---
xs, hs, dlogps, drs, episode_durations = [], [], [], [], []
episode_number = 0
t = 0

while episode_number < 5000:
    x = observation
    aprob, h = forward(x, model)
    action = 1 if np.random.uniform() < aprob else 0

    xs.append(x)
    hs.append(h)
    dlogps.append(action - aprob)  # policy gradient

    observation, reward, terminated, truncated, _ = env.step(action)
    drs.append(reward)

    if terminated or truncated:
        episode_number += 1
        episode_durations.append(t)
        t = 0

        epx = np.vstack(xs)
        eph = np.vstack(hs)
        epdlogp = np.vstack(dlogps)
        epr = np.vstack(drs)
        xs, hs, dlogps, drs = [], [], [], []

        # Discount and standardise rewards
        discounted_epr = discount_rewards(epr, gamma)
        discounted_epr -= np.mean(discounted_epr)
        std = np.std(discounted_epr)
        if std > 0:
            discounted_epr /= std

        # The PG magic: weight gradients by advantage
        epdlogp *= discounted_epr
        grad = backward(eph, epdlogp, epx, model)
        for k in model:
            grad_buffer[k] += grad[k]

        # RMSProp update every batch_size episodes
        if episode_number % batch_size == 0:
            for k, v in model.items():
                g = grad_buffer[k]
                rmsprop_cache[k] = decay_rate * rmsprop_cache[k] + (1 - decay_rate) * g**2
                model[k] += learning_rate * g / (np.sqrt(rmsprop_cache[k]) + epsilon)
                grad_buffer[k] = np.zeros_like(v)

        if episode_number % 500 == 0:
            avg = np.mean(episode_durations[-100:])
            print(f'Episode {episode_number}, 100-ep avg: {avg:.1f}')

        observation, _ = env.reset()
    t += 1

env.close()
print(f'Final 100-episode average: {np.mean(episode_durations[-100:]):.1f}')

The result: The agent converges to the 500-step maximum, using nothing but NumPy. No deep learning framework: we compute every gradient by hand.

Visualise the Learning

import matplotlib.pyplot as plt

rolling = np.convolve(episode_durations, np.ones(100)/100, mode='valid')

fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(rolling, 'b-', linewidth=0.8)
ax.axhline(y=500, color='g', linestyle='--', alpha=0.5, label='Max score (500)')
ax.set_xlabel('Episode')
ax.set_ylabel('Duration (100-episode rolling avg)')
ax.set_title('REINFORCE on CartPole-v1')
ax.set_ylim(0, 550)
ax.legend()
ax.grid(True, alpha=0.3)
plt.show()

REINFORCE on CartPole-v1 — 100-episode rolling average reward climbing from ~25 to ~490

What Just Happened?

We built a complete RL agent with no frameworks: just a policy network, a gradient, and a reward signal. Let's walk through each piece.

The Policy Network (4 → 100 → 1)

The network is a two-layer perceptron that maps a 4-dimensional CartPole state to a single probability:

State [x, ẋ, θ, θ̇]  →  Hidden (100 ReLU)  →  Output (sigmoid)  →  P(push right)
         4                    100                     1
def forward(x, model):
    h = np.dot(model['W1'], x)   # (100, 4) × (4,) → (100,)
    h[h < 0] = 0                  # ReLU activation
    logp = np.dot(model['W2'], h) # (100,) × (100,) → scalar
    p = sigmoid(logp)             # squash to [0, 1]
    return p, h

The output p is the probability of pushing right. We sample from this Bernoulli distribution:

action = 1 if np.random.uniform() < aprob else 0

This is fundamentally different from DQN's approach. DQN outputs Q-values for every action and picks the highest (deterministic, epsilon-greedy). Here, the network outputs a probability and we sample: the policy is stochastic by design. This built-in exploration means we don't need an epsilon schedule.

The Policy Gradient Signal

After taking an action, we record the "gradient that encourages the action that was taken":

dlogps.append(action - aprob)

If we pushed right (action=1) and aprob=0.7, then dlogp = 1 - 0.7 = 0.3, a positive gradient that nudges the network to make "push right" even more likely. If we pushed left (action=0) and aprob=0.7, then dlogp = 0 - 0.7 = -0.7, a negative gradient that decreases the probability of pushing right (making left more likely).

But at this point, we don't know if the action was good. That's what the reward tells us.

The PG Magic Line

This is where policy gradients really happen:

epdlogp *= discounted_epr  # modulate gradient with advantage

Every action's gradient gets multiplied by its discounted reward. Good actions (high reward) get their gradients amplified: "do more of this". Bad actions (low or negative reward) get their gradients flipped: "do less of that". This single line is the heart of REINFORCE.

Think of it like a coach watching game film: every play gets a grade. Plays that led to scoring get reinforced; plays that led to turnovers get discouraged. The magnitude of the grade determines how strongly the feedback applies.

Reward Discounting

CartPole gives +1 reward at every timestep the pole stays up. We discount future rewards so earlier actions, which contributed to a longer run, receive more credit:

def discount_rewards(r, gamma):
    discounted_r = np.zeros_like(r)
    running_add = 0
    for t in reversed(range(r.size)):
        running_add = running_add * gamma + r[t]
        discounted_r[t] = running_add
    return discounted_r

With gamma=0.99, an action taken 100 steps before the end still receives 0.99100≈0.370.99^{100} \approx 0.37 of the terminal reward. This gives the network a smooth gradient across the episode: early actions in long episodes receive higher discounted returns than the same actions in short episodes, so the network learns to favour strategies that keep the pole up for longer.

Reward Standardisation (Variance Reduction)

After discounting, we standardise the rewards to have zero mean and unit variance:

discounted_epr -= np.mean(discounted_epr)
std = np.std(discounted_epr)
if std > 0:
    discounted_epr /= std

This is critical for stable training. Without standardisation, the magnitude of the gradient depends on the absolute reward scale. With it, roughly half the actions get reinforced (above-average) and half get discouraged (below-average). It's a simple form of a baseline, one of the most important variance reduction techniques in policy gradients.

Manual Backpropagation

We compute gradients by hand, just as in the backpropagation post:

def backward(eph, epdlogp, epx, model):
    dW2 = np.dot(eph.T, epdlogp).ravel()     # gradient for output weights
    dh = np.outer(epdlogp, model['W2'])        # backprop to hidden layer
    dh[eph <= 0] = 0                           # backprop through ReLU
    dW1 = np.dot(dh.T, epx)                   # gradient for input weights
    return {'W1': dW1, 'W2': dW2}

The chain rule flows backwards: output layer gradient → hidden layer gradient (masked by ReLU) → input layer gradient. This is identical to standard backprop, except the "loss" is the policy gradient signal (epdlogp already weighted by advantage).

Batch Gradient Accumulation

Rather than updating after every episode, we accumulate gradients over batch_size=5 episodes:

for k in model:
    grad_buffer[k] += grad[k]

if episode_number % batch_size == 0:
    # RMSProp update using accumulated gradients
    ...

This reduces gradient variance: each update reflects 5 episodes worth of experience rather than just one. It's the same idea as minibatch gradient descent in supervised learning.

RMSProp from Scratch

The optimiser is RMSProp, implemented manually:

rmsprop_cache[k] = decay_rate * rmsprop_cache[k] + (1 - decay_rate) * g**2
model[k] += learning_rate * g / (np.sqrt(rmsprop_cache[k]) + epsilon)

RMSProp maintains a running average of squared gradients and divides by their root. This gives adaptive per-parameter learning rates: parameters with consistently large gradients get smaller effective learning rates, and vice versa. The decay_rate=0.99 means the running average looks back roughly 100 updates.

Going Deeper

The Policy Gradient Theorem

The goal of policy gradients is to maximise expected cumulative reward:

J(θ)=Eτ∼πθ[∑t=0Tγtrt]J(\theta) = \mathbb{E}_{\tau \sim \pi_\theta}\left[\sum_{t=0}^{T} \gamma^t r_t\right]

Where τ\tau is a trajectory (sequence of states and actions) sampled under policy πθ\pi_\theta. The policy gradient theorem (Sutton et al., 1999) tells us how to compute the gradient of this objective:

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

Where Gt=∑k=tTγk−trkG_t = \sum_{k=t}^{T} \gamma^{k-t} r_k is the return from timestep tt.

The intuition: ∇θlog⁡πθ(at∣st)\nabla_\theta \log \pi_\theta(a_t | s_t) points in the direction that makes action ata_t more likely. Multiplying by GtG_t scales this: if the return was high, push hard in that direction; if low, push the other way.

The Log-Likelihood Trick

How do we differentiate through sampling? We can't backpropagate through np.random.uniform(). The score function estimator (also called the log-likelihood trick or REINFORCE trick) sidesteps this.

For a Bernoulli policy with probability pp:

∇θlog⁡πθ(a∣s)=a−pp(1−p)⋅∇θp\nabla_\theta \log \pi_\theta(a | s) = \frac{a - p}{p(1-p)} \cdot \nabla_\theta p

But in practice, we use a simpler form. The gradient of the log-likelihood for a sigmoid output is just a−pa - p:

dlogps.append(action - aprob)  # this IS the score function

This is action - aprob, exactly what the code computes. When action=1 and aprob=0.7, the gradient is 0.3: "make right more likely." This gradient gets backpropagated through the network to update all weights.

Why Reward Standardisation Reduces Variance

REINFORCE is an unbiased estimator of the policy gradient, but it has high variance. A single trajectory might get lucky or unlucky, leading to noisy gradient estimates.

Subtracting a baseline bb from the return doesn't change the expected gradient (it's still unbiased) but can dramatically reduce variance:

∇θJ(θ)=E[∇θlog⁡πθ(at∣st)⋅(Gt−b)]\nabla_\theta J(\theta) = \mathbb{E}\left[\nabla_\theta \log \pi_\theta(a_t | s_t) \cdot (G_t - b)\right]

Our code uses the mean reward as the baseline:

discounted_epr -= np.mean(discounted_epr)

With this baseline, actions that performed better than average get reinforced, and below-average actions get discouraged. Without it, if all rewards are positive (as in CartPole, where every alive step gives +1), the gradient would reinforce all actions, just some more than others. The baseline makes the signal much cleaner.

Hyperparameters

ParameterWhat it controlsValueOriginal (v0)Why changed
HHidden neurons100100Unchanged: enough for CartPole
learning_rateRMSProp step size1e-31e-4Longer episodes → more samples per update → can use larger steps
gammaDiscount factor0.990.95Longer episodes (500 vs 200) need longer-horizon credit assignment
batch_sizeEpisodes per update55Unchanged
decay_rateRMSProp memory0.990.99Unchanged
epsilonRMSProp stability1e-51e-5Unchanged

The original code targeted CartPole-v0 (max 200 steps) and used a custom "blame window" that penalised only the last N=10 actions. This worked well for short episodes but created a performance ceiling on CartPole-v1 (max 500 steps): as episodes get longer, the blame signal becomes proportionally smaller and gets washed out after standardisation.

The fix is textbook REINFORCE: standard full-horizon discounting with gamma=0.99. The higher gamma ensures actions 100+ steps before the terminal state still receive meaningful credit. The higher learning rate (1e-3) compensates for the longer episodes providing more gradient samples per update.

Learning rate is critical. Too high and the policy oscillates wildly. Too low and learning crawls. With standard discounting and gamma=0.99, lr=1e-3 converges in ~3,000 episodes.

Value-Based vs Policy-Based: When to Use Each

DQN (Value-Based)REINFORCE (Policy-Based)
OutputsQ-values for each actionAction probabilities
Action selectionArgmax (deterministic)Sample (stochastic)
ExplorationEpsilon-greedy (bolted on)Built-in (stochastic policy)
Action spacesDiscrete onlyDiscrete and continuous
Sample efficiencyHigher (replay buffer)Lower (on-policy, no replay)
StabilityNeeds replay + target netNeeds variance reduction
ConvergenceTo optimal Q → optimal policyDirectly to (locally) optimal policy

Use policy gradients when:

  • The action space is continuous (robotics, continuous control)
  • You want a stochastic policy (exploration, multi-modal strategies)
  • The policy is simpler than the value function

Use DQN when:

  • The action space is small and discrete
  • Sample efficiency matters (you can't run millions of episodes)
  • You have access to a simulator for experience replay

When NOT to Use Vanilla REINFORCE

  1. High-dimensional action spaces: Variance becomes unmanageable. Use actor-critic methods (A2C, PPO) that learn a value function baseline
  2. Sample-scarce settings: REINFORCE is on-policy: every trajectory is used once then discarded. Off-policy methods like DQN are far more sample-efficient
  3. Long episodes: The credit assignment problem worsens. Which of the 10,000 actions caused success? Actor-critic methods handle this better with per-step value estimates
  4. When stability matters: Vanilla REINFORCE can have large policy swings. PPO clips the gradient to prevent catastrophic updates

The Foundational Papers

Williams (1992): The REINFORCE Paper

The REINFORCE algorithm was introduced by Ronald J. Williams in his 1992 paper Simple Statistical Gradient-Following Algorithms for Connectionist Reinforcement Learning. This is one of the foundational papers in policy gradient methods.

Williams framed the problem precisely: given a stochastic network (he called it a "connectionist network") that produces actions probabilistically, how do we adjust the weights to maximise expected reward?

His key result:

"For each such algorithm, convergence to at least a local maximum in expected reinforcement is assured by a simple condition on the sequence of values used to scale the gradient estimate."

The REINFORCE update rule from the paper:

Δwij=αij(r−bij)∂ln⁡gi∂wij\Delta w_{ij} = \alpha_{ij}(r - b_{ij}) \frac{\partial \ln g_i}{\partial w_{ij}}

Where:

  • wijw_{ij}: weight from unit jj to unit ii
  • αij\alpha_{ij}: learning rate
  • rr: the reinforcement (reward)
  • bijb_{ij}: a reinforcement baseline (reduces variance without introducing bias)
  • gig_i: the probability of the output of unit ii

In our code, this maps directly:

  • ∂ln⁡gi∂wij\frac{\partial \ln g_i}{\partial w_{ij}} → dlogps (the score function, computed via backprop)
  • r−bijr - b_{ij} → discounted_epr (standardised, which implicitly subtracts a mean baseline)
  • The product epdlogp *= discounted_epr is the REINFORCE update

Williams proved that this estimator is unbiased: in expectation, it equals the true policy gradient regardless of the baseline bb. The baseline only affects variance; choosing it well (e.g., as the mean reward) can dramatically speed convergence.

Sutton, McAllester, Singh & Mansour (1999): The Policy Gradient Theorem

The policy gradient theorem, proved in Policy Gradient Methods for Reinforcement Learning with Function Approximation, generalised Williams' result to arbitrary function approximators:

∇θJ(θ)=∑sdπ(s)∑a∇θπθ(a∣s)Qπ(s,a)\nabla_\theta J(\theta) = \sum_s d^{\pi}(s) \sum_a \nabla_\theta \pi_\theta(a|s) Q^{\pi}(s,a)

Where dπ(s)d^{\pi}(s) is the stationary state distribution under policy π\pi. The theorem shows that the policy gradient doesn't depend on the gradient of the state distribution, a surprising and essential result that makes policy gradient methods practical.

Karpathy (2016): Deep RL: Pong from Pixels

The original code was inspired by Andrej Karpathy's influential blog post Deep Reinforcement Learning: Pong from Pixels. Karpathy demonstrated that a simple two-layer network trained with REINFORCE could learn to play Atari Pong directly from raw pixels, all in ~130 lines of Python.

The key architectural choices we inherit:

  • Two-layer network with ReLU hidden layer and sigmoid output
  • Manual NumPy backprop: no framework dependency
  • RMSProp as the optimiser
  • Reward discounting with standardisation

Karpathy's version used the raw Pong pixel difference as input (preprocessed frames). Our CartPole version uses the 4-dimensional state vector directly, but the algorithm is identical.

The REINFORCE Algorithm (Pseudocode)

From Sutton & Barto (2018), Section 13.3:

Initialize policy parameters θ arbitrarily
For each episode:
    Generate trajectory τ = (s₀, a₀, r₁, s₁, a₁, ..., sT) following π_θ
    For each step t = 0, 1, ..., T-1:
        Gₜ ← Σ_{k=t+1}^{T} γ^{k-t-1} rₖ       (return from step t)
        θ ← θ + α γ^t Gₜ ∇_θ ln π_θ(aₜ|sₜ)    (policy gradient update)

Our Implementation vs the Theory

Theory (Williams, 1992)Our code
∂ln⁡gi∂wij\frac{\partial \ln g_i}{\partial w_{ij}} (score function)action - aprob backpropagated through network
r−bijr - b_{ij} (reward minus baseline)discounted_epr after mean subtraction
Δw=α(r−b)∇ln⁡π\Delta w = \alpha (r-b) \nabla \ln \piepdlogp *= discounted_epr then backward()
Single-sample updateBatch of 5 episodes
Generic optimiserRMSProp (adaptive learning rates)

What Came After

REINFORCE spawned the entire family of modern policy gradient methods:

Historical Context

  • Williams (1992). REINFORCE: the first general-purpose policy gradient algorithm
  • Sutton et al. (1999). Policy gradient theorem: proves the gradient formula for function approximators
  • Kakade (2001). Natural policy gradients: use Fisher information for better updates
  • Schulman et al. (2015). TRPO: trust regions for policy optimisation
  • Schulman et al. (2017). PPO: simplified TRPO with clipped objective; now the industry standard
  • OpenAI (2017). RLHF: policy gradients for aligning language models

Further Reading

  • Williams (1992): Simple Statistical Gradient-Following Algorithms for Connectionist Reinforcement Learning, the REINFORCE paper
  • Sutton et al. (1999): Policy Gradient Methods for Reinforcement Learning with Function Approximation, the policy gradient theorem
  • Karpathy (2016): Deep Reinforcement Learning: Pong from Pixels, the blog post that inspired the original code
  • Sutton & Barto (2018): Chapter 13 (Policy Gradient Methods), freely available online

Interactive Tools

  • Q-Learning Visualiser: Compare value-based RL with the policy gradient approach covered in this post
  • Multi-Armed Bandit Simulator: Exploration without state. Thompson sampling gets its exploration from posterior uncertainty rather than from an entropy bonus

Try It Yourself

The interactive notebook includes exercises:

  1. Remove reward shaping: Replace discount_rewards with standard full-horizon discounting. How does training speed change?
  2. Vary the blame window: Try N∈{5,10,20,50}N \in \{5, 10, 20, 50\}. How does the learning curve respond?
  3. Remove standardisation: Comment out the mean/std normalisation of rewards. Does the agent still learn?
  4. Gamma sweep: Try γ∈{0.8,0.95,0.99,0.999}\gamma \in \{0.8, 0.95, 0.99, 0.999\}. How does the discount factor affect learning?
  5. Compare with DQN: Plot the REINFORCE learning curve alongside DQN's on the same axes. Which learns faster? Which is more stable?

REINFORCE showed that you can optimise a policy directly, no Q-values needed. The gradient signal is noisy, but with variance reduction (baselines, reward standardisation, batch updates) it works remarkably well. The same core idea, gradient ascent on expected reward, underpins every modern policy gradient algorithm, from PPO to the RLHF that aligns large language models. The fundamentals haven't changed since Williams (1992); only the variance reduction has gotten better.

Stay in the loop

Get notified when new tutorials and deep-dives are published. No spam, unsubscribe anytime.

Related Posts

Frequently Asked Questions