The key sits three rooms away and pays nothing until you touch it. A random explorer rarely gets there in 800 steps. An explorer paid for new places gets there in 90 to 200. Add a TV full of static, and a naive curious explorer stares at it forever.
Pick an explorer and switch the TV on and off. Watch where each one spends its steps. Then we build it piece by piece: the bonus for the unknown, counts for states that never repeat, curiosity, the TV trap and two ways out.
You need the MDP words (state, action, reward) and ε-greedy from the Q-learning lecture, plus a little Python. We build the rest from zero.
Lecture 20 of 25 in Introduction to Robot Learning, CMU 16-831 (Spring 2024), taught by Guanya Shi. Slides on the course lectures page. Every idea is re-taught here in our own words and devices. Before this: Lecture 19, Bandits and Preference Learning.
Illustrative toy: an 18 by 10 grid, 800 steps a run, no reward anywhere except the key. Each explorer moves to the neighbouring cell (or stays) with the largest bonus. The TV's next frame is random, so a raw prediction error there never falls. The RND bonus here shrinks with visits and spreads a little to nearby cells, as a trained predictor would.
Chapter 0
See why random actions almost never find a reward that needs a long, exact sequence
In the Atari game Montezuma's Revenge, the first point needs a long, exact sequence. Climb down a ladder, jump a gap, pass a skull, cross the room, take the key. Until the key, every reward is zero.
This is a sparse-reward task: the reward appears in only a few states. Q-learning, policy gradients and actor-critic all learn from reward. If they never see one, they learn nothing.
Q-learning explores with ε-greedy: take the best-known action, except with probability ε take a random one. Before the first reward, all Q-values are equal, so every action is random. The agent does a random walk: each step goes left or right by chance.
The reward waits at the right end. The warm walker moves at random. The teal walker moves toward the neighbour it has visited less. Set the length and press Race.
Illustrative toy. The random walker bounces off the left wall. On average it needs length squared steps to reach the end; the teal walker needs about length steps.
Work the number. For a corridor of 20 cells, the random walk needs 20 × 20 = 400 steps on average. The walker that prefers new cells needs about 20. Now replace the corridor with an exact sequence. Atari gives 18 actions. A random agent hits a 10-step sequence with probability (1/18) to the 10th = 2.8 × 10−13.
The symptom is a flat learning curve: the return stays at zero for millions of steps. To measure it, log the fraction of episodes with any nonzero reward, and the number of distinct states visited. If both stay low, the agent is not exploring. The fix is a directed bonus, the rest of this lesson.
Chapter 1
Pull slot-machine arms with a bonus for the ones you know least, and watch the bonus shrink as you learn
Three slot machines pay 1 or 0. Each has a hidden chance of paying. You get 100 pulls. This is a multi-armed bandit: a choice repeated many times, with no state that carries over.
The greedy rule pulls the arm with the best average so far. One lucky pull can lock it onto a poor arm forever. The fix is optimism in the face of uncertainty: if you are not sure an arm is bad, act as if it might be good. Try it, and either way you learn.
Pink bars are the estimated payout. The yellow whisker on top is the uncertainty bonus. Choose a rule, then pull. Watch which arm gets the pulls.
Illustrative toy. True payout chances 0.35, 0.50 and 0.65 (the dashed lines). UCB uses c = 1.
The classic rule is the upper confidence bound (UCB). For arm a, call its average payout so far Q-hat of a. Call the number of times you pulled it N of a, and the total pulls so far t. A knob c sets how much the bonus counts.
Pick the arm a that makes largest: Q-hat of a, plus c times the square root of log t over N of a.
For each arm, take its average Q-hat. Add a bonus that is large when N is small. Pull the arm with the largest total. The bonus is how wrong your average could still be.
With made-up numbers: t = 100 and c = 1, so log t = 4.61. Arm A: average 0.60 from 90 pulls, total 0.60 + √(4.61 / 90) = 0.83. Arm B: average 0.45 from 10 pulls, total 0.45 + √(4.61 / 10) = 1.13. UCB pulls B, the arm it knows less.
The square root has a reason. The error of an average of N samples falls like 1 over √N. So the bonus tracks how unsure the average is. Thompson sampling reaches the same goal another way: it draws a guess for each arm from its uncertainty and pulls the best guess.
The symptom is a greedy agent that commits early to a weak arm. To measure it, count how often it pulls the best arm over many runs. The fix is the bonus. One problem stays: UCB counts each arm, and a robot has too many states to count. The next chapters solve that.
Chapter 2
Count visits to each state, pay 1 over the square root of the count, and set how hard novelty pulls against a small real reward
Carry the UCB bonus from arms to states. Keep a tally of how often the agent visited each state. Pay a bonus that is large for a rare state and small for a common one. Then hand the sum to ordinary Q-learning.
Call the real reward for action a in state s r of s and a. Call the visit count of state s N of s. The knob β (beta) sets how much novelty counts against real reward.
r-plus of s and a equals r of s and a, plus beta over the square root of N of s.
Add 1 to the count N of the state you entered. Divide beta by the square root of N. Add that to the real reward r. Give the total r-plus to the learner.
With β = 1 and r = 0: the 1st visit pays 1 / √1 = 1.00. The 4th pays 1 / √4 = 0.50. The 100th pays 1 / √100 = 0.10. The pull of that state has faded ten times.
The device puts the bonus against a real reward. A small decoy pays 0.1 on every step, close to the start. The key pays 1, far away. The explorer steps to the neighbour with the largest r-plus.
Each yellow disc is a cell's bonus, 1 / √(N + 1), drawn before β. The small diamond is the decoy, the large one the key. Drag β, then press Run.
Illustrative toy: a 12 by 8 grid, 600 steps. The explorer may also stay where it is. Ties break at random. The +1 inside the root lets an unvisited cell pay β.
Try β = 0. The explorer wanders until it bumps into the decoy, then sits there. Raise β and it leaves the decoy, because new cells pay more than 0.1. Only once every nearby bonus drops below the decoy's pay does it settle. By then it has usually found the key.
The symptom is an agent that settles on the first small reward. To measure it, plot how many distinct states it has visited over time. A flat line early means β is too small. The fix is a larger β, but too large and the agent ignores the real reward for a long time.
Chapter 3
Replace the exact count with a density model, and read a count back out of how much one more look moves it
Make the state an Atari screen. A sprite moves one pixel, a digit ticks, a torch flickers. The exact same screen almost never comes back. Every count stays at 1, and the bonus never falls.
We want a count of states like this one. A density model gives that: a model that says how probable a state is, given everything seen so far. Call its probability for state s before one more look ρ (rho). Call its probability right after it trains on s once more ρ-prime.
N-hat of s equals rho times one minus rho-prime, over rho-prime minus rho.
Ask the model for rho. Train it on s once and ask again for rho-prime. If rho-prime barely moved, the model knew s well, so N-hat is large. If it jumped, s was rare, so N-hat is small. Put N-hat into Chapter 2's bonus.
With made-up numbers: a common state, ρ = 0.010 and ρ′ = 0.011. N-hat = 0.010 × 0.989 / 0.001 = 9.9, bonus 1 / √9.9 = 0.32. A rare state, ρ = 0.0001 and ρ′ = 0.0011: N-hat = 0.1, bonus 3.2.
Why this form? Use plain counting as the density model: n states seen, N of them equal to s. Then ρ = N / n and ρ′ = (N + 1) / (n + 1). Put both into the formula and it returns N exactly. So the pseudo-count is the count, whenever counting works.
Blue dots are 120 states seen so far, no two equal. The pink bars are a 10-bin density model. Move the probe, then add visits near it.
Illustrative toy. Each bin starts with half a count, so no probability is zero. For this model the pseudo-count equals the bin count plus that half.
Bellemare and colleagues (2016) used this idea with a pixel density model on Montezuma's Revenge. The count bonus took the agent through far more rooms than ε-greedy did.
The symptom is a bonus that never decays on image input. To measure it, log the exact count of each new screen: nearly all are 1. The fix is the pseudo-count. A cheaper fix is to hash each screen to a short code and count codes. A bad hash merges different rooms or splits one room.
Chapter 4
Train a model to predict the next state, pay the agent for its mistakes, and watch the pay dry up where the model has learned
A toddler drops a spoon, then a cup, then a sock. She is not counting. She is testing a guess about what happens next, and she goes where the guess fails. Pathak and colleagues (2017) turned that into a reward.
Keep a forward model: a small network that takes the state now and the action and predicts the next state. Train it on the agent's own experience. Where it predicts badly, pay the agent. Where it predicts well, pay almost nothing.
Call the state now s-t, the action a-t, and the next state s-t-plus-one. Call the forward model f, with weights θ (theta). Its guess is s-hat-t-plus-one.
r-i-t equals the squared length of f-theta of s-t and a-t, minus s-t-plus-one.
Before you act, let f-theta guess the next state. Act and see s-t-plus-one. Subtract the guess, square each entry and add them. Pay that as r-i-t, then train f-theta to shrink it.
With made-up numbers: the model guesses the next state (2.0, 1.0). The world gives (2.5, 0.0). r-i-t = 0.5² + 1.0² = 1.25. After training on this region, the guess is (2.4, 0.1) and the pay drops to 0.02.
Each pink bar is the forward model's error at one place, the curiosity pay. Send the agent to a region. Its bars fall as the model learns them.
Illustrative toy. Each visit cuts the error at that place by 35% and at its neighbours by 10%, as a model that generalizes a little would.
The total reward is r plus r-i, the same shape as Chapter 2. Pathak's agent made progress in Super Mario Bros with the game score switched off, paid by curiosity alone.
The symptom is curiosity that never fades in one place. To measure it, plot the forward model's error per region over training. A region whose error stays high while others fall is the problem. Chapter 5 shows the usual cause.
Chapter 5
Split prediction error into a part the model can learn and a part it never can, and see the agent stuck on the second
Put a TV in the world that shows static: a new random picture every frame. No model can predict the next frame. Its error never falls, so its curiosity pay never falls. The agent sits in front of the TV forever. This is the noisy-TV problem.
To see why, split the error in two. Call the forward model's guess s-hat. Call the true next state s-prime, a random quantity. Its average is E of s-prime.
The expected squared error between s-hat and s-prime equals the squared distance from s-hat to the mean of s-prime, plus the variance of s-prime.
The error has two parts. The first is how far s-hat is from the mean; training drives it to zero. The second is the variance of s-prime; it stays. For a TV, the second part is the whole pay.
With made-up numbers: one TV pixel is 0 or 1, each half the time. Mean 0.5, variance 0.25. A guess of 0.9 costs 0.4² + 0.25 = 0.41. The best guess, 0.5, still costs 0.25 on every frame, forever.
Pink is the part of the error the model can learn. Warm hatching is the variance, the floor. Press Train and watch which bars reach zero.
Illustrative toy. Each training step cuts the learnable part by 30% in every region. The TV's floor equals the variance you set.
Robots live with noise: camera grain, moving shadows, leaves, people walking by. A robot paid for raw prediction error will stare at the noisiest thing in view. Go back to the hero, pick Curiosity and switch the TV on.
The symptom is an agent that parks in one spot with high intrinsic reward and no progress. To measure it, log the share of steps spent near each object, and the forward error there over time. An error that plateaus above zero is variance. The fix is to predict something that has no random part: Chapters 6 and 7.
Chapter 6
Learn features by guessing the action, then measure curiosity in those features, where the static does not appear
The Intrinsic Curiosity Module (ICM) keeps the forward model but changes what it predicts. It predicts the next state's features, a short code that an encoder network computes from the state. The trick is how ICM trains that encoder.
It adds an inverse model: given the features of two states in a row, guess the action taken between them. To guess the action, the features must hold what the action changed: the agent's position, the object it pushed. The TV's static does not help guess the action, so nothing in this training keeps it.
Five input channels. The pink bars show how much the encoder keeps each one. The bar on the right is the curiosity pay, split by channel. Choose a space, then press Train.
Illustrative toy. The inverse model's pull keeps the three channels the action moves. The two TV channels get no pull and shrink with weight decay. Raw pixels keep every channel.
Call the encoder φ, the forward model f, and a scale η (eta) that sets the size of the pay.
r-i-t equals eta over two, times the squared length of f of phi of s-t and a-t, minus phi of s-t-plus-one.
Encode both states with phi. Let f guess the next features from the current features and the action. Square the miss, add it up and scale by eta over two. Static is not in phi, so it adds nothing.
With made-up numbers: η = 2. The guess is (0.4, 0.1), the true features (0.6, 0.1). r-i-t = 1 × (0.2² + 0²) = 0.04. The TV adds 0, because phi has no TV entry.
The symptom is noise the agent's action does move: a TV with a remote, where each button press gives a random channel. The action changes the screen, so the encoder keeps it, and the trap returns. To measure it, check which inputs the inverse model's features depend on. The fix needs a novelty signal with no random target at all.
Chapter 7
Freeze a random network, train a second one to copy it on visited states, and use the gap as novelty
Random Network Distillation (RND) asks a simpler question than curiosity: have I seen a state like this before? It needs no dynamics, no actions and no next state. It uses two networks.
| Network | Weights | Job |
|---|---|---|
| Target f | Random, then frozen forever | Maps a state to a fixed vector. Same state in, same vector out. |
| Predictor f-hat | Trained on every visited state | Learns to copy the target's output. |
On states the agent visits often, the predictor has copied the target well. On a new state it has never trained there, so it misses. The size of the miss is the novelty. Call the target's output for state s f of s, and the predictor's f-hat of s.
r-i of s equals the squared length of f-hat of s minus f of s.
Run s through both networks. Subtract, square each entry and add them. Pay that as r-i. Then take one training step to move f-hat toward f on s, so the next visit pays less.
With made-up numbers: the target gives (0.8, −0.3). On a new state the predictor gives (0.1, 0.2): r-i = 0.49 + 0.25 = 0.74. After many visits it gives (0.75, −0.28): r-i = 0.0029.
The dashed grey curve is the frozen target. The pink curve is the predictor. The yellow gap is the novelty pay. Tap the plot to visit a state, or press Wander.
Illustrative toy. The target is a fixed sum of sine waves. The predictor is a linear model on 24 bumps, trained by one gradient step per visit.
Burda and colleagues (2018) reported that RND was the first method to beat average human play on Montezuma's Revenge. It used no demonstrations and no access to the game's internal state.
The symptom is a bonus that is huge in some runs and tiny in others. Its size depends on the random target and on the input scale. To measure it, log the running mean and spread of r-i. The fix from the paper is to normalize the inputs and divide the pay by a running estimate of its spread.
Chapter 8
Put raw curiosity and RND on the same random state, and see which error has a floor
Take Chapter 5's split. Raw curiosity predicts the next state, which is random, so its error carries the variance term. RND predicts f of s, a fixed function of the state now. Same s in, same f of s out. Its variance term is zero.
| Raw curiosity | RND | |
|---|---|---|
| Predicts | the next state, from state and action | f of s, from the state now |
| Random part of the target | the variance of the next state | none |
| Error at the TV after training | stops at the variance | falls toward 0 |
| Measures | unpredictability | unfamiliarity |
The agent stands at the TV. Each visit, the next frame is random. Warm is the raw next-state error, pink is RND's error on the state now. Set the noise and press Visit.
Illustrative toy, the same set-up as the Code Lab below: an 8-number state, linear models, learning rate 0.1. The floor is 8 × σ².
Work the number. Each of the 8 next-frame numbers has variance σ² = 1. The best forward model still pays 8 × 1 = 8 on average, on every visit. RND's target for that state is one fixed vector, so after 40 visits its pay is near 0.
ICM and RND share one rule: keep randomness out of the novelty signal. ICM removes it from the features. RND removes it from the target.
RND removes the floor for random transitions: one state, a random outcome. A screen that shows new random pixels as the state itself is a different case. Every frame is a new input, so RND can keep paying until its predictor generalizes across noise. To measure it, log RND's pay per object over training. The fix is to keep such inputs out of the observation, or to estimate the noise and subtract it.
Chapter 9
Sort every bonus by the question it asks, and see that all of them plug into the same pipe
Every method in this lesson makes its own reward. One common way to sort them is by the question each bonus asks.
| Family | The question | Examples |
|---|---|---|
| Knowledge | How wrong is my model of the world here? | Curiosity, ICM |
| Data | How much of the state space have I covered? | Counts, pseudo-counts, RND, state-entropy bonuses |
| Competence | Which distinct things can I do on purpose? | Empowerment, skill discovery such as DIAYN |
The competence family is new here. A skill-discovery agent gets a random code z and must act so that a second network can tell z back from its behaviour. It learns a set of distinct skills with no task reward. Skill Discovery builds it in full.
Pick a method. Only the pink middle box changes. Drag β to see the total reward the learner gets.
Illustrative numbers for one step in an empty room: real reward 0, and each method's pay for a state it has seen a few times.
The pipe is always the same. The learner sees the real reward plus β times the bonus. With made-up numbers: real reward 0, RND pay 0.74, β = 0.5. The learner sees 0 + 0.5 × 0.74 = 0.37. Every bonus in the table shrinks with experience, so the real reward takes over once the agent has found it.
The symptom is detachment: the agent drains the bonus near a far frontier, wanders off, and never goes back. To measure it, plot the bonus left in each region against time since the last visit. One fix keeps an archive of frontier states and returns to them before it explores.
Chapter 10
Keep the terms, the numbers, a checklist and the core code in one place
Sparse reward stops random exploration. Every fix in this lesson pays the agent for the unknown, with a bonus that shrinks as the unknown becomes known. The one trap is randomness, which never becomes known. ICM filters it from the features; RND keeps it out of the target.
| Term | One line | Number to remember |
|---|---|---|
| Random walk | What ε-greedy does before any reward. | 20-cell corridor: about 400 steps |
| UCB | Pull the arm with the largest Q-hat + c √(ln t / N). | 0.45 from 10 pulls beats 0.60 from 90 |
| Count bonus | r + β / √N(s). | Visits 1, 4, 100: 1.00, 0.50, 0.10 |
| Pseudo-count | ρ(1 − ρ′) / (ρ′ − ρ). | 0.010 to 0.011: about 10 |
| Curiosity | Squared miss of the next-state prediction. | (2, 1) against (2.5, 0): 1.25 |
| Noisy TV | Error = learnable part + variance. | Coin pixel: floor 0.25 |
| ICM | Curiosity in features trained by an inverse model. | TV share of the pay: 0 |
| RND | Squared miss of a predictor against a frozen random target. | New 0.74, familiar 0.0029 |
pythonimport numpy as np class RND: def __init__(self, d_in, d_out=16, lr=0.05, seed=0): rng = np.random.default_rng(seed) self.T = rng.normal(size=(d_out, d_in)) # frozen random target self.W = np.zeros((d_out, d_in)) # trained predictor self.lr = lr def bonus(self, s): err = self.W @ s - self.T @ s # f_hat(s) - f(s) self.W -= self.lr * np.outer(err, s) # one step toward the target return float(err @ err) # squared miss = novelty # inside any RL loop: r_total = r_ext + beta * rnd.bonus(s_next)
This lesson follows Lecture 20, on exploration, of CMU 16-831 Introduction to Robot Learning, Spring 2024, taught by Guanya Shi. The slides are on the lectures page. The methods come from three papers. Bellemare et al., Unifying Count-Based Exploration and Intrinsic Motivation (2016); Pathak et al., Curiosity-driven Exploration by Self-supervised Prediction (2017); and Burda et al., Exploration by Random Network Distillation (2018).
Now press Present or Teach. Explain from memory: the random walk, the count bonus, the pseudo-count, the noisy-TV split and the two ways out. Then go back to the maze, switch the TV on, and say why Curiosity stops and RND does not.