Simple Algorithm
Reinforcement Learning · Bootstrapping · 24 min

The Policy Gradient Theorem & REINFORCE

The score-function trick and the simplest policy gradient algorithm.

Move a policy's parameters and everything downstream moves too: the actions, the states the agent visits, the rewards it sees. Differentiating the expected return looks as if it needs a model of all of that. It does not. This lesson derives the policy gradient theorem, turns it into REINFORCE, solves Sutton and Barto's switched corridor exactly, and proves that a baseline costs nothing.

What you’ll learn

  • Write softmax and Gaussian policies and compute their score functions ∇θlog⁡πθ(a∣s)\nabla_{\boldsymbol\theta}\log\pi_{\boldsymbol\theta}(a\mid s).
  • Derive the likelihood-ratio gradient of the expected return over trajectories, and use causality to reduce it to the reward-to-go estimator.
  • Prove the policy gradient theorem for episodic tasks by unrolling the gradient of the value function.
  • Compute the switched corridor's expected return J(p)J(p) and its optimum p⋆=2−2p^\star=2-\sqrt2.
  • Implement REINFORCE in PyTorch, and prove that subtracting an action-independent baseline leaves the gradient unbiased.

Policies with parameters

A parameterized policy πθ(a∣s)\pi_{\boldsymbol\theta}(a\mid s) must be differentiable in θ\boldsymbol\theta and, to keep exploring, should give every action positive probability. Two families cover most of practice.

