Search

Search papers and lessons

REINFORCE

Level 4 · 13 min read

REINFORCE trains a policy by running it for whole episodes and nudging the probability of each chosen action in proportion to the return that followed it.

On this page

  • Colour = topic mix
  • Probability
  • Discrete math
  • Calculus
  • Linear algebra
  • Reinforcement learning
  • Inference
  • ReadReady to readNot yet

TL;DR

REINFORCE is the simplest policy gradient algorithm that works in practice. It runs the current Policy (Reinforcement Learning) for a whole episode, records each action taken and the return that followed it, and then moves the policy's parameters so that actions followed by high return become more likely and actions followed by low return become less likely. It needs no model of the environment and no learned value estimate. The policy is the only thing it learns.

Builds on: Policy Gradient, Discounted Return, Score Function, Monte Carlo Method · Leads to: Actor-Critic Methods, PPO

Where it's used

Small control problems. An agent learns to move through a grid toward a goal square and to avoid a pit. Each episode ends at the goal or the pit, and the reward is 1 at the goal and 0 elsewhere. REINFORCE is a natural first method here: the policy is a table of action probabilities per cell, every episode produces a full set of returns, and the code is a few dozen lines.

Robot control in simulation. A simulated arm learns to reach a target. The policy outputs the mean and spread of a Gaussian over joint torques, and REINFORCE raises the probability of torque samples that led to a good final reach.

Fine-tuning language models with a scalar reward. A model writes a response, a separate Reward Model scores it, and the update raises the probability of the tokens in responses that scored well. Some methods use REINFORCE-style updates with a baseline built from other responses to the same prompt, which avoids training a separate value network.

Choosing among items. A recommender shows one of several items, and the user's click is the reward. A Softmax policy over the items is updated after each click, as in Example 3 below.

How it works

Take the smallest possible case: one step, one action, and one parameter θ\theta. The policy chooses action 1 with probability p=σ(θ)=1/(1+e−θ)p = \sigma(\theta) = 1/(1 + e^{-\theta}), where σ\sigma is the sigmoid and θ\theta is the single parameter, and action 0 with probability 1−p1 - p. Start at θ=0\theta = 0, so p=0.5p = 0.5. Use a learning rate α=0.1\alpha = 0.1. The sampled episode is: action 1, then reward 2.

−2−1.5−1−0.500.511.5200.20.40.60.81thetaprobability of action 1
  • probability of action 1
Each episode nudges theta. The two marked points are the start (0.5) and the point after one update (about 0.525).
  1. Start with the policy. θ=0\theta = 0, so p=0.5p = 0.5: both actions are equally likely. Check: σ(0)=1/(1+1)=0.5\sigma(0) = 1/(1 + 1) = 0.5.
  2. Run one episode. The policy samples action 1. The environment returns reward 2 and ends. Because nothing comes after that reward, the return from this step is G=2G = 2. Check: a one-step episode has a return equal to its only reward.
  3. Compute the score of the action taken. The score is ∇θlog⁡p\nabla_\theta \log p for the action that was sampled. For action 1 it is 1−p=0.51 - p = 0.5. Check: the score is positive, because raising θ\theta raises the chance of action 1.
  4. Weight the score by the return. Multiply: 0.5×2=1.00.5 \times 2 = 1.0. A good return turns the score into a push toward the action. Check: a return of zero would give no push at all.
  5. Step the parameter. θ←θ+α×1.0=0+0.1=0.1\theta \leftarrow \theta + \alpha \times 1.0 = 0 + 0.1 = 0.1. Check: θ\theta moved in the direction of action 1.
  6. Read the new probability. p=σ(0.1)≈0.525p = \sigma(0.1) \approx 0.525, up from 0.50.5 by a small step. Check: one episode moves the policy a little, not all the way.
  7. Repeat with new episodes. Some episodes will return less than the one above, and they push back. The average of many such pushes is the real gradient. A single episode is noisy; many episodes make it reliable.
flowchart LR
  P[Current policy] --> E[Run a full episode]
  E --> R[Compute the return after each step]
  R --> S[Score each chosen action]
  S --> W[Weight each score by its return]
  W --> U[Average and step theta]
  U --> P

The math

Definition

Definition: REINFORCE update

