Skip to content
Lambdian

Policy gradients: REINFORCE

Optimise the policy directly by gradient ascent on expected return, using the score-function trick.

Animated explanation

The idea

Value-based methods learn how good actions are and act greedily. Policy-gradient methods skip that and learn the policy itself: a parameterised distribution πθ(a∣s)\pi_\theta(a\mid s), often a neural network with a softmax output. We adjust θ\theta to make high-return behaviour more likely.

The magic is the policy gradient theorem: the gradient of expected return is the expectation of ∇log⁡πθ(a∣s)\nabla\log\pi_\theta(a\mid s) times the return. No model of the environment, no differentiating through it: just run the policy, see what happened, and push up the log-probability of each action by how good the outcome was. That is REINFORCE. It handles stochastic policies and continuous actions naturally, at the cost of noisy gradients.

Reducing variance

  • Use returns-to-go GtG_t (rewards after tt), not the whole episode's return: an action can't affect earlier rewards.
  • Subtract a baseline b(st)b(s_t), e.g. a learned V(st)V(s_t) or the batch average: the gradient stays unbiased because E[∇log⁡π⋅b]=0\mathbb{E}[\nabla\log\pi\cdot b] = 0, and its variance drops a lot.
  • Normalise returns within a batch; average over several episodes per update.

Why learn a policy directly?

Policies can be simpler than value functions; they can be stochastic (useful in games with bluffing and with aliased states); they handle continuous actions (output a Gaussian's mean and variance); and small parameter changes give smooth behaviour changes, unlike greedy argmax. Downsides: local optima, sample inefficiency (on-policy data is used once) and noisy gradients.

The algorithm, step by step

  1. 1
    Initialise policy parameters θ (and optionally a baseline).
  2. 2
    Run an episode with πθ\pi_\theta: s0,a0,r1,…s_0, a_0, r_1, \dots.
  3. 3
    Compute returns-to-go Gt=∑k≥tγk−trk+1G_t = \sum_{k\ge t}\gamma^{k-t}r_{k+1}.
  4. 4
    Update θ←θ+α∑t(Gt−b(st))∇θlog⁡πθ(at∣st)\theta \leftarrow \theta + \alpha\sum_t(G_t - b(s_t))\nabla_\theta\log\pi_\theta(a_t\mid s_t); repeat.

The maths

Policy gradient theorem

∇J(θ)=Eπ[∇log⁡πθ(a∣s) Qπ(s,a)]\nabla J(\theta) = \mathbb{E}_\pi\big[\nabla\log\pi_\theta(a\mid s)\,Q^\pi(s,a)\big]

REINFORCE estimator

∇J^=∑tGt ∇log⁡πθ(at∣st)\widehat{\nabla J} = \sum_t G_t\,\nabla\log\pi_\theta(a_t\mid s_t)

Baseline keeps it unbiased

Ea∼π[∇log⁡π(a∣s) b(s)]=b(s)∇∑aπ(a∣s)=0\mathbb{E}_{a\sim\pi}[\nabla\log\pi(a\mid s)\,b(s)] = b(s)\nabla\sum_a\pi(a\mid s) = 0

Softmax score

∇θslog⁡π(a∣s)=ea−π(⋅∣s)\nabla_{\theta_{s}}\log\pi(a\mid s) = \mathbf{e}_a - \boldsymbol\pi(\cdot\mid s)

Strengths and weaknesses

Strengths

  • Directly optimises what we care about
  • Stochastic and continuous-action policies
  • Simple; works with any differentiable policy

Weaknesses

  • High-variance gradients; sample-inefficient (on-policy)
  • Can converge to local optima
  • Sensitive to step size

Reach for it when…

Continuous or large action spaces, stochastic policies, and as the foundation of actor-critic and PPO.

Check your understanding

Question 1

Q1Why can REINFORCE learn without a model of the environment?

Question 2

Q2Subtracting a baseline b(s) from the return…

Question 3

Q3For a softmax policy with probabilities [0.2, 0.5, 0.3] and action 1 taken, ∇ log π w.r.t. the three scores is…

Lambdian Lab

REINFORCE on the corridor

A tabular softmax policy: theta has shape (N, 2), one row of scores per state.

  • policy(theta, s): action probabilities (stable softmax).
  • grad_log_pi(theta, s, a): gradient of log π(a | s) w.r.t. theta (a full N × 2 array).
  • returns_to_go(rewards, gamma).
  • reinforce(episodes, lr, baseline, seed): one update per episode (cap episodes at 200 steps); with baseline=True subtract the mean of the episode's returns-to-go. Return (theta, episode_returns).
Open the labPython + NumPy runs in your browser. Sign-in keeps your code and attempts.