5.1.11 · D2Reinforcement Learning Foundations

Visual walkthrough — Temporal Difference learning

2,014 words9 min readBack to topic

Before any symbols: imagine you are a robot standing on a tile in a room. From wherever you stand, you care about one number — "how good is it to be standing here?" That number is what we are about to build, name, and learn to correct.


Step 1 — What "being somewhere" is worth: the return

WHAT. Picture a chain of tiles the robot walks across. Each time it leaves a tile it collects a little payout — a number that can be positive (good) or negative (a cost). Call the payout collected on the first move , the next , and so on. The little subscript just means "the current moment"; means "one step later".

WHY. To decide "how good is standing here?" we must add up all the payouts we will collect from here onward. But a payout far in the future should count for less — the room might change, we might not get there. So we shrink each future payout by a factor before adding.

PICTURE.

Look at the shrinking bars. The first bar is full height. The next is multiplied by (a number between 0 and 1 — think ), the one after by , and so on. That fading is the discount factor at work.

Adding the faded bars gives the return:


Step 2 — The value is the average return

WHAT. The robot's world is a little random — the same tile might send it left one day and right another, and payouts wiggle. So is not one fixed number; it is a lottery of possible totals.

WHY. We want one trustworthy number per tile, not a lottery. The honest summary of a lottery is its average — the expectation. The symbol means "average over everything that could happen if we follow behaviour rule ".

PICTURE.

Each faint grey line is one possible walk (one possible ). The bold coloured line is their average — that average height is the value.


Step 3 — Peel off the first payout (the recursive trick)

WHAT. Look again at the return sum. Every term after the first shares a common factor of . Factor it out.

WHY. We want to separate what we can actually observe right now () from everything else. The "everything else" is going to turn out to be a return again — one step later — and that is the door to bootstrapping.

PICTURE.

The figure snips the first bar off the front and boxes the rest. The boxed part, once you pull out , is exactly the return starting from the next tile:

This is the single most important line on the page. Read it as: "the whole future = this step's payout, plus a discounted copy of the future starting one tile over."


Step 4 — Turn the recursion into the Bellman equation

WHAT. Take the average of both sides of the Step 3 identity.

WHY. Averaging is linear — the average of a sum is the sum of the averages. That lets us split the right-hand side into two clean pieces we can name.

PICTURE.

Now the magic: the second average — "average return starting from wherever we land next" — is by definition the value of the next tile. So:

This is the Bellman equation (see Bellman Equations). It says a tile's value equals the payout for leaving it, plus the discounted value of where you land — averaged over the randomness. Notice we replaced an infinite tail () with a single number . That swap is called bootstrapping (see Bootstrapping).


Step 5 — From averages we can't compute to one sample we can

WHAT. The Bellman equation contains — a true average over all futures. We do not know that average; if we did, we would already be done. But we can take one real step and watch what happens.

WHY. A single observed transition is a sample of that average. We drop the and use the one thing we actually saw. This is exactly the Monte-Carlo idea of "estimate an average with samples", but applied to only one step instead of the whole episode.

PICTURE.

We call this the TD target. It is our new, better-informed guess for , because it contains one piece of real data (, the payout we truly received) glued to our current estimate of the rest.


Step 6 — Measure how wrong we were: the TD error

WHAT. Compare the fresh target to the old stored number . Their difference is the TD error.

WHY. A guess is only worth correcting by how much it was off. Subtracting old from new gives a signed size-of-mistake: positive means "better than I thought", negative means "worse than I thought".

PICTURE.

Two dots on a number line: old estimate and the target. The arrow between them is the error.


Step 7 — Step, don't jump: the learning rate

WHAT. We do not overwrite with the target. We nudge it a fraction of the way there.

WHY. The target came from one noisy sample. If we jumped fully onto it (that would be ), one unlucky payout would erase everything we learned. A small averages many noisy targets over time — trading a little slowness for stability. This is the Bias-Variance Tradeoff showing up as a knob.

PICTURE.

The dot slides only part-way along the error arrow — a distance .

Unfolding gives the box from the parent note — now every symbol has been earned:


Step 8 — Degenerate cases: check the arrow still makes sense

WHAT / WHY / PICTURE. A good derivation must survive its extremes.

  • Terminal next state. If is terminal, there is no future, so by definition. Target collapses to just : . Pure real data, no bootstrap.
  • (myopic). The whole term vanishes; the target is alone. Each tile only ever learns its immediate payout — no propagation.
  • (undiscounted). The next tile's value counts fully. Fine for episodes that always end; can diverge for endless loops (the return sum has no shrinking to keep it finite).
  • . Target equals old estimate → no update. This does not mean "learned"; a noisy environment produces nonzero even at the true value. What must vanish is the average , not each individual one.
Recall Quick self-check on the edges

If is terminal, what is the TD target? ::: Just , because . With , what does each tile learn? ::: Only its immediate reward ; nothing propagates backward. Does on one step mean learning is finished? ::: No — noise keeps single errors nonzero; only signals convergence.


The one-picture summary

Everything compresses into one loop: observe a payout and next tile form the target subtract the old to get the surprise slide the estimate a fraction toward the target → repeat.

take action

walk on

stand on tile S_t

see reward R and next tile

target = R + gamma times V next

surprise = target minus old V

V moves by alpha times surprise

Recall Feynman retelling — the whole walkthrough in plain words

I want to know how good it is to stand on a tile. "Good" means the total of all payouts I'll collect from here, with far-off payouts faded down by a shrink-factor — that faded total is the return. But payouts are random, so I take the average return; that average is the tile's value.

Here's the clever peel: the whole future equals this step's payout plus a faded copy of the future starting one tile over. Averaging both sides, the "future from the next tile" is just the next tile's value. So a tile's value = payout for leaving + faded value of where I land. That's Bellman.

I can't compute the true average, but I can take one real step and watch the payout. Real payout + my stored guess of the next tile = a slightly smarter guess, the target. How wrong was my old number? Target minus old = the surprise . I don't leap onto the target — one step is noisy — I only slide a fraction of the way. Slide, walk, repeat. That sliding arrow is TD(0), and it's the seed of Q-Learning, SARSA, and TD(λ).


Connected ideas: Bellman Equations · Monte Carlo Methods · Dynamic Programming · Bootstrapping · Bias-Variance Tradeoff · Q-Learning · SARSA · TD(λ) · Value Function Approximation