Search

Search papers and lessons

Sparse Reward Problem

Level 3 · 10 min read

A sparse reward problem is one where the agent gets feedback only at rare moments, so random exploration rarely finds a reward and learning stalls.

On this page

  • Colour = topic mix
  • Probability
  • Linear algebra
  • Reinforcement learning
  • Stochastic processes
  • Discrete math
  • Sign in to see your progress

TL;DR

A sparse reward problem is a reinforcement learning task where almost every step gives reward zero, and only a few states, such as reaching a goal, give a nonzero reward. The agent learns from reward, so with almost no signal it has little to improve on, and random exploration can take a very long time to find the first reward. The difficulty grows with how far the goal is from where the agent starts.

Where you'll see it: robot manipulation, where the only reward is "the object is in the target place", and games where the score arrives only at the end of a level. It is the problem that hindsight experience replay was designed to address.

Intuition

A treasure hunt with no warmer or colder

Imagine a long corridor with a treasure at one end. You are blindfolded and can take one step left or right at a time. At each step someone tells you "nothing" until you find the treasure, when they say "found it". You have no sense of whether you are getting closer.

A blindfolded walker in a corridor with no feedback will wander. Each step is a coin toss, so the walk tends to drift back and forth. The reward, the treasure, is far away, and the only information you get is that you have not found it yet. Learning in this setting is hard for the same reason: nothing distinguishes a good step from a bad one until the treasure is reached.

Now suppose the same person says "warmer" or "colder" at every step. The search becomes easy. That is the difference between a dense reward, which gives feedback often, and a sparse one, which gives it rarely.

Why the agent cannot learn what it never sees

In reinforcement learning, the agent improves its choices using the reward it receives. A Value Function estimates the total future reward from each state, and an update moves the estimate towards what was actually observed. If every observed reward is zero, every estimate stays at zero, and the update has nothing to move towards. The agent is not failing to learn the right thing. It has no evidence about what is right.

51015200100200300400distance to reward (steps)steps
  • expected steps to first reward
  • distance to reward
For a random walker, the expected time to reach a reward at distance N grows like N squared, while the distance itself grows only linearly.

The math

A sparse reward problem is a Markov decision process (S,A,P,R,γ)(\mathcal{S}, \mathcal{A}, P, R, \gamma) in which the reward is zero for nearly all state-action pairs. Let Rt+1R_{t+1} be the reward after step tt. Sparsity means that for most times tt, Rt+1=0R_{t+1} = 0, and that the set of states with a nonzero reward is small compared with S\mathcal{S}.

The return and discounting

The quantity that the agent maximises is the discounted return

Gt=∑k=0∞γkRt+k+1,0≤γ<1,G_t = \sum_{k=0}^{\infty} \gamma^k R_{t+k+1}, \qquad 0 \le \gamma < 1,

where γ\gamma is the discount factor. Under sparsity, GtG_t is zero for most starting points, because the first nonzero term is far in the future. A reward that arrives kk steps later is worth γk\gamma^k of its face value now. For example, with γ=0.9\gamma = 0.9 and a reward ten steps away, the discount is 0.910≈0.3490.9^{10} \approx 0.349. So even the signal that does exist is weakened by distance.

How long a random walker waits

Take the simplest sparse task. States are the integers 0,1,…,N0, 1, \dots, N on a line. The agent starts at 00, moves left or right with equal chance, and stays at 00 if it tries to move left from there. The only reward is at NN, where the episode ends.

Let EkE_k be the expected number of steps to reach NN from state kk. Then EN=0E_N = 0, and for 0<k<N0 < k < N

Ek=1+12Ek−1+12Ek+1,E0=1+E1.E_k = 1 + \tfrac{1}{2} E_{k-1} + \tfrac{1}{2} E_{k+1}, \qquad E_0 = 1 + E_1.

