This is the "grind through every case" companion to the parent A2C/A3C note . There we derived that the TD error is the advantage estimate:
δ t = what actually + expected r t + γ V ϕ ( s t + 1 ) − what we predicted V ϕ ( s t ) .
Here we make sure you never meet a case you haven't seen: positive/negative/zero advantage, terminal states, γ = 0 and γ = 1 , the n -step dial, GAE smoothing, entropy, and an exam twist.
Intuition Read one symbol before we start
Every quantity below is a plain number. V ϕ ( s ) = the critic's guess of "total future reward starting from state s ." r t = the reward you actually just got . γ (gamma) = a dial in [ 0 , 1 ] for how much the future counts (0 = ignore future, 1 = future counts fully). δ t = the surprise : reality minus prediction. Positive surprise ⇒ "do that action more." Negative ⇒ "do it less."
Every A2C advantage computation falls into one of these cells. The examples below are each tagged with the cell they cover.
Cell
What makes it special
Covered by
C1 Positive advantage
reality beat the critic, δ t > 0
Ex 1
C2 Negative advantage
reality was worse, δ t < 0
Ex 2
C3 Zero advantage
critic was exactly right, δ t = 0
Ex 3
C4 Terminal state
no next state, so V ϕ ( s t + 1 ) = 0
Ex 4
C5 γ extremes
γ = 0 (myopic) and γ = 1 (undiscounted)
Ex 5
C6 n -step dial
use n real rewards then bootstrap
Ex 6
C7 GAE blend
smoothly mix all n via λ
Ex 7
C8 Entropy + loss sign
full actor loss with the β H bonus
Ex 8
C9 Word problem
real story maps onto numbers
Ex 9
C10 Exam twist
stop-gradient / on-policy trap
Ex 10
The picture above is the mental model for every cell: a number line where the critic's prediction sits at zero, and δ t is the signed distance of reality from it.
Worked example Example 1 — Positive advantage (cell C1)
γ = 0.9 , V ϕ ( s t ) = 5 , r t = 2 , V ϕ ( s t + 1 ) = 6 . Find δ t and say which way the actor moves.
Forecast: guess the sign before computing — the next state is valued higher (6 vs 5) and we also got a reward, so… ?
Compute the bootstrap target r t + γ V ϕ ( s t + 1 ) = 2 + 0.9 ( 6 ) = 2 + 5.4 = 7.4 .
Why this step? This is our best one-step estimate of Q ( s t , a t ) — "reward now plus discounted value of where we landed."
Subtract the old prediction: δ t = 7.4 − 5 = 2.4 .
Why this step? δ t = Q − V is the advantage: how much better the action turned out than the state's average.
Actor direction: loss = − log π ⋅ δ t = − 2.4 log π . Minimizing this raises log π , i.e. makes a t more likely .
Why this step? Positive surprise ⇒ reinforce.
Verify: target 7.4 > 5 , so δ t > 0 ✓. Critic will nudge V ϕ ( s t ) up toward 7.4 . Sign matches forecast.
Worked example Example 2 — Negative advantage (cell C2)
Same critic (V ϕ ( s t ) = 5 , γ = 0.9 ) but a bad outcome: r t = − 1 , V ϕ ( s t + 1 ) = 4 .
Forecast: we got a penalty and landed somewhere valued lower (4<5). Positive or negative δ ?
Target = − 1 + 0.9 ( 4 ) = − 1 + 3.6 = 2.6 .
Why this step? Same Q -estimate recipe; the negative reward drags it down.
δ t = 2.6 − 5 = − 2.4 .
Why this step? Reality (2.6) fell below prediction (5) ⇒ negative advantage.
Actor: − log π ⋅ ( − 2.4 ) = + 2.4 log π . Minimizing this lowers log π ⇒ a t becomes less likely .
Why this step? Worse-than-average ⇒ steer away.
Verify: magnitude equals Ex 1 (2.4) but opposite sign — a nice symmetric sanity check ✓.
Worked example Example 3 — Zero advantage (cell C3)
γ = 0.9 , V ϕ ( s t ) = 5 , r t = 0.5 , V ϕ ( s t + 1 ) = 5 .
Forecast: what value of δ t leaves the actor unchanged ?
Target = 0.5 + 0.9 ( 5 ) = 0.5 + 4.5 = 5.0 .
δ t = 5.0 − 5 = 0 .
Why this step? Critic predicted perfectly; nothing was surprising.
Actor gradient = − log π ⋅ 0 = 0 ⇒ no policy update from this step.
Why this step? A2C only nudges the policy when reality differs from the critic — no news, no change.
Verify: the critic loss 2 1 δ t 2 = 0 too, so both networks are momentarily satisfied ✓. This is the fixed-point behaviour you want at convergence.
Worked example Example 4 — Terminal state (cell C4)
Last step of an episode: γ = 0.9 , V ϕ ( s t ) = 3 , final reward r t = 10 , and s t + 1 is terminal .
Forecast: what value do we plug in for V ϕ ( terminal ) ?
Set V ϕ ( s t + 1 ) = 0 for terminal states.
Why this step? There is no future after the episode ends — future value is zero by definition , not by the critic's guess. Forgetting this is a classic bug (the critic's raw output for a terminal state is meaningless).
Target = 10 + 0.9 ( 0 ) = 10 .
δ t = 10 − 3 = 7 .
Why this step? A big positive surprise ⇒ strongly reinforce the winning final action.
Verify: if we had wrongly used V ϕ ( s t + 1 ) = 6 , we'd get δ t = 10 + 5.4 − 3 = 12.4 — inflated by 5.4 . So the terminal mask changes the answer by exactly γ V ϕ ( s t + 1 ) ✓.
Worked example Example 5 —
γ at both extremes (cell C5)
Fixed data: V ϕ ( s t ) = 5 , r t = 2 , V ϕ ( s t + 1 ) = 6 . Compute δ t for γ = 0 then γ = 1 .
Forecast: which γ makes the future matter more, and so gives the larger δ ?
γ = 0 (myopic): δ t = 2 + 0 ⋅ 6 − 5 = − 3 .
Why this step? γ = 0 means "only the immediate reward counts." Here r t = 2 alone can't justify a state worth 5, so the advantage is negative.
γ = 1 (undiscounted): δ t = 2 + 1 ⋅ 6 − 5 = 3 .
Why this step? Now the full next-state value is credited; landing in a valuable state is rewarded.
Verify: the two answers straddle zero, and the γ = 0.9 answer from Ex 1 (2.4) lies between them, close to the γ = 1 end ✓. The dial monotonically moves δ t from − 3 to + 3 as γ : 0 → 1 .
Worked example Example 6 —
n -step advantage (cell C6)
γ = 0.9 , rewards r t = 1 , r t + 1 = 2 , V ϕ ( s t ) = 5 , V ϕ ( s t + 2 ) = 8 . Use the 2-step estimate.
Forecast: more real rewards ⇒ do we trust the environment more or the critic more?
Sum the discounted real rewards: 1 + 0.9 ( 2 ) = 1 + 1.8 = 2.8 .
Why this step? We're using two actual rewards before handing off to the critic — this leans on reality.
Add the bootstrapped tail: + 0. 9 2 ( 8 ) = 0.81 ⋅ 8 = 6.48 .
Why this step? After 2 steps we still stop and ask the critic for the rest; γ n discounts it.
Subtract the baseline: A ^ t ( 2 ) = 2.8 + 6.48 − 5 = 4.28 .
Verify: the 1-step estimate here would be 1 + 0.9 V ϕ ( s t + 1 ) − 5 ; using more real reward raises variance but reduces reliance on a rough critic — consistent with the bias–variance dial ✓.
Worked example Example 7 — GAE blend (cell C7)
Generalized Advantage Estimation (GAE) blends all n -step advantages with weight λ :
A ^ t GAE = ∑ l ≥ 0 ( γ λ ) l δ t + l .
Take a 3-step window with per-step TD errors δ t = 1.0 , δ t + 1 = 0.5 , δ t + 2 = − 0.2 , γ = 0.9 , λ = 0.95 .
Forecast: λ close to 1 — does GAE weight the later errors heavily or lightly?
Compute the decay factor γ λ = 0.9 × 0.95 = 0.855 .
Why this step? This single number controls how fast future TD errors are down-weighted — it's the GAE "memory."
Weighted sum:
A ^ t GAE = 1.0 + 0.855 ( 0.5 ) + 0.85 5 2 ( − 0.2 ) .
Evaluate: 1.0 + 0.4275 + 0.731025 ( − 0.2 ) = 1.0 + 0.4275 − 0.146205 = 1.281295 .
Why this step? GAE ≈ an exponentially-weighted average of every n -step estimate at once — one number, smoothly tunable.
Verify: with λ = 0 GAE collapses to the 1-step δ t = 1.0 ; our λ = 0.95 answer (≈ 1.28 ) is larger because later positive error δ t + 1 still counts ✓.
Worked example Example 8 — Entropy in the full actor loss (cell C8)
A 2-action policy at s t : π = ( 0.8 , 0.2 ) , we took action 1 (prob 0.8), A ^ t = 2.4 , entropy coefficient β = 0.1 .
Forecast: the entropy bonus − β H — does it raise or lower the loss for a spread-out policy?
Policy-gradient term: − log π ( a t ∣ s t ) A ^ t = − log ( 0.8 ) ⋅ 2.4 . With log ( 0.8 ) ≈ − 0.22314 , this is − ( − 0.22314 ) ( 2.4 ) = 0.53554 .
Why this step? Standard actor loss; a positive advantage on a confident action gives a positive loss whose gradient still increases that action's probability.
Entropy H = − ∑ π log π = − ( 0.8 log 0.8 + 0.2 log 0.2 ) . Compute: − ( 0.8 ( − 0.22314 ) + 0.2 ( − 1.60944 )) = − ( − 0.17851 − 0.32189 ) = 0.50040 .
Why this step? H measures how "spread out" the policy is; higher H = more exploration.
Full loss L π = 0.53554 − β H = 0.53554 − 0.1 ( 0.50040 ) = 0.53554 − 0.05004 = 0.48550 .
Why this step? The − β H term subtracts value for staying spread out, so minimizing the loss resists collapsing to a deterministic policy.
Verify: max entropy for 2 actions is log 2 ≈ 0.693 ; our H = 0.500 is below that (policy isn't uniform) ✓, and the entropy term shaved 0.05 off the loss as expected.
Worked example Example 9 — Word problem (cell C9)
A delivery robot in state s t = "at the depot." The critic thinks the depot is worth V ϕ = 40 (expected future packages). The robot takes a shortcut (its action), immediately loses r t = − 5 (traffic fine) but reaches a hub the critic values at V ϕ ( s t + 1 ) = 55 . Discount γ = 0.9 . Should the actor learn to take that shortcut again?
Forecast: short-term penalty but a much better next position — reinforce or avoid?
Target = − 5 + 0.9 ( 55 ) = − 5 + 49.5 = 44.5 .
Why this step? We trade the fine against the discounted value of a great next state.
δ t = 44.5 − 40 = 4.5 > 0 .
Why this step? Despite the fine, the move beat the depot's average expectation.
Actor loss − log π ⋅ 4.5 ⇒ shortcut becomes more likely .
Why this step? Positive advantage ⇒ the long-term gain outweighs the short-term cost.
Verify: flip intuition — if the hub were only worth V ϕ ( s t + 1 ) = 45 , then target = − 5 + 40.5 = 35.5 , giving δ t = − 4.5 < 0 and the robot would learn to avoid the shortcut ✓. The sign hinges entirely on whether the future gain covers the fine.
Worked example Example 10 — Exam twist: the stop-gradient trap (cell C10)
The critic target is y = r t + γ V ϕ ( s t + 1 ) and the critic loss is 2 1 ( y − V ϕ ( s t ) ) 2 . A student differentiates through both V ϕ terms. With γ = 0.9 , r t = 2 , V ϕ ( s t + 1 ) = 6 , V ϕ ( s t ) = 5 , contrast the intended vs buggy behaviour.
Forecast: should the gradient try to change V ϕ ( s t + 1 ) or leave it fixed?
Correct: stop-gradient the target. y = 7.4 is treated as a constant . Loss = 2 1 ( 7.4 − 5 ) 2 = 2 1 ( 2.4 ) 2 = 2.88 ; gradient only pushes V ϕ ( s t ) up toward 7.4 .
Why this step? We want V ϕ ( s t ) to predict the target, not to move the target — otherwise we're chasing our own tail.
Buggy: differentiating y too lets the optimizer lower V ϕ ( s t + 1 ) to shrink the loss trivially, corrupting values (the "moving goalposts" bug from the parent note).
Why this step? Minimizing ( y − V ϕ ( s t ) ) 2 by dragging y down is cheating, not learning value.
Verify: with stop-grad, loss = 2 1 ( 2.4 ) 2 = 2.88 ✓. The advantage used by the actor, δ t = 2.4 , is exactly Ex 1 — the actor and critic share the same TD error, but the critic target must stay frozen for the actor's signal to remain meaningful ✓.
compute delta = r + gamma V next - V now
horizon dial: one step vs n step vs GAE
Mnemonic One sentence for the whole matrix
"Target minus prediction, then read the sign." Build the target (r + γ V ′ , with V ′ = 0 if terminal), subtract the old value, and the sign tells the actor which way to move — everything else (n-step, GAE, γ ) just changes how you build the target.
Recall
When δ t = 0 , what does the actor do? ::: Nothing — the gradient − log π ⋅ 0 = 0 ; the critic already predicted perfectly.
What do you plug in for V ϕ ( s t + 1 ) at a terminal state? ::: Zero — there is no future after the episode ends.
With γ = 0 , the advantage reduces to what? ::: r t − V ϕ ( s t ) (only the immediate reward, no bootstrap).
Why treat the critic target as a constant (stop-gradient)? ::: So the network predicts the target instead of moving it down to trivially shrink the loss.
GAE decay factor per step? ::: γ λ — it exponentially down-weights later TD errors.