Evaluation lab · San Francisco · London · Zürich
Reinforced Labs Request a briefing
Reinforcement learning

The REINFORCE algorithm: derivation, pseudocode, numpy code and its variance problem

REINFORCE is the Monte Carlo policy gradient: run an episode, then make each action more likely in proportion to the return that followed it. We implement it in 30 lines of numpy and measure its weakness: one episode's gradient points the wrong way 36% to 49% of the time.

Part of Reinforcement learning. New to the topic? Start with What is reinforcement learning? A guide from gridworlds to language models.

The REINFORCE algorithm is the simplest policy gradient method: run the current policy for a full episode, compute the return that followed each action, and nudge the policy's parameters so that actions followed by high returns become more likely. Introduced by Ronald Williams in 1992 [1], it is an unbiased Monte Carlo estimate of the policy gradient, which is why it works and also why it is slow: its gradient estimates are extremely noisy.

This page derives the update, gives pseudocode and a short numpy implementation you can run, and then measures the variance problem directly. On a three-state task, a single episode's gradient estimate points the wrong way 36% to 49% of the time, and the step-size range where plain REINFORCE learns without collapsing is narrow. Every number below comes from seeded runs we did for this article.

A task that needs a stochastic policy#

We use the short corridor from Sutton and Barto's textbook (Example 13.1) [2]. There are three non-terminal states in a row and a goal on the right. Every step costs −1, so the return is minus the episode length. Moving right in the first and third states moves right, but in the middle state the actions are swapped: "right" goes left and "left" goes right. The catch is that the agent cannot tell the states apart. It sees the same features everywhere, so its policy is a single number, the probability of pressing "right".

A deterministic policy fails completely. Always pressing right bounces between the first two states forever; always pressing left never leaves the first state. A value-based agent that acts -greedily with is stuck with or , worth −82.1 and −44.2. The best stochastic policy presses right with probability and has expected return −11.66. Finding that probability is exactly what a policy gradient method is for. (The policy gradient theorem page explains why stochastic, parameterized policies are the natural object here, and the Q-learning page covers the value-based alternative.)

The policy has one parameter, the logit , with . For a two-action logistic policy the score has a simple form: , where for right and 0 for left. So each right press contributes times its return and each left press contributes times its return.

Deriving the REINFORCE update#

The objective is the expected return . The policy gradient theorem [3], derived in full on its own page, gives

where is the episode length, the reward at step , the discount factor, and the return from step onward. Two facts produce it: the log-derivative trick , which turns the gradient of an expectation into an expectation, and the fact that the environment's transition probabilities do not depend on , so they drop out. Because the expectation is over the policy's own episodes, one sampled episode gives an unbiased estimate, and REINFORCE simply follows it:

Read it as weighted maximum likelihood. If you set every to 1 you would be doing supervised learning on your own actions. REINFORCE weights each action's log-likelihood gradient by the return that followed it, so actions in good episodes are imitated and actions in bad ones are pushed away. In the corridor and the task is episodic, so , minus the number of steps still to go.

Williams's original form#

Williams wrote the update for a single unit as , with a learning-rate factor , a reinforcement offset by a baseline , and the characteristic eligibility , the derivative of the log-probability of the unit's output [1]. That is where the acronym comes from. He proved that the expected update lies in the direction of the gradient of expected reward for any baseline that does not depend on the action, and he described an episodic version that accumulates eligibilities over an episode. Modern presentations, including Sutton and Barto's [2], are the same algorithm in trajectory notation. The baseline was part of the algorithm from the start; we look at what happens without it first, and the baselines article covers what it buys.

REINFORCE pseudocode#

  1. Initialize policy parameters (here, , a policy that rarely presses right).
  2. Repeat for each episode:
    1. Run until the episode ends, recording .
    2. For down to 0, compute the return-to-go , with .
    3. Accumulate , using the parameters the episode was collected with.
    4. Update .

Sutton and Barto's boxed version updates after every step inside the episode; computing the whole episode's gradient at the old parameters and applying it once, as above, is the more common implementation and is exactly an unbiased gradient step. In practice you average over a batch of episodes before stepping, which is the same estimator with lower variance.

A numpy implementation#

The code below is the complete experiment. It runs as plain Python with numpy, in a few seconds.

import numpy as np