For discrete actions, a softmax policy turns action preferences h(s,a,θ)h(s,a,\boldsymbol\theta) into probabilities. The preferences can be linear, h=θ⊤x(s,a)h=\boldsymbol\theta^\top\mathbf x(s,a) for state–action features x(s,a)\mathbf x(s,a) (Sutton and Barto's choice), or the logits of a neural network with one output per action.

πθ(a∣s)=eh(s,a,θ)∑beh(s,b,θ),∇θlog⁡πθ(a∣s)=x(s,a)−∑bπθ(b∣s) x(s,b)(linear h)\pi_{\boldsymbol\theta}(a\mid s)=\frac{e^{h(s,a,\boldsymbol\theta)}}{\sum_{b}e^{h(s,b,\boldsymbol\theta)}},\qquad \nabla_{\boldsymbol\theta}\log\pi_{\boldsymbol\theta}(a\mid s)=\mathbf x(s,a)-\sum_b\pi_{\boldsymbol\theta}(b\mid s)\,\mathbf x(s,b)\quad\text{(linear }h)
The score of a linear softmax is the chosen action's feature vector minus its average under the policy: the direction that favors aa over what the policy usually does.

For continuous actions, a Gaussian policy samples a∼N(mθ(s),σθ(s)2)a\sim\mathcal N\big(m_{\boldsymbol\theta}(s),\sigma_{\boldsymbol\theta}(s)^2\big), with the mean and the log standard deviation produced by a network (or the log standard deviation kept as a free parameter). We write mm for the mean because μ\mu is the start distribution. Since log⁡π=−(a−m)22σ2−log⁡σ+const\log\pi=-\frac{(a-m)^2}{2\sigma^2}-\log\sigma+\text{const}:

∂log⁡πθ(a∣s)∂m=a−mσ2,∂log⁡πθ(a∣s)∂log⁡σ=(a−m)2σ2−1\frac{\partial\log\pi_{\boldsymbol\theta}(a\mid s)}{\partial m}=\frac{a-m}{\sigma^2},\qquad\frac{\partial\log\pi_{\boldsymbol\theta}(a\mid s)}{\partial\log\sigma}=\frac{(a-m)^2}{\sigma^2}-1
Chain these through the network that outputs mm and log⁡σ\log\sigma. An action above the mean pulls the mean up; an action more than one standard deviation from the mean widens the distribution, one closer narrows it. For a vector action with independent coordinates, the log-probabilities add.

The objective is the expected return from the start distribution, J(θ)=ES0∼μ[Vπθ(S0)]J(\boldsymbol\theta)=\E_{S_0\sim\mu}\big[V^{\pi_{\boldsymbol\theta}}(S_0)\big], and the plan is gradient ascent, θ←θ+α ∇J^\boldsymbol\theta\leftarrow\boldsymbol\theta+\alpha\,\widehat{\nabla J}. From here on ∇\nabla means ∇θ\nabla_{\boldsymbol\theta} and π=πθ\pi=\pi_{\boldsymbol\theta}. One identity does most of the work in this lesson: the score has mean zero under the policy.

DerivationThe score has mean zero
  1. EA∼π(⋅∣s)[∇log⁡π(A∣s)]=∑aπ(a∣s) ∇π(a∣s)π(a∣s)=∇∑aπ(a∣s)=∇1=0\E_{A\sim\pi(\cdot\mid s)}\big[\nabla\log\pi(A\mid s)\big]=\sum_a\pi(a\mid s)\,\frac{\nabla\pi(a\mid s)}{\pi(a\mid s)}=\nabla\sum_a\pi(a\mid s)=\nabla1=\mathbf 0

    Write the expectation as a sum over actions and use π ∇log⁡π=∇π\pi\,\nabla\log\pi=\nabla\pi:

  2. So for any quantity bb that does not depend on the action, E[b ∇log⁡π(A∣s)]=b⋅0=0\E\big[b\,\nabla\log\pi(A\mid s)\big]=b\cdot\mathbf 0=\mathbf 0. This one line is behind causality, baselines and the policy gradient theorem itself. (For a Gaussian the sum becomes an integral; the mean score (a−m)/σ2(a-m)/\sigma^2 visibly averages to zero.)

Check your understanding

A tabular softmax policy in a single state has one preference per action, θ=(θ1,θ2,θ3)\boldsymbol\theta=(\theta_1,\theta_2,\theta_3), currently all equal. What is the score ∇log⁡π(a1)\nabla\log\pi(a_1)?

  1. (1,0,0)(1,0,0)
  2. (23,−13,−13)(\tfrac23,-\tfrac13,-\tfrac13)
  3. (13,13,13)(\tfrac13,\tfrac13,\tfrac13)
  4. (0,0,0)(0,0,0), because all preferences are equal
Show the answer

B. (23,−13,−13)(\tfrac23,-\tfrac13,-\tfrac13)

Tabular preferences are linear with one-hot features, so the score is x(a1)−∑bπ(b) x(b)=(1,0,0)−(13,13,13)\mathbf x(a_1)-\sum_b\pi(b)\,\mathbf x(b)=(1,0,0)-(\tfrac13,\tfrac13,\tfrac13). Raising θ1\theta_1 raises log⁡π(a1)\log\pi(a_1); raising the others lowers it. The components sum to zero because adding the same constant to every preference changes nothing.

Differentiating through the dice

A trajectory τ=(S0,A0,R1,S1,A1,R2,…,ST)\tau=(S_0,A_0,R_1,S_1,A_1,R_2,\dots,S_T) of an episodic task has probability pθ(τ)=μ(S0)∏tπ(At∣St) P(St+1,Rt+1∣St,At)p_{\boldsymbol\theta}(\tau)=\mu(S_0)\prod_{t}\pi(A_t\mid S_t)\,P(S_{t+1},R_{t+1}\mid S_t,A_t), and J(θ)=Eτ∼pθ[G0]J(\boldsymbol\theta)=\E_{\tau\sim p_{\boldsymbol\theta}}[G_0]. The parameters change which trajectories are likely, not the return of any given trajectory. The likelihood-ratio (log-derivative) trick moves the gradient onto the probabilities.

DerivationFrom trajectories to reward-to-go
  1. ∇J(θ)=∑τG0(τ) ∇pθ(τ)=Eτ∼pθ[G0(τ) ∇log⁡pθ(τ)]\nabla J(\boldsymbol\theta)=\sum_\tau G_0(\tau)\,\nabla p_{\boldsymbol\theta}(\tau)=\E_{\tau\sim p_{\boldsymbol\theta}}\big[G_0(\tau)\,\nabla\log p_{\boldsymbol\theta}(\tau)\big]

    Differentiate under the sum (finite trajectories, or bounded returns and dominated convergence) and use ∇p=p ∇log⁡p\nabla p=p\,\nabla\log p:

  2. ∇log⁡pθ(τ)=∑t=0T−1∇log⁡π(At∣St)⟹∇J=E[∑t=0T−1G0 ∇log⁡π(At∣St)]\nabla\log p_{\boldsymbol\theta}(\tau)=\sum_{t=0}^{T-1}\nabla\log\pi(A_t\mid S_t)\quad\Longrightarrow\quad\nabla J=\E\Big[\sum_{t=0}^{T-1}G_0\,\nabla\log\pi(A_t\mid S_t)\Big]

    In log⁡pθ(τ)\log p_{\boldsymbol\theta}(\tau) the start and transition terms do not involve θ\boldsymbol\theta, so they vanish from the gradient. The unknown dynamics are sampled, never differentiated:

  3. E[Rk+1 ∇log⁡π(At∣St)]=E[Rk+1 E[∇log⁡π(At∣St) ∣ Ht]⏟= 0]=0(k<t)\E\big[R_{k+1}\,\nabla\log\pi(A_t\mid S_t)\big]=\E\Big[R_{k+1}\,\underbrace{\E\big[\nabla\log\pi(A_t\mid S_t)\,\big|\,H_t\big]}_{=\,\mathbf 0}\Big]=\mathbf 0\qquad(k<t)

    Causality. Split G0=∑k<tγkRk+1+γtGtG_0=\sum_{k<t}\gamma^kR_{k+1}+\gamma^tG_t. A reward received before step tt is part of the history Ht=(S0,A0,R1,…,St)H_t=(S_0,A_0,R_1,\dots,S_t), and given HtH_t the score at step tt has mean zero. So past rewards contribute nothing on average:

  4. ∇J(θ)=E[∑t=0T−1γt Gt ∇log⁡π(At∣St)],Gt=∑k=tT−1γk−tRk+1\nabla J(\boldsymbol\theta)=\E\Big[\sum_{t=0}^{T-1}\gamma^t\,G_t\,\nabla\log\pi(A_t\mid S_t)\Big],\qquad G_t=\sum_{k=t}^{T-1}\gamma^{k-t}R_{k+1}

    What remains is the reward-to-go form: each action is credited only with the rewards that came after it, and step tt is discounted by γt\gamma^t from the start of the episode:

Sample one episode, evaluate the bracket, and you have an unbiased estimate of ∇J\nabla J. Stepping along it is REINFORCE (Williams, 1992), whose name spells out its update: REward Increment = Nonnegative Factor × Offset Reinforcement × Characteristic Eligibility. The nonnegative factor is the step size, the offset reinforcement is the return minus a baseline (zero so far), and the characteristic eligibility is the score.

The policy gradient theorem

The trajectory form already gives an algorithm. The policy gradient theorem (Sutton, McAllester, Singh & Mansour, 2000) states the same gradient in terms of states and actions, which is what critics need. Take the episodic case of Sutton & Barto (§13.2): undiscounted, a fixed start state s0s_0, and every policy reaches a terminal state with probability 1, so that J(θ)=Vπ(s0)J(\boldsymbol\theta)=V^\pi(s_0).

DerivationProof by unrolling (Sutton & Barto, §13.2)
  1. ∇Vπ(s)=∑a∇π(a∣s) Qπ(s,a)⏟ϕ(s)+∑aπ(a∣s)∑s′P(s′∣s,a) ∇Vπ(s′)\nabla V^\pi(s)=\underbrace{\sum_a\nabla\pi(a\mid s)\,Q^\pi(s,a)}_{\phi(s)}+\sum_a\pi(a\mid s)\sum_{s'}P(s'\mid s,a)\,\nabla V^\pi(s')

    Differentiate Vπ(s)=∑aπ(a∣s) Qπ(s,a)V^\pi(s)=\sum_a\pi(a\mid s)\,Q^\pi(s,a) with the product rule. Rewards and dynamics do not depend on θ\boldsymbol\theta, so ∇Qπ(s,a)=∑s′P(s′∣s,a) ∇Vπ(s′)\nabla Q^\pi(s,a)=\sum_{s'}P(s'\mid s,a)\,\nabla V^\pi(s'):

  2. ∇Vπ(s)=ϕ(s)+∑xPr⁡(S1=x∣S0=s) ϕ(x)+∑xPr⁡(S2=x∣S0=s) ∇Vπ(x)=⋯\nabla V^\pi(s)=\phi(s)+\sum_x\Pr(S_1=x\mid S_0=s)\,\phi(x)+\sum_x\Pr(S_2=x\mid S_0=s)\,\nabla V^\pi(x)=\cdots

    The second term is the expected gradient one step later. Substitute the same identity for ∇Vπ(s′)\nabla V^\pi(s'), then again for the state after that:

  3. ∇Vπ(s0)=∑k≥0∑xPr⁡(Sk=x∣S0=s0) ϕ(x)=∑xη(x)∑a∇π(a∣x) Qπ(x,a)\nabla V^\pi(s_0)=\sum_{k\ge0}\sum_x\Pr(S_k=x\mid S_0=s_0)\,\phi(x)=\sum_x\eta(x)\sum_a\nabla\pi(a\mid x)\,Q^\pi(x,a)

    After kk unrollings the remainder is ∑xPr⁡(Sk=x) ∇Vπ(x)\sum_x\Pr(S_k=x)\,\nabla V^\pi(x). In a finite MDP whose episodes end with probability 1, the probability of still running decays geometrically and ∇Vπ\nabla V^\pi is bounded (and zero at the terminal state), so the remainder vanishes:

  4. Finally ∇π=π ∇log⁡π\nabla\pi=\pi\,\nabla\log\pi turns the sum over actions into an expectation under the policy, and normalizing η\eta gives the second form. ■\blacksquare

Read the theorem as a recipe: sample states the way the policy visits them, sample an action from the policy, and weight its score by how good that action is, Qπ(S,A)Q^\pi(S,A). REINFORCE uses the return GtG_t as an unbiased sample of Qπ(St,At)Q^\pi(S_t,A_t); actor–critic methods use a learned critic instead (Actor–Critic Methods).

Think first The visit counts η\eta depend on θ\boldsymbol\theta: changing the policy changes where the agent goes. Why does no derivative of η\eta appear in the theorem?

It has been absorbed into QπQ^\pi. Unrolling the recursion follows every downstream effect of a parameter change, and the value of acting differently now already includes the value of reaching different states later. A second view uses the performance difference lemma: there, the effect of shifting the state distribution is multiplied by ∑aπ(a∣s)Aπ(s,a)\sum_a\pi(a\mid s)A^\pi(s,a), the policy's average advantage over itself, which is zero. The Policy Gradient Theorem in Depth proves the discounted and average-reward versions that way.

Solving the switched corridor

Back to the corridor of the Exploration lesson (Sutton & Barto, Example 13.1): cells 1, 2, 3 and a goal, reward −1-1 per step, γ=1\gamma=1. Left in cell 1 bumps into the wall, and in cell 2 the actions are switched. The cells look identical, so one parameter, p=Pr⁡(right)p=\Pr(\textit{right}), acts everywhere. The expected return J(p)=−E[steps]J(p)=-\E[\text{steps}] can be found by solving three Bellman equations; here is a shorter route that also explains the shape of the answer.

DerivationExcursions from the middle cell
  1. T1=1p+T2T_1=\frac1p+T_2

    Leaving cell 1. Each step there moves right with probability pp, so the number of steps until the agent enters cell 2 is geometric with mean 1/p1/p. Writing TiT_i for the expected number of steps to the goal from cell ii:

  2. E[cost of an excursion]=p(1+1p)+(1−p)⋅2=3−p,Pr⁡(an excursion finishes)=(1−p) p\E[\text{cost of an excursion}]=p\Big(1+\frac1p\Big)+(1-p)\cdot2=3-p,\qquad\Pr(\text{an excursion finishes})=(1-p)\,p

    Excursions from cell 2. Each visit to cell 2 starts an excursion that ends either at the goal or back in cell 2. With probability pp the agent goes right into cell 1 and needs 1/p1/p more steps on average to come back, a cost of 1+1/p1+1/p. With probability 1−p1-p it goes left into cell 3 and then either finishes (right) or returns (left), two steps either way:

  3. T2=E[N]⋅(3−p)=3−pp(1−p),T1=1−pp(1−p)+3−pp(1−p)=4−2pp(1−p)T_2=\E[N]\cdot(3-p)=\frac{3-p}{p(1-p)},\qquad T_1=\frac{1-p}{p(1-p)}+\frac{3-p}{p(1-p)}=\frac{4-2p}{p(1-p)}

    Excursions are i.i.d. (the chain restarts at each return to cell 2), and the number NN of excursions up to the first that finishes is geometric with mean 1/(p(1−p))1/\big(p(1-p)\big). It is a stopping time for the sequence of excursions, so Wald's identity multiplies the means:

  4. p⋆=2−2≈0.586,J(p⋆)=−2232−4=−(6+42)≈−11.66p^\star=2-\sqrt2\approx0.586,\qquad J(p^\star)=\frac{-2\sqrt2}{3\sqrt2-4}=-\big(6+4\sqrt2\big)\approx-11.66

    So J(p)=−T1=2p−4p(1−p)J(p)=-T_1=\frac{2p-4}{p(1-p)}, the curve the widget draws. The derivative of 4−2pp−p2\frac{4-2p}{p-p^2} has numerator −2(p−p2)−(4−2p)(1−2p)=−2(p2−4p+2)-2(p-p^2)-(4-2p)(1-2p)=-2(p^2-4p+2), which vanishes at

The formula explains the optimum. The only way out is left then right from cell 2, probability p(1−p)p(1-p) per excursion, which alone would favor p=12p=\tfrac12; but each trip back into cell 1 costs 1/p1/p steps against the wall, which favors a larger pp. The balance lands at 0.5860.586. The top of the landscape is also flat: J(0.5)=−12J(0.5)=-12, only a third of a step worse than the optimum, while ε-greedy's p=0.95p=0.95 gives J≈−44J\approx-44.

Predict before you look

At p=12p=\tfrac12, how many excursions from the middle cell does an episode need on average?

Show the answer

4

Each excursion finishes with probability p(1−p)=14p(1-p)=\tfrac14, so E[N]=4\E[N]=4. Each costs 3−p=2.53-p=2.5 steps on average, so T2=10T_2=10, and adding the 1/p=21/p=2 steps needed to leave cell 1 first gives T1=12T_1=12, the twelve steps of the fair coin in the Exploration lesson.

REINFORCE in code, and a free baseline

In an automatic-differentiation framework you never write the score by hand. Build a scalar whose gradient is minus the REINFORCE estimate, and let the optimizer descend it.

python
import torch
from torch.distributions import Categorical

def reinforce_loss(logits, actions, rewards, gamma=0.99, baseline=None):
    """One episode. logits: (T, A); actions: (T,) ints; rewards: (T,), rewards[t] = R_{t+1}."""
    T = rewards.shape[0]
    returns = torch.empty(T)
    g = torch.tensor(0.0)
    for t in reversed(range(T)):          # reward-to-go: G_t = R_{t+1} + gamma * G_{t+1}
        g = rewards[t] + gamma * g
        returns[t] = g
    if baseline is not None:              # e.g. a value network's V(S_t), shape (T,)
        returns = returns - baseline.detach()
    log_probs = Categorical(logits=logits).log_prob(actions)
    discounts = gamma ** torch.arange(T, dtype=torch.float32)   # the gamma^t most code omits
    return -(discounts * returns * log_probs).sum()

The returns and the baseline are constants: gradients flow only through the log-probabilities. Deleting discounts gives the update most libraries implement; with γ=1\gamma=1, as in the corridor, the two coincide.

python
import gymnasium as gym
import torch.nn as nn

env = gym.make("CartPole-v1")
policy = nn.Sequential(nn.Linear(4, 64), nn.Tanh(), nn.Linear(64, 2))
opt = torch.optim.Adam(policy.parameters(), lr=1e-2)
for episode in range(500):
    obs, info = env.reset(seed=episode)
    logits, actions, rewards, done = [], [], [], False
    while not done:
        out = policy(torch.as_tensor(obs, dtype=torch.float32))
        a = Categorical(logits=out).sample()
        obs, r, terminated, truncated, info = env.step(a.item())
        logits.append(out); actions.append(a); rewards.append(float(r))
        done = terminated or truncated
    loss = reinforce_loss(torch.stack(logits), torch.stack(actions), torch.tensor(rewards))
    opt.zero_grad(); loss.backward(); opt.step()

One gradient step per episode, on-policy: every episode is generated by the current parameters and used once. Treating a time-limit truncation like a true ending slightly misjudges the returns near the limit; Generalized Advantage Estimation shows the fix.

DerivationWhy a baseline is free
  1. E[γt b(St) ∇log⁡π(At∣St)]=E[γt b(St) E[∇log⁡π(At∣St) ∣ Ht]]=0\E\big[\gamma^t\,b(S_t)\,\nabla\log\pi(A_t\mid S_t)\big]=\E\Big[\gamma^t\,b(S_t)\,\E\big[\nabla\log\pi(A_t\mid S_t)\,\big|\,H_t\big]\Big]=\mathbf 0

    Let bb be any function of the state StS_t, and possibly of the history before it, but not of AtA_t or anything after. Condition on the history and use the zero-mean score:

  2. So E[∑tγt(Gt−b(St))∇log⁡π(At∣St)]=∇J\E\big[\sum_t\gamma^t\big(G_t-b(S_t)\big)\nabla\log\pi(A_t\mid S_t)\big]=\nabla J for every such bb. The mean is untouched, and bb can be chosen purely to reduce variance. The usual choice is a learned b(s)≈Vπ(s)b(s)\approx V^\pi(s), which turns Gt−b(St)G_t-b(S_t) into an estimate of the advantage Aπ(St,At)=Qπ(St,At)−Vπ(St)A^\pi(S_t,A_t)=Q^\pi(S_t,A_t)-V^\pi(S_t).

  3. The condition matters. A baseline that depends on the action, b(s,a)b(s,a), generally adds a bias E[b(S,A) ∇log⁡π(A∣S)]≠0\E\big[b(S,A)\,\nabla\log\pi(A\mid S)\big]\neq\mathbf 0 unless a correction term is added, the subject of Baselines & Variance Reduction.

Check your understanding

Which of these, subtracted from GtG_t with no correction term, would bias the REINFORCE gradient?

  1. A constant, such as the average return of the previous 100 episodes
  2. A learned state value v^(St,w)\hat v(S_t,\mathbf w)
  3. A critic's estimate Q^(St,At)\hat Q(S_t,A_t) of the action's value
  4. The sum of the rewards received before step tt
Show the answer

C. A critic's estimate Q^(St,At)\hat Q(S_t,A_t) of the action's value

Q^(St,At)\hat Q(S_t,A_t) depends on the action, so E[Q^(S,A) ∇log⁡π(A∣S)∣S]\E\big[\hat Q(S,A)\,\nabla\log\pi(A\mid S)\mid S\big] is not zero. With a perfect critic it equals the whole policy-gradient term at that state, so subtracting it would cancel the signal entirely. Anything fixed before AtA_t is drawn, whether a constant, a state value or past rewards, is free.

Key takeaways

  • A linear softmax policy's score is the chosen action's features minus their policy average; a Gaussian's mean score is (a−m)/σ2(a-m)/\sigma^2. Every score has mean zero under the policy.
  • The likelihood-ratio trick gives ∇J=E[∑tγtGt ∇log⁡πθ(At∣St)]\nabla J=\E\big[\sum_t\gamma^tG_t\,\nabla\log\pi_{\boldsymbol\theta}(A_t\mid S_t)\big]: the dynamics drop out of the gradient, and causality removes past rewards.
  • The policy gradient theorem, ∇J=∑sη(s)∑aQπ(s,a) ∇π(a∣s)\nabla J=\sum_s\eta(s)\sum_aQ^\pi(s,a)\,\nabla\pi(a\mid s), needs no derivative of the state distribution; REINFORCE samples it with GtG_t in place of QπQ^\pi.
  • In the switched corridor, J(p)=(2p−4)/(p(1−p))J(p)=(2p-4)/\big(p(1-p)\big) peaks at p⋆=2−2≈0.586p^\star=2-\sqrt2\approx0.586 with J≈−11.66J\approx-11.66, while every deterministic policy runs forever.
  • Any baseline that does not depend on the action leaves the gradient unbiased; a state value close to VπV^\pi turns returns into advantage estimates.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

A Gaussian policy has mean m=0.5m=0.5 and standard deviation σ=0.2\sigma=0.2, and it samples a=0.9a=0.9. With a positive return and no baseline, what does the REINFORCE update do to mm and to log⁡σ\log\sigma?

  1. It raises mm and lowers log⁡σ\log\sigma
  2. It raises mm and raises log⁡σ\log\sigma
  3. It lowers mm and raises log⁡σ\log\sigma
  4. It changes mm only, because σ\sigma has no score
Show the answer

B. It raises mm and raises log⁡σ\log\sigma

The scores are (a−m)/σ2=0.4/0.04=10(a-m)/\sigma^2=0.4/0.04=10 for the mean and (a−m)2/σ2−1=4−1=3(a-m)^2/\sigma^2-1=4-1=3 for log⁡σ\log\sigma, both positive. The action was two standard deviations above the mean and it paid off, so the update moves the mean toward it and widens the distribution, making such actions more likely. An action within one standard deviation would have narrowed it.

Question 2

Why may REINFORCE weight the score at step tt by the reward-to-go GtG_t instead of the whole-episode return G0G_0?

  1. Because rewards before step tt are always zero
  2. Because GtG_t is a lower-variance but biased replacement for G0G_0
  3. Because past rewards are fixed given the history up to StS_t, and the score at step tt has mean zero given that history, so their contribution averages to zero
  4. Because discounting makes earlier rewards negligible
Show the answer

C. Because past rewards are fixed given the history up to StS_t, and the score at step tt has mean zero given that history, so their contribution averages to zero

Dropping the past rewards removes terms whose mean is exactly zero, so the estimator stays unbiased, and its variance usually falls, since those rewards were pure noise as far as AtA_t is concerned.

Question 3

In the switched corridor, why is the optimal probability of right above 12\tfrac12?

  1. Leaving needs left then right from the middle cell, which alone would favor p=12p=\tfrac12, but every return to the first cell costs 1/p1/p steps at the wall, which favors a larger pp
  2. The goal is to the right, so right is always the better action
  3. Episodes terminate only when p>12p>\tfrac12
  4. REINFORCE is biased toward right
Show the answer

A. Leaving needs left then right from the middle cell, which alone would favor p=12p=\tfrac12, but every return to the first cell costs 1/p1/p steps at the wall, which favors a larger pp

An excursion from the middle cell finishes with probability p(1−p)p(1-p), maximized at p=12p=\tfrac12, but trips back to the first cell cost 1+1/p1+1/p steps. Minimizing (4−2p)/(p(1−p))(4-2p)/\big(p(1-p)\big) balances the two at p⋆=2−2≈0.586p^\star=2-\sqrt2\approx0.586.

Question 4

In the episodic policy gradient theorem, ∇J=∑sη(s)∑aQπ(s,a) ∇π(a∣s)\nabla J=\sum_s\eta(s)\sum_aQ^\pi(s,a)\,\nabla\pi(a\mid s). What is η(s)\eta(s)?

  1. The probability of starting in ss
  2. The stationary distribution of the policy's Markov chain
  3. The fraction of the episode's reward collected in ss
  4. The expected number of visits to ss during an episode
Show the answer

D. The expected number of visits to ss during an episode

η(s)=∑tPr⁡(St=s)\eta(s)=\sum_t\Pr(S_t=s). Normalized, it is the on-policy distribution from which REINFORCE's sampled states arrive; the normalizing constant, the expected episode length, is absorbed into the step size in practice.

Go deeper

Further reading

  • R. J. Williams, "Simple statistical gradient-following algorithms for connectionist reinforcement learning," Machine Learning 8, 229–256 (1992).
  • R. S. Sutton, D. McAllester, S. Singh & Y. Mansour, "Policy gradient methods for reinforcement learning with function approximation," NIPS 12 (2000).
  • R. S. Sutton & A. G. Barto, Reinforcement Learning: An Introduction, ch. 13 (MIT Press, 2nd ed., 2018).