5.2.13 · D2Deep & Advanced RL

Visual walkthrough — Reward shaping and sparse rewards

2,359 words11 min readBack to topic

This is the visual companion to Reward shaping and sparse rewards. If you have not met the ideas of state, reward, return, or policy, we build them here too — assume nothing.


Step 0 — The vocabulary, drawn before it is used

Before any algebra, a handful of words. Each is a picture.

The symbol (gamma) is the discount factor. Why does it exist? A reward now is worth more than the same reward later, so each step into the future we shrink its worth by multiplying by . After steps the shrink factor is .

Figure — Reward shaping and sparse rewards

In the figure the reward sitting two steps away is drawn short because is a shrunk copy of it. That shrinking is the entire meaning of "discounted".

Recall

What does do to a reward steps in the future? ::: Shrinks it — the further away, the smaller its contribution to the return. Under what assumptions do the infinite sums converge? ::: with bounded rewards and bounded .


Step 1 — The potential : a height map over the states

We now invent one new object, the potential.

WHAT: a number per state, nothing more. WHY invent it: we want a "you're getting warmer" signal, and altitude is the cleanest such signal — going downhill toward the goal feels like progress. PICTURE: a landscape where the goal sits in a valley.

Figure — Reward shaping and sparse rewards

The teal surface is . Notice: is defined before we know the agent's plan. It depends only on where you are, never on how you got there. Hold onto that — it is the key.


Step 2 — Turn altitude into a step reward (the shaping term )

We cannot hand the agent an altitude directly; RL only understands rewards per step. So we convert a change in altitude into a reward, using the successor-state notation from Step 0.

Term by term, standing on the picture:

  • — the height of the square you are leaving. You "pay" this.
  • — the height of the successor square you are entering. You "collect" this.
  • in front of — because the collected part is one step in the future, it gets the same shrink every future thing gets. This is not decoration; Step 5 dies without it.
Figure — Reward shaping and sparse rewards

WHY this exact shape and not just ? Because the collected altitude is enjoyed next step, so to stay honest with the return's bookkeeping it must be discounted by . We will literally watch the mismatch cancel only when the is present.


Step 3 — Write the shaped return, then split it

The shaped reward at each step is where . Feed that into the return.

WHAT we just did: replaced by inside the definition of return. WHY: the agent optimizes this quantity now, so we must understand it exactly.

Is it legal to split this infinite sum? Yes — and only because of Step 0. With and bounded, every series here is dominated by a convergent geometric series , so it converges absolutely. Absolute convergence is the precise permission slip that lets us add term-by-term and rearrange without changing the value. So we split:

Our whole job reduces to one question: what is ?


Step 4 — The telescope: watch every middle term die

Write out for the first few steps, and watch the altitudes cancel.

Now collect every appearance of a single altitude, say :

  • It shows up from the first bracket.
  • It shows up from the second bracket.
  • Sum: . Gone.

The identical thing happens for (as from one place, from the next), for , forever. Every interior altitude is collected once with and once with , and they annihilate.

Figure — Reward shaping and sparse rewards

More carefully, the partial sum up to horizon telescopes to The tail term must vanish for the infinite telescope to leave only . It does: since and , we have as . This is exactly why we demanded bounded and in Step 0 — without either, this tail could survive and the clean result would fail.

This is also where the leading earns its keep: the "collect" coefficient must match the "pay" coefficient from the next step. Drop the leading and the two coefficients become vs — they no longer cancel, leaving a leftover proportional to . The telescope is what forces the to be there.

Taking :


Step 5 — Collect: the shaped return is the true return minus a start constant

Put Step 3 and Step 4 together:

Term by term:

  • — what the agent now tries to maximize (with our helper).
  • — the real return we actually care about.
  • — altitude of the start state only. It does not depend on the actions taken, only on where the episode began.

To lift this from single trajectories to values, we need one more object: the policy.

Averaging the boxed identity over all trajectories that a policy produces, the constant simply rides along:

See Value Functions and Bellman Equations for where and come from, and Markov Decision Processes for the full formal setting of policies and trajectories.


Step 6 — Why the policy cannot change (the geometry of "same shift")

The agent picks the action with the biggest . Compare shaped vs true:

WHY the middle equals the right: at a fixed state , the term is the same number for every action . Subtracting the same constant from a list of numbers lowers the whole list equally — the tallest one is still the tallest.

Figure — Reward shaping and sparse rewards

The orange bars (true ) and the teal bars (shaped ) differ by one uniform drop . The winner does not move. This is policy invariance. The helper made the values different but left the decision untouched — exactly what we wanted. See Exploration vs Exploitation for why a better-shaped still speeds learning even though the optimum is unchanged.


Step 7 — Edge cases: , loops, zero potential, and non-potential shaping

A proof you can trust must survive its corners.

(a) The (undiscounted) case and loops. When the partial-sum telescope becomes , and the tail no longer shrinks. So the clean infinite-horizon result needs the setting to be episodic — the agent reaches a terminal state in finite time, where by convention , giving again. In that episodic world consider a closed loop with : step one gives ; step two gives ; total . A loop returns you to where you started, so its shaping telescopes to — no free reward for looping ⇒ no way to hack the shaping. (In the discounted case a non-terminating loop is fine too: the tail handles it automatically.)

(b) The degenerate and cases. If the return is just and — still a constant over , so still no policy change (and the telescope trivially gives ). If instead then and — no shaping at all. Both extremes leave the theorem intact; a correct proof should quietly contain "do nothing" as a case.

(c) Non-potential shaping breaks everything. Suppose just for hugging a wall — this is not of the form . Now the loop in (a) accumulates every pass with no cancelling partner, so , the telescope fails, and can move. The agent hugs walls forever. This is Reward Hacking and Specification Gaming in one line.

Figure — Reward shaping and sparse rewards

The one-picture summary

Figure — Reward shaping and sparse rewards

The final figure compresses the whole chain and is labelled by the step that produced each panel. Reading left to right:

  • (Steps 2–3) raw per-step shaping bonuses stack up along a trajectory (top row);
  • (Step 4) the interior altitudes pairwise annihilate via the telescope, and the tail under our bounded-, assumption (middle);
  • (Steps 5–6) only a single start-state offset survives, which shifts every action's value equally and therefore cannot change the winner (bottom).
Recall Feynman retelling (optional recap — skip if the seven steps already landed)

This box adds no new result; it is a plain-words re-run of the walkthrough for a second pass. Imagine painting every square of a map with a height. The helper reward pays you the height of the square you step onto (shrunk by because it's one step in the future) and charges you the height of the square you step off of. Walk any path and add up all these payments-and-charges: every square you pass through gets paid for once and charged for once — those cancel like and . Because heights are bounded and future amounts keep shrinking (that's ), even the far-future leftover fades to nothing. The only thing that never gets cancelled is the charge for your very first square, fixed the moment you start and independent of your choices. Same fixed amount for every choice ⇒ it can't change which choice is best. That's the magic: the helper changes the numbers but not the decision — it guides without ever tricking you into looping forever. And if the helper isn't built from heights this way, the cancellation fails, a loop pays real money every lap, and the agent will happily spin in circles collecting it.

Related paths from here: relabel failed goals into successes with Hindsight Experience Replay; grow goals from near to far with Curriculum Learning; the full MDP scaffolding lives in Markov Decision Processes. For the same walkthrough in Hindi-English, see 5.2.13 Reward shaping and sparse rewards (Hinglish).