Simple Algorithm
Artificial Intelligence · Deep Layer · 26 min

Exploration Strategies

ε-greedy, optimism, curiosity and count bonuses.

An agent that explores by dithering can need exponentially many steps to find a reward that a systematic explorer reaches in polynomially many. The theory of exploration in MDPs makes this precise with regret and sample-complexity bounds, and its two big ideas, optimism in the face of uncertainty and posterior sampling, return in deep RL as count bonuses, pseudo-counts and curiosity.

What you’ll learn

  • Define regret and PAC sample complexity for finite MDPs, and prove that undirected exploration can need exponentially many steps.
  • Explain R-MAX and UCRL2, state UCRL2's O~(DSAT)\tilde O(DS\sqrt{AT}) regret bound and the Ω(DSAT)\Omega(\sqrt{DSAT}) lower bound, and sketch the regret decomposition behind them.
  • Explain posterior sampling for RL, the lemma behind its Bayesian regret bound, and why it can explore more efficiently than worst-case optimism.
  • Derive pseudo-counts from a density model, and compare count bonuses with curiosity (ICM, RND), including the noisy-TV failure mode.

Measuring exploration, and why dithering fails

DerivationA chain that defeats dithering
  1. Ei=1+12Ei+1+12E1,EN=0E_i=1+\tfrac12E_{i+1}+\tfrac12E_1,\qquad E_N=0

    States 1,…,N1,\dots,N in a line with two actions: advance moves from ii to i+1i+1, reset returns to state 1, and only reaching state NN pays. Until it sees a reward, an ε\varepsilon-greedy or Boltzmann learner with tied estimates picks actions uniformly. Let EiE_i be the expected number of steps to reach NN from ii:

  2. E1=a11−b1=2N−2E_1=\frac{a_1}{1-b_1}=2^N-2

    Write Ei=ai+biE1E_i=a_i+b_iE_1. The recursion gives ai=1+12ai+1a_i=1+\tfrac12a_{i+1} and bi=12+12bi+1b_i=\tfrac12+\tfrac12b_{i+1} with aN=bN=0a_N=b_N=0, so ai=2(1−2−(N−i))a_i=2\big(1-2^{-(N-i)}\big) and bi=1−2−(N−i)b_i=1-2^{-(N-i)}. Solving at i=1i=1:

  3. E1undirected=2N−2versusO(N2) for directed explorationE_1^{\text{undirected}}=2^N-2\qquad\text{versus}\qquad\order{N^2}\ \text{for directed exploration}

    Exponential in the chain length: about 101510^{15} steps for N=50N=50. RL Foundations found the same effect on the bridge gridworld, one crossing per 780 random episodes. A directed explorer that tries untested actions on purpose needs only O(N2)\order{N^2} steps: at each new state it tries the untested action, and at worst one reset and a walk back later it has found advance.

Undirected strategies (ε-greedy, Boltzmann, noise added to actions) randomize locally and pay this exponential price whenever reward requires committed multi-step behavior. Directed strategies keep track of what is unknown and steer toward it. Kearns and Singh's E³ (2002) was the first algorithm with polynomial sample complexity in general MDPs; the two families that followed are optimism and posterior sampling.

Optimism: R-MAX and UCRL2

R-MAX (Brafman & Tennenholtz, 2002) keeps a count N(s,a)N(s,a) and calls a pair known once N(s,a)≥mN(s,a)\ge m. In the model it plans with, every unknown pair leads to a fictitious absorbing state paying the maximum reward Rmax⁡R_{\max} forever, and known pairs use their empirical estimates. Its greedy policy either exploits, being near-optimal in the true MDP, or reaches an unknown pair with substantial probability within the horizon: the implicit explore-or-exploit lemma, proved with the simulation lemma of Bellman Equations & Contraction on the known part. Each pair becomes known after mm visits, so exploration ends after polynomially many steps. UCRL2 refines the idea with confidence sets and episodes.