Let πθ(a∣s)\pi_\theta(a \mid s) be a policy with parameters θ\theta, and let an episode produce actions ata_t in states sts_t and returns GtG_t from each step onward. The REINFORCE gradient estimate is

g^=∑t∇θlog⁡πθ(at∣st) Gt,\hat{g} = \sum_{t} \nabla_\theta \log \pi_\theta(a_t \mid s_t)\, G_t,

and the update with learning rate α\alpha is

θ←θ+α g^.\theta \leftarrow \theta + \alpha\, \hat{g}.

The estimate is averaged over several episodes in practice. The update is gradient ascent on the expected return, because the sign is plus.

Step 1 in symbols: the sampled episode

The episode is generated by the policy itself, so the actions ata_t are random draws from πθ(⋅∣st)\pi_\theta(\cdot \mid s_t). The return Gt=∑k≥0γkrt+kG_t = \sum_{k \ge 0} \gamma^k r_{t+k} is the discounted return from step tt. With γ=1\gamma = 1 and a short episode, it is just the sum of the remaining rewards.

Step 2 in symbols: the score

For a single-parameter logistic policy p=σ(θ)p = \sigma(\theta), the derivative of log⁡σ(θ)\log \sigma(\theta) is σ′(θ)/σ(θ)=1−σ(θ)\sigma'(\theta)/\sigma(\theta) = 1 - \sigma(\theta), and the derivative of log⁡(1−σ(θ))\log(1 - \sigma(\theta)) is −σ(θ)-\sigma(\theta). So the score of action 1 is 1−p1 - p, and the score of action 0 is −p-p. The walkthrough's 0.50.5 is 1−0.51 - 0.5.

For a softmax policy over several actions, πθ(a)=eθa/∑keθk\pi_\theta(a) = e^{\theta_a} / \sum_k e^{\theta_k}, taking the log gives log⁡πθ(a)=θa−log⁡∑keθk\log \pi_\theta(a) = \theta_a - \log \sum_k e^{\theta_k}. Differentiating with respect to θj\theta_j gives

∇θjlog⁡πθ(a)=1[j=a]−πθ(j),\nabla_{\theta_j} \log \pi_\theta(a) = \mathbf{1}[j = a] - \pi_\theta(j),

where 1[j=a]\mathbf{1}[j = a] is 11 when jj equals aa and 00 otherwise. The sampled action gets a push up, and every other action gets a small push down, in proportion to its probability.

Step 3 in symbols: the weighting

Each score is multiplied by the return GtG_t from its own step. This is the causal part: the return counts only rewards from step tt onward, because an action cannot change rewards that came before it. The product of a score and a return is the per-step contribution to g^\hat{g}.

Step 4 in symbols: why the estimate is correct

The Policy Gradient lesson shows that the expected value of this sum equals the gradient of the expected return, E[g^]=∇θJ(θ)\mathbb{E}[\hat{g}] = \nabla_\theta J(\theta). The estimate is unbiased, which means that averaged over many episodes it points the right way. Its variance is large, which is the reason for the tricks in the next sections.

Step 5 in symbols: the step

The update is the usual gradient step from Gradient Descent, with the sign flipped because we maximise return rather than minimise a loss.

Worked examples

Example
Example 1: one step, reward 1, the other action

Setup: θ=0\theta = 0, α=0.1\alpha = 0.1, γ\gamma irrelevant (one step). The policy samples action 0 and receives reward 1.

  1. The return is G=1G = 1.
  2. The score of action 0 is −p=−0.5-p = -0.5.
  3. The contribution is −0.5×1=−0.5-0.5 \times 1 = -0.5.
  4. The update is θ=0+0.1×(−0.5)=−0.05\theta = 0 + 0.1 \times (-0.5) = -0.05.
  5. The new probability of action 1 is σ(−0.05)≈0.4875\sigma(-0.05) \approx 0.4875, so the probability of action 0 rises to about 0.51250.5125.

Interpretation: the sampled action earned a positive reward, so it became more likely. The sign of the score was negative only because action 0 corresponds to the other side of the logistic.

Example
Example 2: two steps with discounting

