5.2.13Deep & Advanced RL

Reward shaping and sparse rewards

2,108 words10 min readdifficulty · medium

WHAT is a sparse reward?

WHY it's hard: RL learns from the return Gt=k0γkrt+kG_t=\sum_{k\ge0}\gamma^k r_{t+k}. If almost all r=0r=0, then Q(s,a)0Q(s,a)\approx 0 everywhere the agent has actually visited, so no action looks better than any other → no policy improvement until the goal is hit by luck. The probability of hitting it by luck decays exponentially with the number of steps required.


WHAT is reward shaping?

The danger: a naïve FF can create reward hacking — the agent finds a loop that milks FF without ever solving the task.


HOW to shape safely: Potential-Based Reward Shaping (PBRS)

Derivation from first principles — WHY the optimal policy is preserved

We derive it, not memorize it.

Step 1 — Write the shaped return. For a trajectory s0,s1,s2,s_0,s_1,s_2,\dots the shaped return is G0=t=0γt(rt+γΦ(st+1)Φ(st)).G'_0=\sum_{t=0}^{\infty}\gamma^t\big(r_t + \gamma\Phi(s_{t+1})-\Phi(s_t)\big). Why this step? By definition the return is the discounted sum of the shaped reward rt=rt+Ftr'_t = r_t + F_t.

Step 2 — Split the sum. G0=tγtrtG0+t=0γt(γΦ(st+1)Φ(st)).G'_0=\underbrace{\sum_{t}\gamma^t r_t}_{G_0}+\sum_{t=0}^{\infty}\gamma^t\big(\gamma\Phi(s_{t+1})-\Phi(s_t)\big). Why? Sum of a sum splits linearly.

Step 3 — Telescope the shaping part. Let T=tγt(γΦ(st+1)Φ(st))T=\sum_t\gamma^t(\gamma\Phi(s_{t+1})-\Phi(s_t)). Write it out: T=(γΦ(s1)Φ(s0))+γ(γΦ(s2)Φ(s1))+γ2(γΦ(s3)Φ(s2))+T=\big(\gamma\Phi(s_1)-\Phi(s_0)\big)+\gamma\big(\gamma\Phi(s_2)-\Phi(s_1)\big)+\gamma^2\big(\gamma\Phi(s_3)-\Phi(s_2)\big)+\cdots Group terms with Φ(s1)\Phi(s_1): coefficient =γγ1=0= \gamma - \gamma\cdot 1 = 0. Same for Φ(s2)\Phi(s_2): γ2γ2=0\gamma^2 - \gamma^2 = 0. Everything cancels except Φ(s0)-\Phi(s_0). Why? Each Φ(st)\Phi(s_{t}) appears with +γγt1=γt+\gamma\cdot\gamma^{t-1}=\gamma^{t} from one term and γt-\gamma^{t} from the next — they annihilate. This is a telescoping series.

Step 4 — Collect. G0=G0Φ(s0).G'_0 = G_0 - \Phi(s_0).

Step 5 — Interpret. The shaped value differs from the true value by a constant that depends only on the start state, not on the actions: Vπ(s)=Vπ(s)Φ(s),Qπ(s,a)=Qπ(s,a)Φ(s).V'^{\pi}(s)=V^{\pi}(s)-\Phi(s),\qquad Q'^{\pi}(s,a)=Q^{\pi}(s,a)-\Phi(s). Why this is the punchline: argmaxaQ(s,a)=argmaxa(Q(s,a)Φ(s))=argmaxaQ(s,a)\arg\max_a Q'(s,a)=\arg\max_a\big(Q(s,a)-\Phi(s)\big)=\arg\max_a Q(s,a) because Φ(s)\Phi(s) is a constant offset over aa. So the greedy/optimal policy is identical. ∎

Figure — Reward shaping and sparse rewards

Other tools for sparse rewards (the 80/20)

  • Intrinsic motivation / curiosity: add F=ηprediction errorF = \eta\,\|\text{prediction error}\| or a count bonus F=β/N(s)F=\beta/\sqrt{N(s)} so the agent is rewarded for visiting novel states → explores until it finds the real reward.
  • Hindsight Experience Replay (HER): if the agent aimed for goal gg but reached gg', relabel the episode as if gg' was the goal. Now a "failure" becomes a "success" → dense synthetic learning signal, no reward hacking.
  • Curriculum learning: start with goals close to the agent (dense success), gradually move them farther.

Worked examples


Common mistakes (Steel-man them)


Flashcards

