This page is the practice arena for Policy gradient methods . The parent note built the theory; here we grind through every kind of situation the policy gradient can throw at you: positive and negative returns, the do-nothing (zero-gradient) case, degenerate deterministic policies, continuous actions, a full multi-step trajectory, a baseline rescue, and an exam twist.
Before a single number: recall the one object we keep computing, the score ∇ θ log π θ ( a ∣ s ) — literally "how would the log-probability of the action I took change if I wiggled θ ?" Every example is that score times a weight (return, reward-to-go, or advantage). If any symbol here feels unearned, re-read the parent's derivation and the Log-derivative trick .
Every policy-gradient exercise lands in one of these cells. Each worked example below is tagged with the cell(s) it covers.
#
Cell class
What's special
Covered by
C1
Positive return, discrete
good action, push up
Ex 1
C2
Negative return, discrete
bad action, push down
Ex 2
C3
Zero / degenerate weight
return = 0 or baseline cancels → no update
Ex 3
C4
Deterministic-limit policy
π → 0 or → 1 : score blows up / vanishes
Ex 4
C5
Multi-action softmax
full gradient over > 2 actions, reward-to-go
Ex 5
C6
Continuous action (Gaussian)
arg max impossible; score of a normal density
Ex 6
C7
Baseline rescue
large-offset returns, variance killed by b = V
Ex 7
C8
Full trajectory, causality
discounting + reward-to-go across time
Ex 8
C9
Exam twist
"gradient is zero because reward has no θ " trap, sign of γ -weighting
Ex 9
Intuition The single skeleton
Every update is θ ← θ + α ⋅ ( score ) ⋅ ( weight ) .
Score says which direction makes this action more likely . Weight says how much we liked it (and its sign says up or down). That's the whole page.
Two policies appear everywhere below. Let's earn their scores once.
Definition Sigmoid (2-action) policy
σ ( θ ) = 1 + e − θ 1 is a number squashed into ( 0 , 1 ) — a probability. Let π θ ( a 1 ) = σ ( θ ) , π θ ( a 2 ) = 1 − σ ( θ ) .
Its scores (derivative of the log-probability):
∇ θ log σ ( θ ) = 1 − σ ( θ ) , ∇ θ log ( 1 − σ ( θ ) ) = − σ ( θ )
Definition Softmax (many-action) policy
Given "logits" z 1 , … , z K (raw scores per action), π ( a i ) = ∑ j e z j e z i .
If z i = θ i directly, the score of the chosen action a k w.r.t. logit θ i is
∂ θ i ∂ log π ( a k ) = 1 [ i = k ] − π ( a i )
Read it: "1 if this is the action I took, minus its current probability." Taking an action pushes up its own logit and pulls down all others in proportion to their probability.
The figure shows why the score is ( 1 − σ ) for a 1 : when σ is already near 1, there's little room to grow, so the nudge is small; when σ is near 0, the score is near 1 — a huge push. Look at the orange curve's slope.
Worked example Ex 1 — Positive return, discrete
(C1)
Sigmoid policy, θ = 0 so σ ( 0 ) = 0.5 . We sample a 1 and the episode returns R = + 2 . Learning rate α = 0.1 . Compute the update to θ .
Forecast: does θ go up or down, and does σ move toward or away from a 1 ?
Score of the taken action. ∇ θ log σ ( 0 ) = 1 − σ ( 0 ) = 1 − 0.5 = 0.5 .
Why this step? The gradient only ever touches the log-prob of the action we actually took (parent Step 3).
Multiply by the weight. grad = 0.5 × R = 0.5 × 2 = 1.0 .
Why? REINFORCE weights the score by the return R ( τ ) .
Ascent update. θ ← 0 + 0.1 × 1.0 = 0.1 .
Why ascent? J is a return we maximize .
Verify: new σ ( 0.1 ) = 1 + e − 0.1 1 ≈ 0.525 > 0.5 . Probability of the rewarded action a 1 rose. ✓ A good action got reinforced.
Worked example Ex 2 — Negative return, discrete
(C2)
Same policy, θ = 0 , but now sampling a 1 gave R = − 3 . Same α = 0.1 .
Forecast: the score is still positive (0.5 ) — so how does a 1 get less likely?
Score unchanged: ∇ θ log σ ( 0 ) = 0.5 .
Why? The score depends only on the policy, not on the reward.
Weight carries the sign. grad = 0.5 × ( − 3 ) = − 1.5 .
Why? The negative return flips the direction — this is how "bad" propagates.
Update. θ ← 0 + 0.1 × ( − 1.5 ) = − 0.15 .
Verify: σ ( − 0.15 ) ≈ 0.4626 < 0.5 . The punished action became less probable. ✓ The sign of the weight, not the score, decides up/down.
Worked example Ex 3 — Zero / degenerate weight
(C3)
Sigmoid policy, θ = 0.4 , sample a 2 , return R = 0 (the agent survived but earned nothing). Then a second case: R = 5 but we subtract baseline b = 5 .
Forecast: what is the update in each sub-case?
Case A, R = 0 . grad = ( − σ ( 0.4 ) ) × 0 = 0 .
Why? No reward signal → nothing to learn from this sample. The policy is unchanged.
Case B, R = 5 , b = 5 . Advantage A = R − b = 0 , so grad = ( − σ ( 0.4 ) ) × 0 = 0 .
Why? A perfectly average return carries no relative information; the baseline turns "as expected" into a zero update.
Verify: In both cases θ stays at 0.4 ; σ ( 0.4 ) ≈ 0.599 before and after. ✓ Degenerate weight ⇒ frozen parameter. This is the algorithm correctly doing nothing.
Worked example Ex 4 — Deterministic-limit policy
(C4)
Sigmoid policy pushed to the edge: θ = 6 , so σ ( 6 ) ≈ 0.9975 (nearly deterministic toward a 1 ). Compare the score for sampling a 1 vs. sampling the rare a 2 .
Forecast: which sampled action produces the bigger score magnitude?
Score if a 1 sampled: 1 − σ ( 6 ) = 1 − 0.9975 = 0.0025 — almost zero.
Why? a 1 is already near-certain; there's almost no room to make it more likely.
Score if a 2 sampled: − σ ( 6 ) = − 0.9975 — nearly − 1 , huge.
Why? A surprising action carries a strong gradient; if it earned reward it dramatically reshapes the policy.
Limit reading. As θ → ∞ : the confident action's score → 0 (learning stalls), the rare action's score → − 1 (saturated but bounded).
Why this matters: over-confident policies stop exploring — the vanishing score is the mathematical face of "premature convergence."
Verify: ∣ − 0.9975 ∣ > ∣0.0025∣ ; ratio ≈ 399 . ✓ Rare actions dominate the gradient near a deterministic limit — exactly why entropy bonuses exist.
Worked example Ex 5 — Multi-action softmax
(C5)
Three actions with logits θ = ( z 1 , z 2 , z 3 ) = ( 1 , 0 , − 1 ) . We sample a 1 and get G = + 1 . Compute the gradient w.r.t. all three logits.
Forecast: the taken action a 1 's logit rises — but what happens to a 2 , a 3 ?
Softmax probabilities. e 1 , e 0 , e − 1 = 2.718 , 1 , 0.368 ; sum = 4.086 .
π = ( 0.665 , 0.245 , 0.090 ) .
Why? We need current probabilities to evaluate the score 1 [ i = k ] − π ( a i ) .
Scores (chosen k = 1 ).
∂ z 1 log π ( a 1 ) = 1 − 0.665 = 0.335
∂ z 2 log π ( a 1 ) = 0 − 0.245 = − 0.245
∂ z 3 log π ( a 1 ) = 0 − 0.090 = − 0.090
Why negatives? Boosting a 1 's probability necessarily steals mass from the others.
Weight by G = + 1 → gradient = ( 0.335 , − 0.245 , − 0.090 ) .
Why? Positive reward, so we move along the score.
Verify: the three scores sum to 0.335 − 0.245 − 0.090 = 0 . ✓ (Softmax scores always sum to zero — probabilities can only be redistributed, not created.)
Worked example Ex 6 — Continuous action, Gaussian policy
(C6)
The action is a real number (e.g. steering angle). Policy: a ∼ N ( μ θ , σ 2 ) with fixed σ = 1 and mean μ θ = θ . We sample a = 3 while θ = 1 , and the return is R = + 2 . Learning rate α = 0.1 .
Forecast: Q-learning would need an arg max over infinitely many angles — impossible. The policy gradient just needs the score of a bell curve. Which way does μ move?
Log-density of a normal. log π θ ( a ) = − 2 σ 2 ( a − μ θ ) 2 + const .
Why this tool? The density is smooth in θ , so its log has a clean derivative — no maximization needed.
Score w.r.t. the mean. ∇ θ log π θ ( a ) = σ 2 a − μ θ = 1 3 − 1 = 2 .
Why? The score points toward the sampled action : if the action was good, shift the mean toward it.
Weighted update. θ ← 1 + 0.1 × 2 × 2 = 1.4 .
Why × R ? We only move toward a = 3 because it paid off (R > 0 ).
Verify: new mean 1.4 is closer to the rewarded action 3 than the old 1 . ✓ Continuous actions handled with zero arg max — see Value-based methods (Q-learning) for the contrast this dodges.
The figure shows the Gaussian centred at μ = 1 ; the sampled a = 3 sits on the right tail; the arrow shows the mean sliding right because R > 0 . If R had been negative, the arrow would flip left — the score's sign is fixed, the reward decides the direction.
Worked example Ex 7 — Baseline rescue
(C7)
Three actions all return large positive numbers: G = ( 100 , 102 , 101 ) . Softmax at θ = ( 0 , 0 , 0 ) so π = ( 3 1 , 3 1 , 3 1 ) . Compare the effective weights on each action's gradient with and without baseline b = G ˉ .
Forecast: without a baseline, which action gets pushed down ?
No baseline. Weights are the raw returns ( 100 , 102 , 101 ) — all strongly positive.
Why bad? Every action's probability is pushed up hard; the informative differences (± 1 ) are swamped by the shared offset of ∼ 101 . High variance.
Baseline b = G ˉ = 3 100 + 102 + 101 = 101 .
Why G ˉ ? It's the sample estimate of V ( s ) , the best variance-minimizing baseline.
Advantages. A = ( 100 − 101 , 102 − 101 , 101 − 101 ) = ( − 1 , + 1 , 0 ) .
Why unbiased? Parent proof: E [ score ⋅ b ] = 0 , so subtracting b leaves the gradient's expectation unchanged.
Verify: the advantages sum to − 1 + 1 + 0 = 0 and their spread (± 1 ) is tiny versus the raw ∼ 101 magnitude. ✓ Now action 2 is clearly promoted, action 1 demoted, action 3 untouched — see Variance reduction in Monte Carlo and Advantage function .
Worked example Ex 8 — Full trajectory with causality & discounting
(C8)
A 3-step episode, rewards r 0 = 1 , r 1 = 0 , r 2 = 2 , discount γ = 0.9 . Compute the reward-to-go weight G t that multiplies each step's score.
Forecast: should the last action be weighted by the full episode reward or only by what came after it?
Reward-to-go definition. G t = ∑ t ′ ≥ t γ t ′ − t r t ′ .
Why not full R ? Causality: an action at t can't change rewards before t ; including them is pure noise.
Compute each.
G 2 = r 2 = 2 .
G 1 = r 1 + γ r 2 = 0 + 0.9 × 2 = 1.8 .
G 0 = r 0 + γ r 1 + γ 2 r 2 = 1 + 0 + 0.81 × 2 = 2.62 .
Why discount? Future reward is worth less; γ t ′ − t shrinks distant terms.
Assemble the estimator. ∇ θ J ≈ ∑ t ∇ θ log π θ ( a t ∣ s t ) G t with weights ( 2.62 , 1.8 , 2 ) .
Why per-term weights? Each action is credited only with the (discounted) future it could influence.
Verify: G 0 = 1 + 0.9 ⋅ G 1 = 1 + 0.9 × 1.8 = 2.62 (recursion consistent) and G 1 = 0 + 0.9 × 2 = 1.8 . ✓ Compare with Generalized Advantage Estimation for the bias–variance dial on these targets.
Worked example Ex 9 — Exam twist
(C9)
"A student writes: since R ( τ ) contains no θ , ∇ θ J = ∇ θ E [ R ] = E [ ∇ θ R ] = E [ 0 ] = 0 . Find the error and give the correct one-line reason. Then: with sigmoid policy θ = 0 , action a 2 sampled, return R = + 4 , compute the update (α = 0.25 )."
Forecast: where exactly does the "= 0 " argument break?
Locate the fallacy. The step E [ ∇ θ R ] is illegal: the expectation itself is taken over τ ∼ π θ , which does depend on θ . You cannot pull ∇ θ inside without also differentiating the sampling distribution.
Why? J = ∫ P θ ( τ ) R ( τ ) d τ — the gradient hits P θ , not R (parent Step 1).
Correct one-liner. ∇ θ J = E τ [ ∇ θ log P θ ( τ ) R ( τ )] via the Log-derivative trick ; θ acts through which trajectories appear , not their reward values.
Now the number. Score for a 2 : ∇ θ log ( 1 − σ ( 0 )) = − σ ( 0 ) = − 0.5 . Weight = + 4 . Update: θ ← 0 + 0.25 × ( − 0.5 ) × 4 = − 0.5 .
Why negative even though R > 0 ? We rewarded a 2 , whose probability is 1 − σ ; increasing a 2 means decreasing θ .
Verify: at θ = − 0.5 , 1 − σ ( − 0.5 ) = σ ( 0.5 ) ≈ 0.622 > 0.5 . The rewarded action a 2 became more likely. ✓ Sign handled correctly.
Recall Rapid self-check
Which sub-quantity carries the sign of "good vs bad"? ::: The weight (return / advantage), not the score.
Near a deterministic policy, whose score dominates the gradient? ::: The rare, surprising action's score (≈ ± 1 ); the confident action's score → 0 .
Do softmax scores over all actions sum to? ::: Zero — probability mass is only redistributed.
Why does baseline subtraction leave the gradient unbiased? ::: E a ∼ π [ score ⋅ b ( s )] = b ( s ) ∇ θ 1 = 0 .
For continuous actions, what replaces the impossible arg max ? ::: The score of the action's density, e.g. ( a − μ ) / σ 2 for a Gaussian.
Mnemonic "SCORE × WEIGHT"
SCORE = direction that makes this action more likely (policy only). WEIGHT = how much we liked it and its sign (reward-to-go minus baseline). Every update on this page is one multiplication.
Policy gradient methods (index 5.2.5) — the parent derivation these examples exercise.
REINFORCE algorithm — the Monte-Carlo estimator used in Ex 1–2, 5, 8–9.
Score function estimator and Log-derivative trick — the machinery behind every score.
Advantage function · Variance reduction in Monte Carlo — Ex 7.
Actor-Critic methods · Generalized Advantage Estimation — where the baseline becomes learned.
Value-based methods (Q-learning) — the arg max Ex 6 sidesteps.