Setup: θ=0\theta = 0, α=0.1\alpha = 0.1, γ=0.5\gamma = 0.5. The episode has two steps. At step 0 the policy chooses action 0 and the reward is 2. At step 1 it chooses action 1 and the reward is 6.

  1. The return from step 0 is G0=r0+γr1=2+0.5×6=5G_0 = r_0 + \gamma r_1 = 2 + 0.5 \times 6 = 5.
  2. The return from step 1 is G1=r1=6G_1 = r_1 = 6.
  3. The score at step 0 is the score of action 0, which is −0.5-0.5. Its contribution is −0.5×5=−2.5-0.5 \times 5 = -2.5.
  4. The score at step 1 is the score of action 1, which is 0.50.5. Its contribution is 0.5×6=3.00.5 \times 6 = 3.0.
  5. The total is g^=−2.5+3.0=0.5\hat{g} = -2.5 + 3.0 = 0.5.
  6. The update is θ=0+0.1×0.5=0.05\theta = 0 + 0.1 \times 0.5 = 0.05.
  7. The new probability of action 1 is σ(0.05)≈0.5125\sigma(0.05) \approx 0.5125.

Interpretation: the second action earned the larger return, and the update favoured it. Step 0 pushed the other way, because action 0 has a negative score, and its push was smaller than step 1's. Each step is weighted by what happened after it.

Example
Example 3: a recommender with three items, and a try-it

Setup: a recommender chooses among three items with a softmax policy whose parameters start at θ=(0,0,0)\theta = (0, 0, 0), so each item has probability 1/31/3. The learning rate is α=0.5\alpha = 0.5. The user clicks the item chosen, which gives reward 1. Treat each episode as one step.

  1. The policy chooses item 2 and the user clicks. The return is G=1G = 1.
  2. The score for item 2 is 1[j=2]−π(j)\mathbf{1}[j = 2] - \pi(j) for each jj. For j=1j = 1 that is 0−1/3=−1/30 - 1/3 = -1/3. For j=2j = 2 it is 1−1/3=2/31 - 1/3 = 2/3. For j=3j = 3 it is 0−1/3=−1/30 - 1/3 = -1/3.
  3. Multiply by G=1G = 1. The gradient vector, in the order items 1, 2, 3, is (−1/3,2/3,−1/3)(-1/3, 2/3, -1/3).
  4. Update: θ←θ+0.5×(−1/3,2/3,−1/3)=(−1/6,1/3,−1/6)\theta \leftarrow \theta + 0.5 \times (-1/3, 2/3, -1/3) = (-1/6, 1/3, -1/6).
  5. New probabilities. The exponentials are e−1/6≈0.8465e^{-1/6} \approx 0.8465, e1/3≈1.3956e^{1/3} \approx 1.3956, and e−1/6≈0.8465e^{-1/6} \approx 0.8465. They sum to about 3.08863.0886. So item 1 has probability about 0.2740.274, item 2 about 0.4520.452, and item 3 about 0.2740.274.
  6. Check: 0.274+0.452+0.274=1.0000.274 + 0.452 + 0.274 = 1.000.

Interpretation: the item that earned the click gained probability. Each of the other two lost a little, which is why the update is a shift of probability, not a single change.

Try it: the same starting point, θ=(0,0,0)\theta = (0, 0, 0), α=0.5\alpha = 0.5, but this time the policy chooses item 1 and the user clicks. Compute the gradient vector, the new θ\theta, and the new probability of item 1.

Answer: the score vector is (2/3,−1/3,−1/3)(2/3, -1/3, -1/3), so the gradient is (2/3,−1/3,−1/3)(2/3, -1/3, -1/3). The new parameter is θ=0.5×(2/3,−1/3,−1/3)=(1/3,−1/6,−1/6)\theta = 0.5 \times (2/3, -1/3, -1/3) = (1/3, -1/6, -1/6). By the same calculation as step 5 with the signs swapped, the probability of item 1 is about 0.4520.452 and each of the other two is about 0.2740.274.

Common mistakes and tradeoffs

It is tempting to weight every action by the total reward of the episode. In fact each action should be weighted by the return from its own step onward, because an action cannot change rewards that came before it. Using the whole episode adds noise without changing the expected gradient, but it makes learning much slower.