text
function UCRL2(δ):
    t ← 1;  counts N(s,a) ← 0;  empirical estimates P̂, r̂
    for episodes k = 1, 2, ...:
        t_k ← t;  v(s,a) ← 0                          # visits within this episode
        M_k ← all MDPs (P̃, r̃) such that, for every (s,a),
                |r̃(s,a) − r̂(s,a)|          ≲ √( log(S A t_k / δ) / max(1, N(s,a)) )
                ‖P̃(·|s,a) − P̂(·|s,a)‖₁    ≲ √( S log(A t_k / δ) / max(1, N(s,a)) )
        π_k ← optimal policy of the most optimistic MDP in M_k   # extended value iteration
        while v(S_t, π_k(S_t)) < max(1, N(S_t, π_k(S_t))):        # until a count doubles
            act A_t = π_k(S_t); observe r_t, S_{t+1}; v(S_t, A_t) += 1; t += 1
        N ← N + v;  update P̂, r̂

Constants are omitted (≲\lesssim). Extended value iteration solves the optimistic problem jointly over policies and over MDPs in the confidence set: in each backup, the inner maximization moves as much of P^(⋅∣s,a)\hat P(\cdot\mid s,a)'s mass as the L1L_1 ball allows onto the currently best next state, which needs only a sort. Ending an episode when some count doubles keeps the number of episodes at most SAlog⁡28TSASA\log_2\frac{8T}{SA}.

DerivationProof sketch of the upper bound
  1. Δk=∑t∈episode k(ρ∗−rt)≤∑t∈episode k(ρ~k−rt)\Delta_k=\sum_{t\in\text{episode }k}\big(\rho^*-r_t\big)\le\sum_{t\in\text{episode }k}\big(\tilde\rho_k-r_t\big)

    Good event. A union bound over pairs and times, with Hoeffding's inequality for rewards and Weissman's L1L_1 inequality for transition vectors, puts the true MDP inside every Mk\mathcal M_k with probability at least 1−δ1-\delta. Optimism. The optimistic average reward then satisfies ρ~k≥ρ∗\tilde\rho_k\ge\rho^*, so each episode's regret is bounded by how far the agent's rewards fall short of the optimistic model's promise:

  2. ρ~k−rt ≈ (r~k−r)(St,At)⏟≲ log⁡/N+(P~k−P)(⋅∣St,At)⊤h~k⏟≤ ∥P~k−P∥1D/2 ≲ DSlog⁡/N+martingale+telescoping\tilde\rho_k-r_t\ \approx\ \underbrace{(\tilde r_k-r)(S_t,A_t)}_{\lesssim\,\sqrt{\log/N}}+\underbrace{\big(\tilde P_k-P\big)(\cdot\mid S_t,A_t)^{\top}\tilde h_k}_{\le\,\norm{\tilde P_k-P}_1D/2\ \lesssim\ D\sqrt{S\log/N}}+\text{martingale}+\text{telescoping}

    Decomposition. The optimistic bias vector h~k\tilde h_k from extended value iteration has span at most DD, because the extended MDP contains the true one, which reaches any state in at most DD expected steps. Its Bellman equation splits each step's shortfall into a reward-confidence term, a transition term, martingale differences, and a telescoping sum worth at most DD per episode:

  3. ∑k∑s,avk(s,a)max⁡{1,Nk(s,a)}≤(2+1)SAT\sum_k\sum_{s,a}\frac{v_k(s,a)}{\sqrt{\max\{1,N_k(s,a)\}}}\le\big(\sqrt2+1\big)\sqrt{SAT}

    Summing. Azuma's inequality bounds the martingale part by O(DTlog⁡(1/δ))\order{D\sqrt{T\log(1/\delta)}}. For the confidence widths, the doubling rule keeps counts from more than doubling within an episode, and a pigeonhole argument gives

  4. Multiplying the per-visit width DSlog⁡(T/δ)D\sqrt{S\log(T/\delta)} by this sum gives O(DSATlog⁡(T/δ))\order{DS\sqrt{AT\log(T/\delta)}}, and the telescoping terms add DD per episode, O(DSAlog⁡T)\order{DSA\log T} in total. The lower bound combines many copies of a two-state gadget in which one action is slightly better at reaching the rewarding state, with a KL-divergence argument like the bandit lower bounds of Multi-Armed Bandits & Regret.

The gap between DSATDS\sqrt{AT} and DSAT\sqrt{DSAT}, a factor DS\sqrt{DS}, took years to narrow. In the episodic setting, UCBVI (Azar, Osband & Munos, 2017) adds Bernstein-style bonuses to value iteration on the empirical model and matches the lower bound Ω(HSAT)\Omega(\sqrt{HSAT}) up to logarithmic factors once TT is large, and optimistic Q-learning reaches T\sqrt T regret without a model (Jin et al., 2018).