# Short corridor (Sutton and Barto, Example 13.1): states 0, 1, 2; state 3 is the goal.
# Action 1 = "right", 0 = "left". In state 1 the actions are swapped. Reward -1 per step.
NEXT = np.array([[0, 1],    # state 0: left stays, right -> 1
                 [2, 0],    # state 1: left -> 2, right -> 0 (reversed)
                 [1, 3]])   # state 2: left -> 1, right -> goal

def run_episode(p_right, rng, max_steps=10_000):
    s, actions = 0, []
    while s != 3 and len(actions) < max_steps:
        a = int(rng.random() < p_right)
        actions.append(a)
        s = NEXT[s, a]
    return np.array(actions)

def reinforce(alpha, episodes, seed, p0=0.05):
    rng = np.random.default_rng(seed)
    theta = np.log(p0 / (1 - p0))            # logit of "right"; same in every state
    returns = []
    for _ in range(episodes):
        p = 1 / (1 + np.exp(-theta))
        acts = run_episode(p, rng)
        T = len(acts)
        G = -(T - np.arange(T))               # reward-to-go: -1 per remaining step
        score = acts - p                      # d log pi(a) / d theta for a Bernoulli policy
        theta += alpha * np.sum(G * score)    # Monte Carlo policy gradient step
        returns.append(-T)
    return np.array(returns), 1 / (1 + np.exp(-theta))

if __name__ == "__main__":
    R = np.array([reinforce(2 ** -11, 1000, seed)[0] for seed in range(20)])
    print("mean return, first 100 episodes:", R[:, :100].mean().round(2))
    print("mean return, last 100 episodes: ", R[:, -100:].mean().round(2))

Run as shown, it prints a mean return of −36.69 over the first 100 episodes and −11.62 over the last 100 (20 seeds), against an optimum of −11.66. The last-100 average can sit slightly above the optimum by chance because individual episodes are noisy.

Setup (simulation). To mirror the textbook's Figure 13.1 we ran 100 independent runs of 1,000 episodes at three step sizes, each starting from . We also ran the version with a learned baseline (a single scalar , updated by at every step) at a larger step size.

VariantMean return, episodes 1 to 100Episodes 101 to 200Last 100 episodesFinal , mean (s.d.)
REINFORCE, −39.2−18.3−11.750.559 (0.048)
REINFORCE, −50.4−26.6−12.480.469 (0.033)
REINFORCE, −60.6−38.3−15.020.342 (0.023)
With baseline, −22.8−12.1−11.940.589 (0.079)
-90-70-50-30-1002004006008001,000optimum -11.66EpisodeMean return (100 runs, 20-episode moving average)α = 2^-112^-8 + baselineα = 2^-12α = 2^-13
Figure 1. Mean return per episode over 100 seeded runs (20-episode moving average) on the short corridor. Smaller step sizes learn more slowly; after 1,000 episodes the runs have only reached . The baseline version, at an eight-times larger step size, is near the optimum within about 50 episodes.

REINFORCE works. Every run at every step size moves toward the optimal stochastic policy, as the theory promises. But the slower step sizes are painfully slow on a problem with one parameter, and the obvious fix, a larger step size, runs straight into the variance problem.

Watch REINFORCE learn#

The figure reruns the same algorithm in your browser: 20 runs of 500 episodes on the corridor, starting at . The top panel shows each run's return and the mean; the bottom panel plots where each run's policy ended on the exact return curve . Move the step size and toggle the baseline.

Return per episode (20 runs; thin = one run, thick = mean)0-25-50-75-100optimum -11.660100200300400500Episode (20-episode moving average; returns below -100 drawn at -100)Exact J(p) and where each run ended0-30-6000.250.50.751Probability of "right", p
2^-11

Figure 2 (interactive). REINFORCE on the short corridor, computed live with the article's update rule. The static version shows without a baseline. Episodes are capped at 2,000 steps in the browser. At and above, some runs without a baseline jump to a near-deterministic policy and stop reaching the goal; with the baseline, runs at still learn, and collapses begin around .

Why REINFORCE has high variance#

Unbiased is not the same as useful. To see how noisy a single episode's gradient estimate is, we froze the policy at several values of , ran 20,000 episodes at each, and compared the one-episode estimate with the exact derivative computed from the closed-form .