It is tempting to think that a positive return means an action was good in absolute terms. In fact the update is relative. If every reward in a task is positive, every sampled action is pushed up, and the updates mostly reflect which actions were sampled. Subtracting a baseline, such as the average return of the episode or a learned value, leaves the expected gradient the same and reduces the variance a great deal. Note that the baseline must not depend on the action taken.

It is tempting to think that one episode is enough. In fact one episode gives a very noisy estimate of the gradient, and a single large step can undo earlier progress. Average over several episodes, use a small learning rate, and track progress over many updates.

It is tempting to forget the sign of the update. REINFORCE maximises return, so the step is plus the gradient of the log-probability times the return. If the code computes a loss to minimise, the loss is the negative of this sum.

When to use it: REINFORCE is simple, model-free, and works with discrete or continuous actions. It is a good first algorithm to implement and a clear reference for the policy gradient idea.

When not to use it: it is sample-hungry, because each episode is used once and then discarded. It can be unstable with large steps, and its variance grows with episode length. For long episodes or expensive environments, use a baseline, an Actor-Critic Methods method that learns a critic, or PPO, which limits how far each update can move the policy.

MethodExtra learned objectTypical use
Plain REINFORCEnoneteaching, small problems
REINFORCE with baselinea value estimate used only as baselineshort episodes with noisy returns
Actor-critica critic that estimates advantagesonline training with many environments
PPOa critic and a clipped updatelarge-scale training, language model fine-tuning

History

Ronald Williams described the REINFORCE family of algorithms in 1992, in a paper on connectionist (neural network) reinforcement learning. He showed that the gradient of expected reward could be estimated from sampled actions and rewards, and that the estimate could be used to update the weights of a network.

For a long time, policy gradient methods were a minor part of reinforcement learning, because their gradient estimates were very noisy. Baselines and actor-critic ideas reduced that noise, and later trust-region and clipped methods made large-scale training stable enough for deep networks.

Recap

  • A policy is a rule that gives action probabilities. REINFORCE learns it directly, using Policy Gradient.
  • Each step's contribution is the score of the action taken, multiplied by the Discounted Return from that step onward.
  • Averaging over sampled episodes, as in Monte Carlo Method, gives an unbiased estimate of the gradient of expected return.
  • A baseline reduces variance without changing the expected update. This leads to Actor-Critic Methods methods.
  • Read next: Actor-Critic Methods for a critic that reduces variance, and PPO for a stable update rule built on the same gradient.

Check yourself

QuestionWhy does REINFORCE not need a model of the environment's transition rule?Show answer

The update uses only the gradient of the log-probability of the sampled actions, which depends on the policy alone. The environment's transition probabilities appear in the product that defines the trajectory probability, but they do not depend on θ\theta, so their gradient is zero. The method only needs samples of the episode, which come from running the environment.

QuestionA policy has θ=0\theta = 0 and a learning rate of α=0.2\alpha = 0.2. It samples action 1 in a one-step episode with reward 4. What is the new θ\theta, and what is the new probability of action 1?Show answer

The score of action 1 is 1−0.5=0.51 - 0.5 = 0.5. The return is G=4G = 4, so the contribution is 0.5×4=20.5 \times 4 = 2. The update is θ=0+0.2×2=0.4\theta = 0 + 0.2 \times 2 = 0.4. The probability is σ(0.4)=1/(1+e−0.4)≈0.599\sigma(0.4) = 1/(1 + e^{-0.4}) \approx 0.599.

QuestionWhy is each action weighted by the return from its own step onward, instead of by the total reward of the episode?Show answer

An action at step tt cannot change rewards received before step tt. Using the total episode reward adds terms that do not depend on the action, which add noise to the estimate without changing its expected value. Using the return from step tt onward removes that noise and keeps the same expected gradient.

QuestionA game agent gets a reward of +1 for every step it survives, and no negative rewards. Without a baseline, what happens to the actions it samples, and why is that a problem?Show answer

Every sampled action has a positive return, so every sampled action's probability is pushed up. Actions that happen to be sampled more often gain probability, even if they are not better than others. The expected gradient is still correct, but the variance is high and learning is slow. Subtracting a baseline, such as the average return, makes some returns negative relative to the baseline, so the updates push up only the better-than-average actions.

Sources