Why is a sparse reward hard to learn from?
Almost all r=0r=0, so QQ is flat over visited states → no action looks better → policy can't improve until the goal is hit by rare chance.
What is reward shaping?
Adding an extra term FF to the reward, R=R+FR'=R+F, to guide learning toward the goal.
State the potential-based shaping formula.
F(s,a,s)=γΦ(s)Φ(s)F(s,a,s')=\gamma\Phi(s')-\Phi(s) for any potential Φ:SR\Phi:\mathcal S\to\mathbb R.
Why does PBRS keep the optimal policy unchanged?
The shaping telescopes so Q(s,a)=Q(s,a)Φ(s)Q'(s,a)=Q(s,a)-\Phi(s); Φ(s)\Phi(s) is constant over aa, so argmaxa\arg\max_a is unchanged.
What is the ideal choice of potential Φ\Phi?
Φ(s)V(s)\Phi(s)\approx V^*(s), which makes good actions distinguishable from step one.
Why do loops give no free reward under PBRS?
Bonus entering a state =γΦ(s)Φ(s)=\gamma\Phi(s')-\Phi(s) is cancelled by leaving it; over a closed loop the sum telescopes to zero.
What is Hindsight Experience Replay (HER)?
Relabel a failed episode's goal as the state actually reached, turning failures into successful (dense) learning examples.
What is a curiosity/intrinsic bonus and when do you use it?
A reward for visiting novel states (e.g. prediction error or 1/N(s)1/\sqrt{N(s)}); used when you have no goal heuristic to build Φ\Phi.
What happens if you drop the γ\gamma in the PBRS formula?
Telescoping leaves residual (1γ)Φ\propto(1-\gamma)\Phi terms → policy invariance breaks in discounted MDPs.

Recall Feynman: explain to a 12-year-old

You're playing "hot or cold" but blindfolded and the friend only shouts "found it!" once you touch the hidden toy — silence otherwise. You'd wander forever. Reward shaping is your friend now whispering "warmer... warmer... colder." That helps! But there's a rule: whenever you step closer they give you a candy, and whenever you step back they take one away — exactly the candy back. So walking in circles earns you nothing; only actually finding the toy pays off. That's why we can help you and never trick you into pacing back and forth for candy.

Connections

Concept Map

almost always zero

Q near zero everywhere

hit goal by luck

adds helper term F

fixes

naive F

example

safe form of

F equals gamma Phi s' minus Phi s

loops sum to zero

prevents

Ng Harada Russell 1999

Sparse reward

No learning signal

No policy improvement

Exponentially rare exploration

Reward shaping

Shaped reward R plus F

Reward hacking

Boat spins on checkpoints

Potential-based shaping

Potential function Phi

Policy invariance

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, RL mein sabse bada problem hota hai sparse reward. Matlab agent ko reward tabhi milta hai jab woh finally goal tak pahunch jaaye — beech mein har step pe zero. To jab tak luck se goal nahi milta, agent ko koi signal hi nahi milta ki "sahi ja raha hoon ya galat". Isliye learning atak jaati hai. Ise samajhne ka easy tareeka: blindfold "hot-cold" game, jisme dost sirf "mil gaya!" bolta hai, warna chup — tum toh ghumte hi rehte ho.

Reward shaping ka idea hai ek helper reward FF add kar do, jaise "goal ke paas ja rahe ho to thoda plus, door ja rahe ho to minus". Isse har step pe ek gradient mil jaata hai. Par ek khatra hai: agar FF galat design kiya, to agent asli kaam chhod ke sirf FF ko farm karega — jaise boat race wali agent circle mein ghoom ke checkpoints re-hit karti rehti hai. Isko reward hacking kehte hain.

Iska safe solution hai Potential-Based Reward Shaping (PBRS): F=γΦ(s)Φ(s)F=\gamma\Phi(s')-\Phi(s) lo, jahan Φ\Phi koi bhi potential function hai (best choice ΦV\Phi\approx V^*). Iska magic ye hai ki jab tum kisi loop mein ghoomoge, to andar aane ka bonus, bahar jaane ke penalty se exactly cancel ho jaata hai (telescoping series). Isliye loop se koi free reward nahi milta, aur maths proof karta hai ki optimal policy same rehti hai — kyunki Q(s,a)=Q(s,a)Φ(s)Q'(s,a)=Q(s,a)-\Phi(s), aur Φ(s)\Phi(s) toh action pe depend hi nahi karta.

Jab tumhare paas koi heuristic nahi hai (pata hi nahi goal kahan hai), tab curiosity bonus (naye states explore karne pe reward) ya HER (fail hui episode ka goal relabel karke success bana do) use karo. Yahi 20% cheezein 80% problems solve kar deti hain. Yaad rakho: shaping madad kar sakti hai, par PBRS form use karke hi taaki cheating na ho.

Go deeper — visual, from zero

Test yourself — Deep & Advanced RL

Connections