Exact Mean episode lengthStd. dev. of one-episode estimateSignal-to-noiseOne-episode estimates with the wrong signEpisodes to get the sign right 95% of the time
0.0575.982.22240.3435.6%24
0.2015.522.558.60.2641.5%39
0.404.6713.330.80.1541.9%118
0.502.0012.127.00.07448.8%492
0.80−7.0014.833.50.2146.0%62

The last column uses the normal approximation . Even far from the optimum, where the gradient is large, the noise is three to four times the signal. Near the optimum the true gradient shrinks while the noise does not, so the signal-to-noise ratio collapses: at , still short of the best policy, you need about 500 episodes before a batch average reliably points the right way. Where does that noise come from?

  • The return is a sum of many random terms. adds up every future reward, and each depends on every future random action. Its variance grows with the horizon. In the corridor, episode lengths at range from 3 steps to several hundred.
  • Credit is smeared across the episode. Every action in an episode is weighted by roughly the same large return. Good and bad actions in a long episode are blamed together, and only averaging over many episodes separates them.
  • There is no reference point. All returns in this task are negative, so every action's log-probability is pushed down after every episode. Only the difference in how hard it is pushed carries information. Adding a constant to every reward does not change the expected update but can increase its variance without limit. That is the problem a baseline solves; Greensmith, Bartlett and Baxter analysed baselines as control variates and derived the variance-optimal one [4].

The step-size trap#

Noise sets an upper limit on the step size. We swept the step size over 40 seeds of 500 episodes each, with episodes capped at 5,000 steps.

Step size No baseline: mean return, last 100 episodesWith baseline
−11.69−11.95
−12.47−11.71
−262.5−12.07
−3,129−137.6
−5,000 (every run stuck)−1,680

The failure mode is not slow learning but collapse. One unlucky long episode produces a huge update that drives toward 0 or 1. Both extremes are deterministic policies that never reach the goal, so the next episodes are very long, their score terms are tiny, and the policy has no way back. With a baseline the safe range extends by about a factor of two: still learns, and collapse starts at . The general lesson carries over to large models: a policy gradient step that is too large can move the policy to a region where it no longer collects the data needed to undo the mistake, which is the motivation for the trust regions in TRPO and PPO.

REINFORCE vs actor-critic#

REINFORCE uses the actual sampled return . Actor-critic methods replace it with an estimate built from a learned value function, for example the one-step temporal-difference error . The trade is bias for variance: a critic's estimate is far less noisy than a Monte Carlo return but is wrong wherever the critic is wrong. Actor-critic methods can also update before an episode ends, which matters for long or continuing tasks. Generalized advantage estimation interpolates between the two with a single parameter ; at it is REINFORCE with a value baseline. The actor-critic and GAE article measures that trade-off directly.

REINFORCEREINFORCE with baselineActor-critic
Weight on TD error or GAE
BiasNoneNoneYes, from critic error
VarianceHighestLowerLowest
Needs full episodesYesYesNo
Learned componentsPolicyPolicy and baselinePolicy and critic

Where REINFORCE shows up today#

REINFORCE is rarely the first choice for control tasks. In a benchmark of continuous-control algorithms, Duan and colleagues found it effective on basic and locomotion tasks but prone to premature convergence to local optima, which left it behind natural-gradient methods on some tasks [6]. It has had a second life in reinforcement learning for language models, where an "episode" is one generated response and the reward arrives once at the end. There, the costs of a learned critic are high, and Ahmadian and colleagues reported that REINFORCE-style estimators with a sampled baseline, such as the leave-one-out average of other responses to the same prompt, matched or beat PPO for RLHF [5]. Group-relative methods used for reinforcement learning with verifiable rewards are close relatives: REINFORCE with a baseline computed from a group of samples. The variance analysis on this page applies to them directly.

Making REINFORCE work in practice#

  1. Use reward-to-go and a baseline from the start. Both are free of bias. The baseline alone doubled the largest stable step size in our sweep.
  2. Average over a batch of episodes. Variance falls as . Near the optimum, our corridor needed hundreds of episodes for a reliable sign.
  3. Tune the step size on a log grid and watch for collapse. Track the policy's entropy; a sudden drop to zero entropy with falling returns is the signature of the step-size trap.
  4. Normalize or clip returns when their scale is unknown. Scaling returns to unit variance makes one step size work across tasks; it does not change the update direction within a batch.
  5. Use several seeds when comparing variants. With estimates this noisy, a single run is an anecdote. See how to evaluate RL agents.
  6. Move to a critic when episodes are long. Variance grows with the horizon; at some length an actor-critic method or GAE will learn faster per sample.

