Equations · Williams, “Simple statistical gradient-following algorithms for connectionist reinforcement learning” (1992); Sutton et al., policy gradient theorem (2000)

The policy gradient (REINFORCE) formula, explained

The gradient of a policy's expected return, written as something you can estimate from sampled episodes. Each action's log-probability is pushed up in proportion to the return that followed it: actions that led to high returns become more likely.

∇θJ(θ)=Eτ∼πθ[∑t=0T−1∇θlog⁡πθ(at∣st) Gt],Gt=∑k=t+1Tγk−t−1rk\nabla_\theta J(\theta) = \mathbb{E}_{\tau \sim \pi_\theta}\left[\sum_{t=0}^{T-1} \nabla_\theta \log \pi_\theta(a_t \mid s_t)\, G_t\right], \quad G_t = \sum_{k=t+1}^{T} \gamma^{k-t-1} r_k

Open in the decoder →

Term by term

  1. J(θ)J(\theta)

    The objective: the expected total (discounted) reward of the policy with parameters θ\theta. It is maximized, by gradient ascent.

  2. ∇θ\nabla_\theta

    The gradient with respect to the policy's parameters.

  3. Eτ∼πθ\mathbb{E}_{\tau \sim \pi_\theta}

    An average over trajectories τ=(s0,a0,r1,s1,…)\tau = (s_0, a_0, r_1, s_1, \ldots) generated by running the policy. In practice, an average over a few sampled episodes. Here τ\tau is a trajectory, not a temperature.

  4. ∇θlog⁡πθ(at∣st)\nabla_\theta \log \pi_\theta(a_t \mid s_t)

    The direction in parameter space that makes action ata_t more likely in state sts_t. Taking the gradient of the log is what lets the expectation's gradient be estimated by sampling (the "log-derivative trick").

  5. Gt=∑k=t+1Tγk−t−1rkG_t = \sum_{k=t+1}^{T} \gamma^{k-t-1} r_k

    The return from step tt: the rewards that came after action ata_t, discounted by γ\gamma per step. Subtracting a baseline, such as a value estimate, from GtG_t keeps the gradient unbiased and lowers its variance.

A small worked example

Two actions, softmax policy with logits θ=(0,0)\theta = (0, 0), so π=(0.5,0.5)\pi = (0.5, 0.5). One episode: action 1 is taken and the return is G=2G = 2. For a softmax, ∇θlog⁡π(a1)=(1,0)−π=(0.5,−0.5)\nabla_\theta \log \pi(a_1) = (1, 0) - \pi = (0.5, -0.5). The estimate is 2×(0.5,−0.5)=(1,−1)2 \times (0.5, -0.5) = (1, -1); a step of size 0.10.1 gives θ=(0.1,−0.1)\theta = (0.1, -0.1) and π(a1)≈0.550\pi(a_1) \approx 0.550.

In code

import torch

def reinforce_loss(log_probs, rewards, gamma=0.99):
    # log_probs: (T,) log pi(a_t|s_t) for one episode; rewards: r_1 .. r_T
    G, returns = 0.0, []
    for r in reversed(rewards):
        G = r + gamma * G
        returns.insert(0, G)
    return -(log_probs * torch.tensor(returns)).sum()   # minimize the negative

Every symbol in it

J(θ)=Eτ∼πθ[R(τ)]J(\theta) = \mathbb{E}_{\tau \sim \pi_\theta}\left[R(\tau)\right]Objective Jpθ(x)p_\theta(x)Theta∇θL\nabla_\theta \mathcal{L}Nabla (gradient)xi,  Wq,  pθ,  dmodelx_i,\; W_q,\; p_\theta,\; d_{\text{model}}Subscript (index or label)Ex∼p[f(x)]\mathbb{E}_{x \sim p}[f(x)]Expectationsoftmax⁡(z/τ)\operatorname{softmax}(z / \tau)Taux∼pdatax \sim p_{\text{data}}Distributed as (tilde)log⁡pθ(x)\log p_\theta(x)Logarithmπθ(a∣s)\pi_\theta(a \mid s)Pip(y∣x)p(y \mid x)Given (conditional bar)st,at,rt,st+1s_t, a_t, r_t, s_{t+1}State s and action aGt=∑k=0∞γkrt+k+1G_t = \sum_{k=0}^{\infty} \gamma^k r_{t+k+1}Return Grt=R(st,at)r_t = R(s_t, a_t)Reward rγ\gammaGamma∑i=1Nxi\sum_{i=1}^{N} x_iSummation

Other equations