The solution is E0=N2E_0 = N^2. Check for N=2N = 2: E1=1+12E0E_1 = 1 + \tfrac{1}{2} E_0 and E0=1+E1E_0 = 1 + E_1. Substituting gives E0=2+12E0E_0 = 2 + \tfrac{1}{2} E_0, so E0=4=22E_0 = 4 = 2^2.

The result is the main reason sparse rewards are hard. The expected time grows with the square of the distance, so doubling the length of the corridor quadruples the wait. A real task has far more than one dimension, and the number of states grows much faster than the length of the corridor.

Reward shaping

One remedy is to add an extra term to the reward. A common form is potential-based shaping. Choose a function Φ(s)\Phi(s) that estimates progress, and define the shaped reward as

F(s,s′)=γΦ(s′)−Φ(s).F(s, s') = \gamma \Phi(s') - \Phi(s).

Adding FF to the reward leaves the optimal policy unchanged, a result that the reward shaping literature established in the late 1990s. Along any trajectory the shaping terms telescope, so their discounted sum depends only on the start and end states. The usual choice is Φ(s)=−d(s,goal)\Phi(s) = -d(s, \text{goal}), which rewards moving closer to the goal.

Worked examples

Example
Waiting time on a corridor of ten steps

Use the corridor from the math section with N=10N = 10, a random walk starting at 00, and reward only at 1010.

  1. The expected number of steps to reach 1010 is E0=N2=100E_0 = N^2 = 100.
  2. Doubling the corridor to N=20N = 20 gives E0=400E_0 = 400.
  3. Under γ=0.9\gamma = 0.9, a reward at 1010 steps is worth 0.910≈0.3490.9^{10} \approx 0.349 of its face value at the start.
  4. Combining both: the expected wait is 100 steps, and the reward that finally arrives is worth about a third of its face value.

Interpretation: the agent spends a hundred steps on average before it gets any signal at all. Doubling the corridor makes the wait four times as long, even though the reward itself has not changed.

Example
One shaping term on a grid