Failure modes to recognize#

  • Collapse to a deterministic policy after one large update, as in the step-size sweep. Policies with zero probability on an action can never learn that it was good.
  • Endless episodes when a degenerate policy cannot terminate. Always cap episode length and log how often the cap is hit.
  • Reward scale sensitivity. Multiplying rewards by 10 multiplies the effective step size by 10. Adding a constant to rewards inflates variance unless you subtract a baseline.
  • Stale data. REINFORCE is on-policy. Reusing episodes from older parameters without importance weighting estimates the wrong gradient.

Frequently asked questions#

What is the REINFORCE algorithm?

REINFORCE is a policy gradient method introduced by Ronald Williams in 1992. After each episode it increases the log-probability of every action taken in proportion to the return that followed it. That update is an unbiased sample of the gradient of expected return, so REINFORCE performs stochastic gradient ascent on the policy's performance without a model of the environment.

What does REINFORCE stand for?

Williams named it for the form of the update: REward Increment = Nonnegative Factor times Offset Reinforcement times Characteristic Eligibility. The nonnegative factor is the step size, the offset reinforcement is the reward minus a baseline, and the characteristic eligibility is the derivative of the log-probability of the chosen action.

Why does REINFORCE have high variance?

It weights each action by a Monte Carlo return that sums many random future rewards, it gives all actions in an episode similar credit, and without a baseline it has no reference point. On our three-state task the noise in a one-episode gradient estimate was 3 to 14 times the size of the true gradient.

Is REINFORCE on-policy or off-policy?

On-policy. The gradient is an expectation over episodes generated by the current policy, so each batch of episodes supports one update and is then discarded. Reusing older episodes requires importance weights, which add variance; trust-region methods such as PPO exist largely to reuse a batch for several careful steps.

What is the difference between REINFORCE and actor-critic?

REINFORCE weights the score by the actual sampled return. Actor-critic methods replace the return with an estimate from a learned value function, such as the temporal-difference error. That lowers variance and allows updates before an episode ends, at the cost of bias wherever the critic is wrong.

How do I choose the step size for REINFORCE?

Search on a log scale and watch for collapse. In our sweep on the short corridor, plain REINFORCE learned at step sizes up to 2^-8 but collapsed to non-terminating deterministic policies at 2^-7 and above. A learned baseline doubled the largest stable step size. Normalizing returns makes a single step size transfer across tasks.

Is REINFORCE used to train large language models?

REINFORCE-style estimators are used in RL fine-tuning of language models, where one generated response is an episode with a single reward. Methods that subtract a baseline computed from other sampled responses to the same prompt, such as leave-one-out REINFORCE and group-relative methods, are close relatives of Williams's algorithm with a baseline.

Sources#

  1. R. J. Williams. Simple statistical gradient-following algorithms for connectionist reinforcement learning, 1992. doi.org
  2. R. S. Sutton, A. G. Barto. Reinforcement Learning: An Introduction (2nd ed.), 2018. incompleteideas.net
  3. R. S. Sutton, D. McAllester, S. Singh, Y. Mansour. Policy Gradient Methods for Reinforcement Learning with Function Approximation, 1999. proceedings.neurips.cc
  4. E. Greensmith, P. L. Bartlett, J. Baxter. Variance Reduction Techniques for Gradient Estimates in Reinforcement Learning, 2004. jmlr.org
  5. A. Ahmadian, C. Cremer, M. Gallé, M. Fadaee, J. Kreutzer, O. Pietquin, A. Üstün, S. Hooker. Back to Basics: Revisiting REINFORCE Style Optimization for Learning from Human Feedback in LLMs, 2024. arxiv.org
  6. Y. Duan, X. Chen, R. Houthooft, J. Schulman, P. Abbeel. Benchmarking Deep Reinforcement Learning for Continuous Control, 2016. arxiv.org

Know the number before you ship.

A 45-minute working session with an evaluation engineer. Bring one use case; leave with a threat model, a draft eval plan with sample sizes, and an honest read on where you stand.