Check your understanding

In the regret analysis of UCRL2, what does optimism, ρ~k≥ρ∗\tilde\rho_k\ge\rho^*, buy?

  1. It makes the agent's policy optimal from the first episode
  2. It removes the need for confidence sets
  3. It makes the regret independent of the diameter
  4. It replaces the unknown ρ∗\rho^* by a computable quantity, so regret is bounded by the gap between the optimistic model's predictions and what actually happens, a gap that shrinks wherever the agent gathers data
Show the answer

D. It replaces the unknown ρ∗\rho^* by a computable quantity, so regret is bounded by the gap between the optimistic model's predictions and what actually happens, a gap that shrinks wherever the agent gathers data

Regret compares the agent with an unknown optimum. Optimism bounds that optimum by ρ~k\tilde\rho_k, and the excess ρ~k−rt\tilde\rho_k-r_t is controlled by confidence widths that shrink like 1/N1/\sqrt N on the pairs the agent visits. Either the optimistic model is right and the agent earns nearly ρ∗\rho^*, or it is wrong where the agent goes, and the agent learns there.

Posterior sampling

Posterior sampling for RL (PSRL; Strens, 2000; Osband, Russo & Van Roy, 2013) extends Thompson sampling to MDPs. Keep a posterior over MDPs, for example Dirichlet posteriors on each transition vector and conjugate posteriors on rewards. At the start of episode kk, sample an MDP MkM_k from the posterior, compute its optimal policy, follow that policy for the whole episode, and update. Committing to one sample per episode is what makes the exploration deep: a sample in which the far side of the bridge pays well sends the agent all the way across, instead of one step east and back.

DerivationThe posterior sampling lemma, and why optimism is not needed
  1. E[g(M∗) ∣ Hk]=E[g(Mk) ∣ Hk]\E\big[g(M^*)\,\big|\,\mathcal H_k\big]=\E\big[g(M_k)\,\big|\,\mathcal H_k\big]

    Let Hk\mathcal H_k be the history before episode kk. By construction MkM_k and M∗M^* have the same distribution given Hk\mathcal H_k, so for any function gg of an MDP,

  2. E[VM∗∗(s1)−VM∗πk(s1)]=E[VM∗∗(s1)−VMk∗(s1)]⏟= 0+E[VMkπk(s1)−VM∗πk(s1)]\E\big[V^*_{M^*}(s_1)-V^{\pi_k}_{M^*}(s_1)\big]=\underbrace{\E\big[V^*_{M^*}(s_1)-V^*_{M_k}(s_1)\big]}_{=\,0}+\E\big[V^{\pi_k}_{M_k}(s_1)-V^{\pi_k}_{M^*}(s_1)\big]

    Take g(M)=VM∗(s1)g(M)=V^*_M(s_1), the optimal value of MM. Episode kk's regret is VM∗∗(s1)−VM∗πk(s1)V^*_{M^*}(s_1)-V^{\pi_k}_{M^*}(s_1); since πk\pi_k is optimal for MkM_k, insert VMkπk=VMk∗V^{\pi_k}_{M_k}=V^*_{M_k}:

  3. The first term vanishes in expectation, exactly where UCRL2 needed optimism. The second compares two MDPs on the same policy, and a simulation-lemma argument bounds it by how concentrated the posterior is on the pairs that policy visits, giving the same T\sqrt T structure as before.

The guarantee is Bayesian, averaged over the prior; worst-case analyses of posterior sampling in MDPs are more delicate. In Osband et al.'s experiments PSRL outperformed UCRL2 by a wide margin. Optimism must hold uniformly over all pairs and all plausible models, with union bounds and worst-case L1L_1 balls, so it over-explores, while a posterior sample is optimistic only where the data actually leave room. Scalable versions randomize value functions instead of models: randomized least-squares value iteration (Osband, Van Roy & Wen, 2016), and bootstrapped DQN (Osband et al., 2016), which trains an ensemble of value heads on bootstrapped data and follows one head per episode.

Bonuses at scale: counts, pseudo-counts and curiosity

Tabular counts are useless when no state is ever seen twice, as with images. Exploration bonuses generalize the count: add r+(s,a)=β/N^(s,a)r^+(s,a)=\beta/\sqrt{\hat N(s,a)} to the reward, the form analyzed in MBIE-EB (Strehl & Littman, 2008), with a generalized count N^\hat N.

