Tabular n-step TD algorithm
The semi-gradient n-step TD algorithm is the function-approximation extension of tabular n-step TD.
Waiting Before Updating
The n-Step TD Algorithm does not immediately update the value estimate for the state just observed. Instead, it waits for several steps of experience, uses those rewards to construct an n-step return G, and then updates an earlier state estimate. The state being updated is the state at time τ, not necessarily the state at the current time step.
The method connects two operations: first calculate the return for Sτ, then move V(Sτ) toward that return.
Finding the Updated State
As experience arrives one step at a time, the algorithm identifies an earlier time index τ. That index determines which state is eligible for an update. The value changed by the update is V(Sτ). The current time step supplies new experience, but τ points back to the state whose return can now be calculated.
Constructing the Return
The n-step return combines discounted rewards collected from time τ through the n-step boundary. If the episode has not terminated by that boundary, the return also includes a discounted later value estimate, V(Sτ+n). This later estimate is the bootstrapped part of the target. If termination occurs before the boundary, the return uses the rewards available up to termination instead of continuing with a later value estimate.
G = discounted rewards from time τ through time τ+n−1, plus a discounted V(Sτ+n) when the n-step boundary occurs before termination
A three-step target
Suppose the three discounted reward contributions available for Sτ are 2, 1.5, and 0.5. The episode has not terminated at the three-step boundary, and the discounted later value contribution is 3.
Combine rewards: The reward portion of the return is 2 + 1.5 + 0.5 = 4.
Add the continuation estimate: Because the episode has not terminated at the boundary, include the later value contribution of 3.
Form G: The resulting target is G = 4 + 3 = 7.
The n-step return used for Sτ is G = 7.
The numerical contributions in this example are illustrative. The important structure is that several rewards are combined first, and a later value estimate is included only when the episode continues far enough for bootstrapping to apply.
Applying the Value Update
After G has been calculated, the tabular update is V(Sτ) ← V(Sτ) + α[G − V(Sτ)]. The step size α lies in the interval (0, 1], so the update moves the current estimate partway toward G rather than replacing it directly.
Moving toward the return
Suppose V(Sτ) = 4, the calculated return is G = 10, and α = 0.5.
Find the difference: G − V(Sτ) = 10 − 4 = 6.
Scale the difference: α[G − V(Sτ)] = 0.5 × 6 = 3.
Update the estimate: V(Sτ) becomes 4 + 3 = 7.
The updated value estimate is V(Sτ) = 7, not G = 10.
The update changes the value associated with Sτ. It does not directly replace that value with G; α controls how much of the difference is applied.
Return Versus Procedure
The n-step return equation and the complete n-step TD algorithm are related but not identical. The return describes how to construct the target G from rewards and, when appropriate, a later value estimate. The complete algorithm describes when experience is collected, how τ is identified, when the return can be calculated, how terminal time T affects the calculation, and when the update is performed.
Terminal time T is part of the control flow, not merely a detail of the return formula. While the process is before T, the algorithm gathers the next reward and state. Once termination occurs, it must stop extending the experience and complete the updates that are still pending. The reward sum is also limited by termination, so the algorithm must not bootstrap beyond the terminal point.
From Tables to Function Approximation
| Part of the method | Tabular n-step TD | Semi-gradient n-step TD |
|---|---|---|
| Experience structure | Uses several steps of episode experience | Uses the same multi-step experience structure |
| Target | Uses an n-step return G | Uses a generalized n-step return |
| Value representation | Uses tabular state values | Uses a function-approximation value estimate |
| Conceptual relationship | Base method | Natural function-approximation extension |
The semi-gradient n-step TD algorithm is not presented as an unrelated method. It begins with the tabular n-step TD method, generalizes the n-step return, and uses that generalized return in a function-approximation setting. The key conceptual path is therefore tabular method, generalized return, then semi-gradient method.
Control-Flow Mistakes
Updating a value when τ is negative.
A negative τ refers to an earlier index that is not yet available for the algorithm's update scheme.
Fix:
Check the update condition and proceed only when τ is eligible.Never updating when τ is nonnegative.
An eligible state has been identified, but control flow prevents its return from being calculated or its value from being changed.
Fix:
Trace the condition around τ, the return calculation, and the update statement.Bootstrapping after the episode has terminated.
The return should stop at termination and omit the continuation value beyond the terminal point.
Fix:
Use the later value estimate only when the episode continues to the n-step boundary.Treating the return equation as the whole algorithm.
The equation defines the target, while the complete procedure determines when that target can be used.
Fix:
Check both target construction and the surrounding time and termination control flow.Replacing V(Sτ) directly with G.
The tabular update moves the estimate by α[G − V(Sτ)], using a fraction controlled by the step size.
Fix:
Apply the full update expression and verify that α is in (0, 1].
Practice Check
An implementation has collected enough experience for an earlier state Sτ. The episode has not terminated at the n-step boundary, and the return calculation is complete. Explain which value is updated, what target it moves toward, and why the later value estimate is included.
Hints
- The time index in the update is τ, not the current time index.
- The target is the n-step return G.
- The later value estimate is included because termination has not occurred at the boundary.
A terminal transition occurs before the n-step boundary. List the two changes this causes in the return and in the continuing control flow.
Hints
- Consider whether a later value estimate can still be used.
- Consider whether the algorithm should continue gathering new experience.
Key Takeaways
- n-step TD waits for several steps of experience before updating an earlier state estimate.
- The return G combines discounted rewards and, when the episode continues to the boundary, a discounted later value estimate.
- The update changes V(Sτ) using V(Sτ) ← V(Sτ) + α[G − V(Sτ)].
- The return equation is one part of the complete algorithm; τ, terminal time T, eligibility checks, and pending updates belong to the surrounding procedure.
- Semi-gradient n-step TD extends the tabular method by generalizing the return for function approximation.
Key Takeaways
- The algorithm delays an update so that an earlier state can use several rewards.
- τ identifies the state whose value estimate is updated.
- G contains discounted rewards and conditionally includes a later value estimate.
- The complete procedure must handle τ, terminal time T, update eligibility, and pending updates.
- The semi-gradient version is the function-approximation extension of tabular n-step TD.