A robot is on a grid, and the goal is at distance 5 from its starting cell. Use γ=0.9\gamma = 0.9 and Φ(s)=−d(s,goal)\Phi(s) = -d(s, \text{goal}).

  1. Before the move, the distance is 5, so Φ(s)=−5\Phi(s) = -5.
  2. After one step towards the goal, the distance is 4, so Φ(s′)=−4\Phi(s') = -4.
  3. The shaping term is F(s,s′)=0.9⋅(−4)−(−5)=−3.6+5=1.4F(s, s') = 0.9 \cdot (-4) - (-5) = -3.6 + 5 = 1.4.
  4. A move away from the goal gives Φ(s′)=−6\Phi(s') = -6, so F=0.9⋅(−6)+5=−0.4F = 0.9 \cdot (-6) + 5 = -0.4.

Interpretation: the shaped reward is positive for progress and negative for retreat, so the agent gets a signal on every step. Over a whole trajectory the shaping terms add up to a value that depends only on the start and end cells, so the best policy for the original task is unchanged.

Example
Why one reward in a million samples is not enough

Suppose a policy succeeds on a random 1 in 1,000,000 episodes, and each episode takes 100 steps.

  1. Expected number of episodes to see one success: 1,000,0001{,}000{,}000.
  2. Expected number of steps: 1,000,000⋅100=1081{,}000{,}000 \cdot 100 = 10^8.
  3. At 1,000 steps per second of simulation, that is 10510^5 seconds, about 28 hours.

Interpretation: a sparse signal can be rare enough that collecting it takes days even with fast simulation. This is why the methods that help are the ones that either make the rare event more common, through shaping or better exploration, or extract more from each rare event, through relabelling.

Tradeoffs and failure modes

Sparse rewards are often the true objective, and they are the honest specification. Dense rewards are easier to learn from but can mislead the agent. The table summarises the usual choices.

SituationProblemBetter choice
The goal is reachable by chanceRandom exploration finds reward eventuallyPlain RL with a long training budget
The goal is far from the startWaiting time grows with the square of the distanceShaping, or curriculum that starts close to the goal
Many goals in one environmentA reward for only one goal wastes most experienceRelabelling past experience with the goals it reached, as in Hindsight Experience Replay
The shaping term is hand-designedThe agent may optimise the shaping rather than the taskPotential-based shaping, which preserves the optimal policy
Dense intrinsic rewards from curiosityThe agent may chase novelty rather than the taskUse the task reward for evaluation, and anneal the bonus

Failure modes to watch for:

  • Reward hacking under shaping. A shaping term that is not potential-based can change the optimal policy, so the agent learns to collect shaping reward instead of finishing the task. Potential-based shaping avoids this.
  • Silent plateaus. A sparse-reward agent can show a flat learning curve for a long time and look broken. The flat curve is the expected behaviour before the first success, not evidence of a bug.
  • Overfitting to the rare successes. With very few successes, the value estimates rest on little data. Check how many successful episodes the agent has actually seen.
  • Choosing a discount that erases the signal. A small γ\gamma makes distant rewards nearly worthless. Lowering γ\gamma to speed up learning can stop the agent from valuing the goal at all.

History

Reward as the only training signal is the core idea of Reinforcement Learning as presented in the textbook by Sutton and Barto, and the delay between an action and its reward is a standard difficulty in it. The sparse reward problem is the extreme form of that delay, where reward is so rare that the delay dominates.

Reward shaping as a principled tool was developed in the late 1990s. The result that potential-based shaping preserves the optimal policy is usually attributed to Ng, Harada and Russell, and it is the reason the form γΦ(s′)−Φ(s)\gamma \Phi(s') - \Phi(s) is preferred over arbitrary bonuses.

Hindsight relabelling, which turns failed episodes into successes for a different goal, was proposed more recently, and it addresses sparsity by reusing the experience the agent already has.

Check yourself

QuestionIn your own words, why does a sparse reward make learning hard even if the agent is capable of solving the task?Show answer

The agent learns by moving its value estimates towards observed rewards. If almost every observed reward is zero, the estimates have nothing to move towards, so the agent cannot tell a good action from a bad one until it reaches a reward by chance. The difficulty is a lack of evidence, not a lack of ability.

QuestionA random walker on the states 0 to 6 starts at 0, moves left or right with equal chance (staying at 0 when it tries to go left from there), and receives reward only at 6. What is the expected number of steps to reach 6? Show the reasoning.Show answer

The expected time from 00 to NN on this walk is N2N^2. Here N=6N = 6, so the expected time is 3636 steps. The reasoning is the recurrence Ek=1+12Ek−1+12Ek+1E_k = 1 + \tfrac{1}{2} E_{k-1} + \tfrac{1}{2} E_{k+1} with E6=0E_6 = 0 and E0=1+E1E_0 = 1 + E_1, whose solution is E0=N2E_0 = N^2.

QuestionWhy is potential-based shaping γΦ(s′)−Φ(s)\gamma \Phi(s') - \Phi(s) safe, when an arbitrary bonus for progress is not?Show answer

Along any trajectory the shaping terms telescope. Their discounted sum depends only on the start and end states, not on the path taken. Because the total extra reward is fixed by the endpoints (it shifts each state's value by −Φ(s)-\Phi(s) and nothing else), the ranking of actions in every state is unchanged, so the optimal policy is preserved. An arbitrary bonus can reward behaviour that the original task does not value, which lets the agent optimise the bonus instead.

QuestionYou are training a robot to pick up an object, with reward only when the object is in the target box. After a day of training the success rate is still zero. Which change would you try first, and what would you watch for?Show answer

Start from a curriculum, placing the object close to the box so the agent sees success early, and gradually move it further away. Watch for whether the success rate becomes nonzero. If the curriculum works, check that the agent still succeeds at the full distance, because a curriculum can leave gaps in what the agent has learned. Shaping with a potential based on the distance to the box is a second option, provided it is potential-based.

Sources