DerivationPseudo-counts from a density model (Bellemare et al., 2016)
  1. ρn(x)=N^(x)n^,ρn′(x)=N^(x)+1n^+1\rho_n(x)=\frac{\hat N(x)}{\hat n},\qquad\rho'_n(x)=\frac{\hat N(x)+1}{\hat n+1}

    Train a density model on the states seen so far. Let ρn(x)\rho_n(x) be its probability of state xx, and ρn′(x)\rho'_n(x) the recoding probability: its probability of xx after one more update on xx. Ask for a pseudo-count N^(x)\hat N(x) and pseudo-total n^\hat n that make the model behave like an empirical frequency before and after one more observation of xx:

  2. N^(x)=ρn(x) (1−ρn′(x))ρn′(x)−ρn(x)\hat N(x)=\frac{\rho_n(x)\,\big(1-\rho'_n(x)\big)}{\rho'_n(x)-\rho_n(x)}

    Two equations, two unknowns. The second gives n^ (ρn′−ρn)=1−ρn′\hat n\,(\rho'_n-\rho_n)=1-\rho'_n; substitute into the first:

  3. N^(x)=1−ρn′(x)ePGn(x)−1≈1ePGn(x)−1for small ρn′(x)\hat N(x)=\frac{1-\rho'_n(x)}{e^{\mathrm{PG}_n(x)}-1}\approx\frac{1}{e^{\mathrm{PG}_n(x)}-1}\quad\text{for small }\rho'_n(x)

    In terms of the prediction gain PGn(x)=log⁡ρn′(x)−log⁡ρn(x)\mathrm{PG}_n(x)=\log\rho'_n(x)-\log\rho_n(x), a state the model learns a lot from has a small pseudo-count and earns a large bonus. Bellemare et al. used bonuses β(N^+0.01)−1/2\beta(\hat N+0.01)^{-1/2} from a pixel density model and made large gains on Montezuma's Revenge, an Atari game with famously sparse rewards.

Predict before you look

A density model assigns a state probability ρ=0.0100\rho=0.0100, and ρ′=0.0102\rho'=0.0102 after one more update on it. What is the pseudo-count N^\hat N?

Show the answer

49.5

N^=0.01×(1−0.0102)0.0102−0.01=0.0098980.0002≈49.5\hat N=\frac{0.01\times(1-0.0102)}{0.0102-0.01}=\frac{0.009898}{0.0002}\approx49.5. The model behaves as if it had seen this state about 50 times, so the bonus is about β/49.5≈0.14β\beta/\sqrt{49.5}\approx0.14\beta.

Curiosity rewards prediction error instead of rarity. The intrinsic curiosity module (ICM; Pathak et al., 2017) learns features ϕ\phi by inverse dynamics, predicting ata_t from ϕ(st)\phi(s_t) and ϕ(st+1)\phi(s_{t+1}), which keeps what the agent can influence, and rewards the error of a forward model in that space, η2∥ϕ^(st+1)−ϕ(st+1)∥2\frac\eta2\norm{\hat\phi(s_{t+1})-\phi(s_{t+1})}^2. Random network distillation (RND; Burda et al., 2019) trains a predictor to match the output f(s)f(s) of a fixed, randomly initialized network and rewards the squared error. The target is a deterministic function of the state, so the error shrinks wherever the agent has been, much like an inverse count; the RND paper reported better-than-average-human scores on Montezuma's Revenge without demonstrations.

Two further failures concern memory rather than measurement. Detachment: an agent following intrinsic reward can use up the bonus near a promising frontier, wander elsewhere and forget how to get back. Derailment: exploratory noise keeps it from reaching the frontier again. Go-Explore (Ecoffet et al., 2021) addresses both: it archives visited states, first returns to a promising one (by resetting a simulator or with a goal-conditioned policy), and only then explores.

Key takeaways

  • Undirected exploration can take exponentially long (2N−22^N-2 steps on an NN-state reset chain); directed strategies track what is unknown and steer toward it.
  • Optimism (R-MAX's fictitious maximum-reward state, UCRL2's confidence sets) gives polynomial sample complexity and regret O~(DSAT)\tilde O(DS\sqrt{AT}), against a lower bound Ω(DSAT)\Omega(\sqrt{DSAT}).
  • Posterior sampling replaces optimism by the fact that the true and the sampled MDP are identically distributed given the history; PSRL has Bayesian regret O~(τSAT)\tilde O(\tau S\sqrt{AT}) and often explores more efficiently in practice.
  • At scale, pseudo-counts N^=ρ(1−ρ′)/(ρ′−ρ)\hat N=\rho(1-\rho')/(\rho'-\rho), RND and inverse-dynamics curiosity provide bonuses; the noisy TV shows why bonuses must target reducible, epistemic uncertainty.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

On the NN-state reset chain, a uniformly random agent needs 2N−22^N-2 expected steps to reach the reward. What property of ε\varepsilon-greedy exploration is responsible?

  1. It explores too much: with ε\varepsilon small enough it would succeed quickly
  2. Its random actions are independent from step to step, so reaching the reward requires a long run of lucky choices, each undone by a single reset
  3. It uses the wrong learning rate
  4. It cannot represent the optimal policy
Show the answer

B. Its random actions are independent from step to step, so reaching the reward requires a long run of lucky choices, each undone by a single reset

Before any reward is seen, ε\varepsilon-greedy with tied estimates acts uniformly at random, and the reward needs N−1N-1 consecutive advance actions. Directed explorers remember which actions are untested and try them deliberately, reaching the end in O(N2)\order{N^2} steps.

Question 2

UCRL2's regret is O~(DSAT)\tilde O(DS\sqrt{AT}) and the lower bound is Ω(DSAT)\Omega(\sqrt{DSAT}). By what factor do they differ, up to logarithms?

  1. DS\sqrt{DS}
  2. DD
  3. A\sqrt A
  4. They match
Show the answer

A. DS\sqrt{DS}

DSAT/DSAT=DSDS\sqrt{AT}/\sqrt{DSAT}=\sqrt{DS}. The extra S\sqrt S comes from L1L_1 confidence sets over whole transition vectors, the extra D\sqrt D from bounding the bias span by DD in every step. Bernstein-type bonuses and variance arguments remove such factors in the episodic setting.

Question 3

Why does PSRL follow one sampled MDP for a whole episode instead of resampling at every step?

  1. Resampling every step would be computationally cheaper but statistically biased
  2. The posterior does not change within an episode
  3. A single sample yields a coherent multi-step plan, so the agent commits to reaching states its sample deems valuable; per-step resampling dithers between plans
  4. The posterior sampling lemma only holds for whole episodes
Show the answer

C. A single sample yields a coherent multi-step plan, so the agent commits to reaching states its sample deems valuable; per-step resampling dithers between plans

Deep exploration needs consistency: if each step followed a fresh sample, the agent would head east under one sample and west under the next, reproducing undirected dithering. Committing to one plausible world for an episode carries the agent to the places that world promises.

Question 4

An environment contains a screen showing random images that the agent can watch at no cost. Which intrinsic reward is most likely to trap the agent there?

  1. A count-based bonus on the agent's position in the maze
  2. Random network distillation's error on the current observation
  3. The error of a forward model predicting the next observation from the current one
  4. An extrinsic reward for reaching the exit
Show the answer

C. The error of a forward model predicting the next observation from the current one

The next frame of a random screen is unpredictable however much data the forward model sees: aleatoric uncertainty, so its error, and the reward, never decay. Position counts ignore the screen, RND's deterministic target removes stochastic transitions as a reward source, and extrinsic reward does not reward watching at all.

Further reading

  • T. Jaksch, R. Ortner & P. Auer, "Near-optimal regret bounds for reinforcement learning," Journal of Machine Learning Research 11 (2010).
  • I. Osband, D. Russo & B. Van Roy, "(More) efficient reinforcement learning via posterior sampling," NeurIPS (2013).
  • M. G. Bellemare, S. Srinivasan, G. Ostrovski, T. Schaul, D. Saxton & R. Munos, "Unifying count-based exploration and intrinsic motivation," NeurIPS (2016).
  • D. Pathak, P. Agrawal, A. A. Efros & T. Darrell, "Curiosity-driven exploration by self-supervised prediction," ICML (2017).
  • T. Lattimore & C. Szepesvári, Bandit Algorithms, chapter on Markov decision processes (Cambridge University Press, 2020).