5.2.5 · D4Deep & Advanced RL

Exercises — Policy gradient methods

1,961 words9 min readBack to topic

Before we start, one reminder of every symbol you will meet, so nothing appears unexplained:


L1 — Recognition

Q1.1 — Spot the survivor

In the expansion , the trajectory probability factorizes into three kinds of terms: the initial-state distribution , the environment dynamics , and the policy . Which term(s) survive the gradient, and why?

Recall Solution

WHAT survives: only . WHY: turns the product into a sum: . The gradient asks "how does this change when I wiggle the knobs ?" The initial distribution and the dynamics belong to the environment — they contain no — so their slope is . Only the policy carries . Consequence: we never need a model of the world → policy gradients are model-free.

Q1.2 — Ascent or descent?

We have . Do we perform or ?

Recall Solution

Ascent: . is return we want to maximize, and the gradient points uphill. (Equivalently: minimize the loss .)


L2 — Application

Q2.1 — One-step softmax gradient

A two-action softmax policy has and . You sample and receive return . With learning rate and current , compute the update .

Recall Solution

Step 1 — the log-prob gradient. . A standard identity: . Why? , and . Step 2 — plug in . , so . Step 3 — assemble. . What it looks like: rises → rises above → action becomes more probable. A rewarded action was reinforced. ✓

Q2.2 — Reward-to-go by hand

An episode with has rewards . Compute the reward-to-go .

Recall Solution

WHAT we do: sum rewards from onward, each discounted by how far in the future it is relative to . WHY start the exponent at : the reward at is "now" from 's viewpoint, so it gets . is excluded because an action at cannot affect the past (causality).


L3 — Analysis

Q3.1 — Prove the baseline is unbiased

Show that for any function that does not depend on the action, .

Recall Solution

Step 1 — write the expectation as a sum over actions (discrete case): Step 2 — undo the log-derivative trick. Since : Step 3 — probabilities sum to 1, and the gradient of a constant is : WHY came out front: it does not depend on , so it is a constant with respect to the sum over . That is the whole reason a state-only baseline is allowed but an action-dependent one is not.

Q3.2 — Why does a baseline shrink variance?

Given three actions with returns , compare the spread of the update weights with baseline versus . What does this say about variance?

Recall Solution

The update weight on each action is .

  • : weights , mean , spread around a huge common offset.
  • : weights — the offset is removed; only the differences that matter remain. WHY variance drops: the Monte-Carlo estimator's variance scales with the magnitude of these weights (roughly ). With every weight is , so sampling noise on gets amplified . Subtracting centers the weights near zero without changing the expected gradient (Q3.1), so we keep the signal and kill the amplified noise.
    Figure — Policy gradient methods
    The figure shows the same three weights before (tall bars far from zero) and after (short bars straddling zero) subtracting the baseline — same shape, dramatically smaller magnitude.

L4 — Synthesis

Q4.1 — Advantage from returns and a value estimate

In a state you have three actions whose sampled reward-to-go values are , each sampled once with equal policy probability. Estimate as the mean return, then compute the advantage for each action. Which actions get reinforced, which get suppressed?

Recall Solution

Step 1 — baseline : the natural estimate is the average return, . Step 2 — advantages: Step 3 — interpretation via the update :

  • : → pushed up (better than average).
  • : → pushed down (worse than average).
  • : no change (exactly average). WHY this is the seed of Actor-Critic methods: replace the hand-averaged with a learned critic , and you turn REINFORCE-with-baseline into actor-critic. The advantage is the shared currency; see Advantage function and Generalized Advantage Estimation.

Q4.2 — Effective sample-count intuition

Suppose a plain REINFORCE estimate has variance per trajectory and you average independent trajectories. If a baseline reduces per-trajectory variance to , how many un-baselined trajectories would you need to match the accuracy of baselined trajectories?

Recall Solution

Step 1 — accuracy = variance of the average. For i.i.d. samples the mean's variance is (per-sample variance).

  • Baselined, : variance .
  • Un-baselined, : variance . Step 2 — match them: . Answer: un-baselined trajectories — more data for the same accuracy. This is the practical payoff of Variance reduction in Monte Carlo.

L5 — Mastery

Q5.1 — Build and verify a two-action gradient step end-to-end

A softmax policy over two actions uses logits , so Start at . You run one episode: sample , and the environment returns for this action (baseline ). Then run a second episode: sample , return as well, still . (a) Compute symbolically. (b) Give both updates with , then the net after both (apply sequentially, recomputing ). (c) Explain what goes wrong here and how a baseline of fixes it.

Recall Solution

(a) Gradient of the losing branch. . Since , (Compare Q2.1's — the two branches have opposite-signed gradients, as they must, since raising one lowers the other.)

(b) Sequential updates (, ).

  • Episode 1 at , : sampled , weight . . New .
  • Episode 2 at , : sampled , weight . . New .

(c) What went wrong. Both actions gave the same return , so neither is genuinely better — yet we still shoved around by more than a full unit each step. With , every positive return is treated as "reinforce me," so the updates are dominated by the common offset, not by real differences. The net wandered to purely from ordering/noise, not from any signal.

The baseline fix. Set . Now each weight is , so both updates are exactly : stays put at . That is the correct behavior — when two actions are equally good, the policy should not move. The baseline stripped the meaningless common offset, leaving only the (here, zero) advantage. This is exactly Q3.1's unbiasedness paying off as Q3.2's variance reduction.

Figure — Policy gradient methods


Connections

  • Policy gradient methods — the parent theory these exercises drill.
  • REINFORCE algorithm · Log-derivative trick · Score function estimator — Q1, Q2.1, Q5.
  • Advantage function · Actor-Critic methods — Q4.1.
  • Variance reduction in Monte Carlo · Generalized Advantage Estimation — Q3.2, Q4.2.
  • Trust Region and PPO — controlling step size when updates (like Q5's ) get wild.