14.4 Exploration

Returning to a favorite restaurant gives a good meal; a new one may give a better meal or a worse one, and staying with the favorite forever means never finding out. The same question arises whenever we choose repeatedly among options whose value we learn only by choosing them: how often should we try something new, and when may we stop?

William Thompson posed the problem in 1933 for medicine: which of two treatments should the next patient receive, given how the earlier patients fared (Thompson, 1933)? Herbert Robbins restated it in 1952 as a problem of sampling repeatedly from two populations, choosing each time which one to sample (Robbins, 1952). It soon took the name two-armed bandit, after the slot machine, the one-armed bandit; with more than two options it is the multi-armed bandit. In 1985 Tze Leung Lai and Robbins showed that the cost of learning must grow with the number of trials, but that it can be kept as small as the logarithm of that number (Lai and Robbins, 1985).

Q-learning in Section 14.3 faced the same question and answered it crudely. It learned only the entries of its table that it tried, and to keep trying commands that its table rated poorly, it acted at random with probability ϵ\epsilon. The rule has a visible cost: forced to act at random one step in twenty, the optimal policy on the slippery lake succeeds about 54% of the time instead of 73%. Is that the right amount of trying? Could a different rule learn as much while losing less?

The question matters again for language models trained with a verifier. The model samples several answers to a prompt, a checker scores them, and the model moves toward the answers that scored better. If every answer scores the same, there is nothing to move toward, and the model's own randomness is the only way it can find a better answer.

We first study the trade-off in a problem with a single state, the bandit, where its cost can be measured exactly. We then return to MDPs, where trying an untried pair can require a long sequence of actions, and end with the verifier task of Section 14.1.

import gymnasium as gym

14.4.1 The Multi-Armed Bandit#

A bandit is an MDP with a single state. Each round the agent pulls one of several arms and receives a random reward. Nothing it does changes what comes next, and no reward is delayed, so only one question remains: which arm is best? The agent can answer it only by pulling arms, and every pull of a worse arm is a pull not spent on the best one.

14.4.1.1 Arms and Regret#

We use ten arms with Bernoulli rewards: arm aa returns 11 with probability μa\mu_a and 00 otherwise. The best arm, the third, has μ∗=max⁡aμa=0.90\mu^* = \max_a \mu_a = 0.90; the second best has 0.600.60. The agent does not know the μa\mu_a. After n(a)n(a) pulls of arm aa it knows the empirical mean μ^(a)\hat\mu(a), the fraction of those pulls that returned 11. A bandit rule maps these tallies to the next arm. The function bandit_run plays one rule for num_pulls rounds, drawing all randomness from the generator rng; it keeps the tallies wins and count for every arm and passes them to the rule:

arms = np.array([0.50, 0.42, 0.90, 0.25, 0.55, 0.38, 0.60, 0.32, 0.50, 0.45])

def bandit_run(arms, rule, num_pulls, rng):
    """One state, one step: pull an arm, log the shortfall, update tallies.
    Returns the cumulative regret after each pull."""
    wins, count = np.zeros(len(arms)), np.zeros(len(arms))
    gap = np.empty(num_pulls)
    for t in range(num_pulls):
        a = rule(wins, count, t, rng)
        count[a] += 1
        wins[a] += float(rng.random() < arms[a])
        gap[t] = arms.max() - arms[a]
    return np.cumsum(gap)

The function records the shortfall of each pull rather than its reward. A success rate says how good a policy is; for a learner we want to know how much the learning cost. The pull of arm aua_u in round uu falls short of the best arm by μ∗−μau\mu^* - \mu_{a_u} in expectation, and the regret after tt pulls adds up these gaps:

Regret(t)=∑u=1t(μ∗−μau)=∑aΔa nt(a),Δa=μ∗−μa.(14.4.1)\textrm{Regret}(t) = \sum_{u=1}^{t} \big(\mu^* - \mu_{a_u}\big) = \sum_a \Delta_a\, n_t(a), \qquad \Delta_a = \mu^* - \mu_a. \tag{14.4.1}

The second form groups the pulls by arm: each arm's gap Δa\Delta_a times the number nt(a)n_t(a) of its pulls. Regret is what the agent loses against one that knew the best arm from the start. A rule that pulls one arm with mean 0.500.50 for good loses 0.400.40 per pull, 800800 after 20002000 pulls, and its regret grows linearly in tt. If a rule's regret grows more slowly than tt, the rule pulls worse arms ever more rarely, and its average regret per pull tends to zero.

14.4.1.2 Greedy and Epsilon-Greedy Rules#

The rule of Section 14.3 works here unchanged. d2l.epsilon_greedy(q, epsilon, rng) takes a row of value estimates, here the ten empirical means. With probability ϵ\epsilon it picks an arm uniformly at random; otherwise it picks the arm with the largest estimate, breaking ties at random. An arm that was never pulled has empirical mean 00. With ϵ=0\epsilon = 0 the rule is greedy. A fixed ϵ\epsilon keeps exploring at the same rate forever, while an annealed rule lowers ϵ\epsilon over time. We use ϵt=min⁡(1,20/t)\epsilon_t = \min(1, 20/t) in round tt, which pulls at random for the first twenty rounds and then ever more rarely. The schedule depends only on the round, not on anything the agent has learned.

Each of these rules turns a row of value estimates into a distribution over actions, as Figure 14.4.1 shows for a row of four action values, one per move of the lake. Greedy puts all probability on the largest estimate, and ϵ\epsilon-greedy spreads a fraction ϵ\epsilon of it uniformly over all actions. A third rule, Boltzmann exploration, makes the probability of each action proportional to exp⁡(Q^(s,a)/β)\exp(\hat Q(s, a)/\beta). This is the softmax of Section 3.1 with temperature β\beta: a large β\beta approaches the uniform distribution, a small one the greedy rule. Unlike ϵ\epsilon-greedy, it tries an action that is nearly the best more often than one that is clearly bad.

Figure 14.4.1:

Three exploration rules applied to one row of action values, Q^(s,⋅)=(0.20,0.90,0.55,0.10)\hat Q(s, \cdot) = (0.20, 0.90, 0.55, 0.10) for the four moves of the lake. Greedy puts all probability on the largest value. ϵ\epsilon-greedy with ϵ=0.3\epsilon = 0.3 gives each action 0.0750.075 and the rest to the largest value. The softmax with temperature 0.30.3 ranks the actions by their values.

The function eps_rule turns a schedule t↦ϵtt \mapsto \epsilon_t into a bandit rule that applies d2l.epsilon_greedy to the empirical means. The function regret_runs plays a rule in twenty independent runs and returns their regret curves; each run seeds its own generator from the stream number and the run index, so that different rules get independent runs. The function report prints the median and the quartiles of the final regret over the runs. We report medians because a few runs can dominate a mean:

def eps_rule(schedule):
    """Epsilon-greedy on the empirical means, with epsilon = schedule(t)."""
    return lambda wins, count, t, rng: d2l.epsilon_greedy(
        wins / np.maximum(count, 1), schedule(t), rng)

def regret_runs(arms, rule, stream, num_runs=20, num_pulls=2000):
    """Regret curves of `num_runs` independent runs, shape [run, pull]."""
    return np.array([bandit_run(arms, rule, num_pulls,
                                np.random.default_rng([2026, stream, s]))
                     for s in range(num_runs)])

def report(name, r):
    q25, q50, q75 = np.percentile(r[:, -1], [25, 50, 75])
    print(f'{name:>16}: regret after {r.shape[1]} pulls: median {q50:3.0f}'
          f' (quartiles {q25:.0f} to {q75:.0f})')

rules = {'greedy': eps_rule(lambda t: 0.0),
         'epsilon = 0.1': eps_rule(lambda t: 0.1),
         'epsilon = 20/t': eps_rule(lambda t: min(1.0, 20 / (t + 1)))}
regret = {name: regret_runs(arms, rule, i)
          for i, (name, rule) in enumerate(rules.items())}
for name, r in regret.items():
    report(name, r)
          greedy: regret after 2000 pulls: median 800 (quartiles 700 to 980)
   epsilon = 0.1: regret after 2000 pulls: median 106 (quartiles 92 to 132)
  epsilon = 20/t: regret after 2000 pulls: median  46 (quartiles 44 to 53)

The greedy rule does worst, and the reason can be worked out exactly. All empirical means start at 00, so the first pull goes to a random arm. If it returns 00, that arm's mean stays 00 and all ten arms are still tied. If it returns 11, its mean becomes positive and stays positive forever: one success keeps the fraction of wins above zero, while every other arm still reads 00. Greedy therefore pulls random arms until one returns a reward and then never pulls anything else. In each of those random pulls, arm aa is chosen with probability 1/101/10 and returns 11 with probability μa\mu_a, so the arm it settles on is aa with probability μa/∑bμb\mu_a / \sum_b \mu_b. The best arm wins this lottery with probability 0.90/4.870.90/4.87, in about one run in five. A run that settles on arm aa loses Δa\Delta_a on almost every pull, about 2000 Δa2000\,\Delta_a in all, so the expected regret is

E[Regret(2000)]≈2000∑aμa∑bμb Δa≈707.\mathbb{E}\big[\textrm{Regret}(2000)\big] \approx 2000 \sum_a \frac{\mu_a}{\sum_b \mu_b}\, \Delta_a \approx 707.

A single run loses one of the values 2000 Δa2000\,\Delta_a, from 00 (about one run in five) to 13001300; the median, around 800800, belongs to the runs that settle on an arm with mean 0.500.50.

The fixed rule with ϵ=0.1\epsilon = 0.1 finds the best arm in almost every run but never stops exploring. A random pull falls short of the best arm by the average gap, 0.90−0.487=0.410.90 - 0.487 = 0.41, where 0.4870.487 is the mean of the ten μa\mu_a. Exploration alone therefore costs 0.1×0.41≈0.040.1 \times 0.41 \approx 0.04 per pull, about 8383 of its median regret of 106106, and this cost does not shrink: the regret of a fixed ϵ\epsilon grows linearly in tt.

The annealed rule makes about 112112 random pulls in 20002000 rounds: twenty at the start and about 20ln⁡(2000/20)≈9220 \ln(2000/20) \approx 92 afterwards. At 0.410.41 each, they cost 4646, matching its median regret of 4646. Nearly all of its regret is spent on exploration, and the greedy pulls in between go to the best arm. Since the number of random pulls up to round tt grows like 20ln⁡t20 \ln t, so does the regret: an ϵt\epsilon_t proportional to 1/t1/t gives logarithmic regret. The constant matters. If it is too small, the rule stops exploring before it has told the best arm from the second best. The analysis of Auer et al. (2002) guarantees logarithmic regret on every bandit with mm arms whose smallest gap is Δmin⁡\Delta_{\min}, with ϵt=min⁡(1,c m/(Δmin⁡2t))\epsilon_t = \min(1, c\,m/(\Delta_{\min}^2 t)) and any c>5c > 5. For these arms, with Δmin⁡=0.30\Delta_{\min} = 0.30, that is nearly thirty times our 20/t20/t. Our constant was chosen for these arms, and the guarantee does not cover it.

14.4.2 Optimism and Posterior Sampling#

The ϵ\epsilon-greedy rules explore blindly. A random pull is as likely to go to the worst arm as to the second best, and an arm pulled a hundred times is as likely to be tried again as one pulled twice. A better rule directs exploration to the arms whose means are still uncertain and might be the best, because only those pulls can change the decision.

14.4.2.1 Upper Confidence Bounds#

One way to do this is optimism: treat each arm as if it were as good as the data still allow. An untried arm could be anything, so it gets tried. An arm pulled often has a precise estimate and is pulled again only if that estimate is high. The upper confidence bound (UCB) rule adds to each empirical mean a bonus that measures its uncertainty, and pulls the arm with the largest sum (Auer et al., 2002):

at=argmaxa[μ^(a)+κlog⁡t / n(a)].(14.4.2)a_t = \underset{a}{\mathrm{argmax}} \Big[ \hat{\mu}(a) + \kappa \sqrt{\log t \,/\, n(a)} \Big]. \tag{14.4.2}

The bonus shrinks like 1/n(a)1/\sqrt{n(a)}, the rate at which the error of an average of n(a)n(a) rewards shrinks, and it grows slowly with tt while arm aa is not pulled, so no arm is given up for good. An arm is pulled either because its mean is high or because its mean is uncertain. Every arm is pulled once before the rule starts, since the bonus is undefined at n(a)=0n(a) = 0. Figure 14.4.2 shows the bonus and the indices of one run.

Figure 14.4.2:

Optimism under uncertainty, with κ=0.5\kappa = 0.5. (a) The bonus κlog⁡t/n(a)\kappa\sqrt{\log t/n(a)} shrinks with an arm's count n(a)n(a) and grows slowly with time tt. (b) One run of the UCB rule on the ten arms after 2,000 pulls: each bar is an arm's estimate μ^(a)\hat\mu(a) with its bonus on top, labeled by the arm's pull count, and the black marks are the true means. Nearly all pulls went to the best arm, whose index is the largest; the rarely tried arms keep indices close to it, so each will be tried again as log⁡t\log t grows.

The size of the bonus comes from Hoeffding's inequality (Hoeffding, 1963): the mean μ^n\hat\mu_n of nn independent rewards in [0,1][0, 1] exceeds the true mean μ\mu by more than hh with probability at most e−2nh2e^{-2 n h^2}, and likewise falls below it. With h=κlog⁡t/nh = \kappa \sqrt{\log t / n}, the true mean lies above the index with probability at most t−2κ2t^{-2\kappa^2}, for a fixed count nn. A larger κ\kappa makes such failures rarer and spends more pulls on bad arms. With κ=2\kappa = \sqrt{2} the rule is UCB1 of Auer et al. (2002), whose regret grows logarithmically in tt on every bandit with rewards in [0,1][0, 1]; the same holds for every κ>1\kappa > 1 (Bubeck and Cesa-Bianchi, 2012), and exercise 2 derives the bound.

14.4.2.2 Thompson Sampling#

Thompson's rule of 1933 replaces the bonus by a belief. It keeps, for each arm, a posterior distribution over the arm's mean, draws one sample from each posterior, and pulls the arm with the largest draw. For rewards in {0,1}\{0, 1\} and a uniform prior on [0,1][0, 1], the posterior after ww wins in nn pulls is the Beta distribution Beta(1+w,1+n−w)\mathrm{Beta}(1 + w, 1 + n - w). An arm with 33 wins in 44 pulls has the posterior Beta(4,2)\mathrm{Beta}(4, 2), with mean 2/32/3 but wide. An untried arm has the uniform Beta(1,1)\mathrm{Beta}(1, 1), and its draw beats a draw from Beta(4,2)\mathrm{Beta}(4, 2) with probability 1/31/3. Figure 14.4.3 shows one such round, in which the untried arm even beats an arm that has won 1818 of 2020 pulls. As pulls accumulate, the posteriors narrow, and each arm is pulled with the posterior probability that it is the best one. Thompson sampling also has logarithmic regret (Agrawal and Goyal, 2012), and it has no constant to choose.

Figure 14.4.3:

One round of Thompson sampling with three arms: an untried arm, the arm with 33 wins in 44 pulls, and an arm with 1818 wins in 2020. The curves are the Beta posteriors of the arms' means, narrower the more often an arm was pulled. The rule draws one value from each posterior (triangles) and pulls the arm with the largest draw, which in this round is the untried arm.

14.4.2.3 UCB and Thompson Sampling Compared#

The function ucb(kappa) returns the UCB rule with coefficient κ\kappa, and thompson draws from the Beta posteriors. We run UCB with κ=0.5\kappa = 0.5, a value chosen for these arms that we examine below, and Thompson sampling, and then plot all five rules. d2l.plot_curves draws the median over the runs with the band between the quartiles, here at 100 pulls spaced evenly on logarithmic axes:

def ucb(kappa):
    """The UCB rule with coefficient kappa: each arm once, then the largest
    index mean + kappa * sqrt(log t / n)."""
    def rule(wins, count, t, rng):
        if (count == 0).any():          # play each arm once before pricing any
            return int(np.argmax(count == 0))
        bonus = kappa * np.sqrt(np.log(t + 1) / count)  # t + 1: round number
        return int(np.argmax(wins / count + bonus))
    return rule

def thompson(wins, count, t, rng):
    """Draw each arm's mean from its Beta posterior; play the largest draw."""
    return int(rng.beta(1 + wins, 1 + count - wins).argmax())

for i, (name, rule) in enumerate([('UCB, kappa = 0.5', ucb(0.5)),
                                  ('Thompson', thompson)], 3):
    regret[name] = regret_runs(arms, rule, i)
    report(name, regret[name])
pulls = np.unique(np.geomspace(1, 2000, 100).astype(int))  # even on a log axis
d2l.plot_curves({name: r[:, pulls - 1] for name, r in regret.items()},
                xlabel='pulls', ylabel='cumulative regret', x=pulls,
                xscale='log', yscale='log', band='iqr')
UCB, kappa = 0.5: regret after 2000 pulls: median  38 (quartiles 29 to 46)
        Thompson: regret after 2000 pulls: median  30 (quartiles 26 to 36)

Both rules end with a median regret of roughly 3030 to 4040, about a third of the fixed ϵ\epsilon and below the annealed rule. They are close to each other: the difference between them is smaller than the spread over runs. On logarithmic axes a regret that grows linearly is a line of slope one. Greedy and fixed ϵ\epsilon approach that slope, while UCB, Thompson sampling and the annealed rule bend away from it. 20002000 pulls cannot establish an asymptotic rate, but the bend is what logarithmic growth looks like.

14.4.2.4 The UCB Coefficient#

The value κ=0.5\kappa = 0.5 lies below the range κ>1\kappa > 1 that the theory covers. How much does the coefficient matter? The cell plays forty runs for each of three coefficients and prints the median and extremes of the regret:

for j, kappa in enumerate([0.15, 0.5, np.sqrt(2)]):
    r = regret_runs(arms, ucb(kappa), 10 + j, num_runs=40)[:, -1]
    print(f'kappa = {kappa:.2f}: regret after 2000 pulls over 40 runs: median '
          f'{np.median(r):3.0f}, min {r.min():3.0f}, max {r.max():3.0f}')
kappa = 0.15: regret after 2000 pulls over 40 runs: median   8, min   4, max 709
kappa = 0.50: regret after 2000 pulls over 40 runs: median  38, min  21, max  59
kappa = 1.41: regret after 2000 pulls over 40 runs: median 204, min 173, max 241

At 20002000 pulls the coefficient decides the result. UCB1, κ=2\kappa = \sqrt{2}, ends near 200200, five to six times the regret of κ=0.5\kappa = 0.5. With κ=0.15\kappa = 0.15 the median is the smallest, about ten, but the maximum is large. With so small a bonus, a run in which the best arm starts with a few unlucky pulls can settle on a worse arm and keep losing 0.30.3 or more per pull. This happens in roughly one run in ten; the median hides these runs, and the mean does not.

One might expect the theory's κ\kappa to win once the horizon is long enough. On these arms it does not: in runs of 10510^5 pulls and more, which exercise 4 reproduces, κ=2\kappa = \sqrt{2} keeps six to eight times the regret of κ=0.5\kappa = 0.5, and both grow like log⁡t\log t. The bonus accounts for the factor. UCB keeps pulling a worse arm aa until its bonus κlog⁡t/n(a)\kappa\sqrt{\log t / n(a)} has shrunk to about Δa\Delta_a, that is for about κ2log⁡t/Δa2\kappa^2 \log t / \Delta_a^2 pulls, so in long runs the ratio of the two regrets approaches (2/0.5)2=8(\sqrt{2}/0.5)^2 = 8. What the theory guarantees is something else: κ=2\kappa = \sqrt{2} has logarithmic regret on every bandit with rewards in [0,1][0, 1], and κ=0.5\kappa = 0.5 has no such guarantee. Hoeffding's inequality must hold for the worst case, an arm with variance 1/41/4. The best arm here has variance 0.9×0.1=0.090.9 \times 0.1 = 0.09, so its empirical mean is more accurate than the inequality assumes, and a smaller bonus is enough on this problem. Thompson sampling matches the tuned UCB rule without a constant to tune.

14.4.2.5 A Lower Bound on Regret#

How low can regret go? Lai and Robbins proved that any rule that does well on every bandit of a family must, in tt pulls, pull each worse arm aa at least about ln⁡t/D(μa ∥ μ∗)\ln t / D(\mu_a \,\|\, \mu^*) times (Lai and Robbins, 1985). Here

D(p ∥ q)=pln⁡pq+(1−p)ln⁡1−p1−q(14.4.3)D(p \,\|\, q) = p \ln \frac{p}{q} + (1 - p) \ln \frac{1 - p}{1 - q} \tag{14.4.3}

is the relative entropy between Bernoulli distributions with means pp and qq. It is small when the two means are close, so an arm that is nearly as good as the best is hard to tell apart and must be pulled more often. For the arm with mean 0.600.60, D(0.60 ∥ 0.90)=0.31D(0.60 \,\|\, 0.90) = 0.31, and it must be pulled about ln⁡2000/0.31≈24\ln 2000 / 0.31 \approx 24 times by round 20002000. With (14.4.1), the regret of every such rule grows at least like ln⁡t\ln t. UCB and Thompson sampling grow at this rate, so in long runs no rule does better by more than a constant factor; fixed ϵ\epsilon, whose regret grows linearly, does not reach this rate.

14.4.3 Exploration in MDPs#

In a bandit every arm can be pulled in every round. In an MDP, a state-action pair can be tried only from its state, and reaching that state may take a particular sequence of actions. Random actions produce a given sequence with a probability that falls geometrically in its length: with two actions, a particular sequence of kk actions comes up with probability 2−k2^{-k}.

14.4.3.1 A Chain of States#

A chain makes this exact. It has NN states in a row and starts in state 00. Action 11 moves one state on, action 00 returns to the start, and entering the last state gives reward 11 and ends the episode, which is cut after 2N2N steps. To succeed, the agent must choose action 11 in N−1N - 1 consecutive steps, since a single action 00 sends it back to the start (Figure 14.4.4).

Figure 14.4.4:

The chain with NN states. Action 11 (blue) moves one state on, action 00 (orange) returns to state 00 from anywhere, and the only reward is the 11 for entering the last state, which ends the episode.

The function chain builds this MDP as a d2l.TabularMDP(P, r, gamma), the arrays of Section 14.1: P[s, a] is the distribution of the next state and r[s, a] the expected reward. The function success_random computes the exact probability that uniformly random actions reach the end within horizon steps. It averages the kernel over the two actions, Pπ(s′∣s)=12∑aP(s′∣s,a)P_\pi(s' \mid s) = \frac12 \sum_a P(s' \mid s, a), and propagates the state distribution from state 00 through PπP_\pi one step at a time. The last state is absorbing, so its probability after horizon steps is the probability of having arrived:

def chain(N, gamma):
    """N states in a row: action 1 moves one state on, action 0 returns to
    the start; entering the last state pays 1 and ends the episode."""
    P, r = np.zeros((N, 2, N)), np.zeros((N, 2))
    P[:, 0, 0] = 1
    P[np.arange(N - 1), 1, np.arange(1, N)] = 1
    P[N - 1] = 0
    P[N - 1, :, N - 1] = 1              # the last state is absorbing
    r[N - 2, 1] = 1
    return d2l.TabularMDP(P, r, gamma)

def success_random(mdp, horizon):
    """Exact probability that uniformly random actions, started in state 0,
    reach the last state within `horizon` steps."""
    P_pi, d = mdp.P.mean(axis=1), np.eye(mdp.num_states)[0]
    for _ in range(horizon):
        d = d @ P_pi
    return d[-1]

d2l.q_learning(seed, Q, visits, env, num_episodes, gamma, alpha, epsilon) of Section 14.3 updates the table Q in place from transitions it collects with ϵ\epsilon-greedy actions, counts visits in visits, and yields the return of each episode. It touches the environment only through env.reset and env.step, and the class ModelEnv gives a TabularMDP these two methods: reset puts the agent in state 00, and step draws the next state from P[s, a], reporting termination in a terminal state and truncation after max_steps steps.

class ModelEnv:
    """Gymnasium's reset/step on a TabularMDP: start in state 0, draw s'
    from P[s, a], end in a terminal state or after max_steps steps."""
    def __init__(self, mdp, terminal, max_steps):
        self.mdp, self.terminal, self.max_steps = mdp, terminal, max_steps
        self.rng = np.random.default_rng(0)

    def reset(self, seed=None):
        if seed is not None:
            self.rng = np.random.default_rng(seed)
        self.s, self.t = 0, 0
        return self.s, {}

    def step(self, a):
        s, self.t = self.s, self.t + 1
        self.s = int(self.rng.choice(self.mdp.num_states, p=self.mdp.P[s, a]))
        return (self.s, self.mdp.r[s, a], self.s in self.terminal,
                self.t >= self.max_steps, {})

The learner is the Q-learning of Section 14.3 with one change: every entry of the table starts at 11, an upper bound on every return, and the agent acts greedily, ϵ=0\epsilon = 0. This is optimistic initialization, the idea behind UCB applied to a table: an untried pair looks as good as anything can be. For comparison, the cell prints the expected number of episodes until random actions first succeed, the inverse of the probability from success_random. Q-learning from a zero table acts exactly like this until its first success, whatever its ϵ\epsilon, because every row is a tie broken at random (exercise 1):

def first_success(env, gamma, seed, max_episodes=100):
    """Episodes until greedy Q-learning from an optimistic table first
    reaches the goal (inf if it never does within max_episodes)."""
    # Optimistic: every entry starts at 1, an upper bound on every return
    Q = np.ones((env.mdp.num_states, env.mdp.num_actions))
    returns = d2l.q_learning(seed, Q, np.zeros_like(Q), env, max_episodes,
                             gamma, epsilon=lambda episode: 0.0)
    return next((k + 1 for k, ret in enumerate(returns) if ret > 0), np.inf)

for N in (4, 8, 12, 16, 20):
    mdp = chain(N, gamma=0.95)
    env = ModelEnv(mdp, terminal={N - 1}, max_steps=2 * N)
    first = [first_success(env, mdp.gamma, seed) for seed in range(20)]
    print(f'N = {N:2d}: episodes to the first success: random '
          f'{1 / success_random(mdp, 2 * N):5.0f} (mean), optimistic '
          f'{np.median(first):2.0f} (median, max {max(first):.0f})')
N =  4: episodes to the first success: random     2 (mean), optimistic  2 (median, max 2)
N =  8: episodes to the first success: random    23 (mean), optimistic  3 (median, max 4)
N = 12: episodes to the first success: random   273 (mean), optimistic  4 (median, max 5)
N = 16: episodes to the first success: random  3449 (mean), optimistic  6 (median, max 7)
N = 20: episodes to the first success: random 45590 (mean), optimistic  8 (median, max 10)

Random actions need about twice as many episodes for every state added: 2323 at N=8N = 8, about 3,4003{,}400 at N=16N = 16, about 46,00046{,}000 at N=20N = 20. The optimistic learner needs a handful, about eight at N=20N = 20, a number that grows roughly in proportion to NN. Optimism works because a tried pair is marked as tried. After one try of a pair that pays nothing, its entry moves toward a target r+γmax⁡a′Q(s′,a′)r + \gamma \max_{a'} Q(s', a') of at most γ<1\gamma \lt 1, so it falls below every untried entry, which still reads 11. The greedy rule therefore prefers a pair it has not tried, and the learner works through the chain systematically instead of by chance. The number of episodes grows polynomially in NN instead of exponentially.

14.4.3.2 Count-Based Bonuses#

Optimistic initialization explores a pair until its first try lowers its value. In a stochastic MDP one try says little, and a pair needs repeated visits before its value is known. Count-based bonuses carry the UCB rule over to MDPs. They add to the reward of each pair a bonus that shrinks like 1/n(s,a)1/\sqrt{n(s, a)} with its visit count, and let the value backups carry the bonus back to the states that lead there, so the agent plans its way toward pairs it knows little about. Model-based interval estimation does this with a learned model (Strehl and Littman, 2008), and Q-learning with such bonuses has a regret that grows like the square root of the number of steps, up to factors polynomial in the size of the MDP (Jin et al., 2018). With a network in place of a table, states are rarely visited twice, and practical agents replace the count by a learned measure of novelty, such as the error of a network trained to predict a fixed random function of the state (Burda et al., 2019).

14.4.3.3 Coverage on the Lake#

Does the lake need any of this? In Section 14.3, Q-learning on the calm lake, where every move goes where it is commanded, used the step size α=0.9\alpha = 0.9 for 512512 episodes, and its greedy table solved the lake. We repeat that run with 256256 episodes under three schedules: greedy, ϵ=0\epsilon = 0; always random, ϵ=1\epsilon = 1; and the default of q_learning, d2l.linear_schedule(1.0, 0.05, 128), which lowers ϵ\epsilon linearly from 11 to 0.050.05 over the first half of training. In each of twenty runs the cell records, after every episode, the fraction of the 4444 pairs tried at least once (1111 cells to act from, four actions each). It also records the fraction of training episodes that reach the goal, and whether the greedy policy of the final table does, which d2l.evaluate of Section 14.2 checks in one episode on this deterministic lake:

def coverage_run(env, live, epsilon, seed, num_episodes=256, gamma=0.95):
    """One run of Q-learning with step size 0.9: the fraction of live pairs
    tried after each episode, the fraction of episodes won, and whether the
    final greedy policy reaches the goal."""
    Q = np.zeros((env.observation_space.n, env.action_space.n))
    visits, tried, returns = np.zeros_like(Q), [], []
    for ret in d2l.q_learning(seed, Q, visits, env, num_episodes, gamma,
                              alpha=lambda n: 0.9, epsilon=epsilon):
        tried.append((visits[live] > 0).mean())  # visits is updated in place
        returns.append(ret)
    solved = d2l.evaluate(env, lambda s, rng: int(Q[s].argmax()), 1)
    return tried, np.mean(returns), solved

calm = gym.make('FrozenLake-v1', is_slippery=False)
live = np.array([c not in b'HG' for c in calm.unwrapped.desc.flatten()])
schedules = {'greedy': lambda k: 0.0,
             'epsilon = 1': lambda k: 1.0,
             'linear, 1 to 0.05': d2l.linear_schedule(1.0, 0.05, 128)}
num_seeds, coverage = 20, {}
for name, epsilon in schedules.items():
    tried, won, solved = zip(*[coverage_run(calm, live, epsilon, seed)
                               for seed in range(num_seeds)])
    coverage[name] = np.array(tried)                  # (seed, episode)
    print(f'{name:>17}: pairs tried {np.median(coverage[name][:, -1]):.0%}, '
          f'episodes won {np.median(won):.0%} (medians); greedy policy '
          f'solves {np.sum(solved):.0f}/{num_seeds}')
d2l.plot_curves(coverage, xlabel='episode', ylabel='fraction of pairs tried',
                band='iqr')
           greedy: pairs tried 93%, episodes won 60% (medians); greedy policy solves 19/20
      epsilon = 1: pairs tried 100%, episodes won 2% (medians); greedy policy solves 19/20
linear, 1 to 0.05: pairs tried 98%, episodes won 58% (medians); greedy policy solves 19/20

On the calm lake, random tie-breaking alone makes the greedy rule explore: it tries about nine pairs in ten, and its final greedy table solves the lake in nearly every run, as do the tables of the other two schedules. With ϵ=1\epsilon = 1 the agent tries every pair but reaches the goal in almost none of its training episodes. Learning a good table and collecting reward while learning are different goals, and ϵ\epsilon trades one for the other. What limits all three schedules is the first success. Until then every entry is zero and every schedule acts at random, so for a given seed the first success arrives in the same episode under every schedule. The goal is six moves from the start, and random walks find it within a few dozen episodes. The lake is too small to show the difficulty of the chain. On slippery ice, the lake of this section's opening, a single visit to a pair says little about it, and trying every pair once is not enough (exercise 1).

14.4.4 Exploration on the Verifier Task#

The verifier task of Section 14.1 has four arithmetic prompts xx, nine candidate responses yy shared by all of them, and a verifier with r(x,y)=1r(x, y) = 1 if yy is the prompt's answer and 00 otherwise. The reference policy πref\pi_{\textrm{ref}}, a softmax over one row of preferences per prompt, spreads its probability evenly over the eight ordinary candidates. It gives almost none to the ninth, a hedge that lists every answer and that the exact-match verifier never accepts. Two prompts, 2+22+2 and 3×43 \times 4, have their answer among the candidates; for 8×88 \times 8 and 9×99 \times 9 no candidate matches. A sampled response is correct with probability 0.0620.062 on average over the prompts.

Each round a prompt arrives, one response is scored, and the episode ends. The prompt is a context that the agent observes but cannot influence, and the response is the arm. A bandit whose best arm depends on an observed context is a contextual bandit: the agent faces one bandit per prompt and, with a model, can share what it learns across prompts.

A language model explores such a task differently from the rules above. A common way to train it, derived in Section 14.8, samples a group of KK responses to a prompt from the current policy, scores them, and raises the probability of the responses that scored above the group's average while lowering those below it. A group in which every response scores the same has nothing above or below its average and changes nothing. If the policy answers a prompt correctly with probability p=π(correct∣x)p = \pi(\textrm{correct} \mid x), a group of KK independent responses is identical with probability

(1−p)K+pK,(14.4.4)(1 - p)^K + p^K, \tag{14.4.4}

the chance that all KK are wrong plus the chance that all are right. The cell samples 1,0001{,}000 groups per prompt and compares the fraction of identical groups with this formula. It does so at the reference policy and at a policy that has nearly solved the two answerable prompts, built by hand by raising the preference of each correct answer by 55; it is not a trained policy. The array R[x, y] holds the verifier's score of every pair:

task = d2l.VerifierTask()
R, rng = task.R, np.random.default_rng(0)     # R[x, y] = r(x, y)
for name, pi in [('reference', task.pi_ref),
                 ('solved', task.policy(task.theta_ref + 5 * R))]:
    p = (pi * R).sum(axis=1)            # chance that one response is right
    for K in (2, 8, 32):
        same = []                       # per prompt: all K rewards equal
        for x in range(len(task.prompts)):
            ys = rng.choice(len(task.resp), (1000, K), p=pi[x])
            same.append((R[x, ys] == R[x, ys[:, :1]]).all(axis=1).mean())
        print(f'{name:>9}, K = {K:2d}: identical {np.round(same, 2)}, '
              f'mean {np.mean(same):.2f}, '
              f'exact {np.mean((1 - p) ** K + p ** K):.2f}')
reference, K =  2: identical [0.78 0.78 1.   1.  ], mean 0.89, exact 0.89
reference, K =  8: identical [0.35 0.33 1.   1.  ], mean 0.67, exact 0.67
reference, K = 32: identical [0.01 0.02 1.   1.  ], mean 0.51, exact 0.51
   solved, K =  2: identical [0.89 0.93 1.   1.  ], mean 0.96, exact 0.96
   solved, K =  8: identical [0.67 0.7  1.   1.  ], mean 0.84, exact 0.85
   solved, K = 32: identical [0.21 0.21 1.   1.  ], mean 0.60, exact 0.61

The sampled fractions agree with the formula. Half of all groups are identical at every KK: on the two prompts without a correct candidate every response fails, and no amount of sampling finds an answer that the policy cannot produce. On the two answerable prompts the reference is right with probability p≈0.125p \approx 0.125, and larger groups are more often mixed: about 78%78\% of the groups of two are identical, a third of the groups of eight, and almost none of the groups of 3232. Once a prompt is nearly solved, groups become identical again, about two thirds of them at K=8K = 8, now because almost every response is right, and learning on that prompt slows.

The learner explores only through its own sampling. The probability that a group contains at least one correct response, 1−(1−p)K1 - (1 - p)^K, is the pass@KK score used to evaluate models for code and reasoning (Chen et al., 2021). On a prompt that the policy usually fails, it is nearly the probability that a group can teach anything. Sampling at a higher temperature spreads probability over more responses and mixes more groups on a prompt the policy has nearly solved, at the cost of more wrong answers. It cannot help where no candidate is right.

Every rule in this section learns which actions are good by trying them, and the trying costs pulls of worse arms, episodes of random walking, or groups that teach nothing. On the chain an untried pair is hard to reach, and on the verifier task a correct answer that the policy never produces is never found. An expert who shows good actions would spare the agent all of this, and Section 14.5 learns from such demonstrations.

14.4.5 Summary#

An agent that learns from its own actions must try actions that look worse to find out whether they are, and regret measures what this costs against an agent that knew the best action from the start. On a ten-armed bandit, the greedy rule settles on the first arm that pays out, and a fixed ϵ\epsilon loses the same amount per pull forever. An ϵ\epsilon that decays like 1/t1/t, UCB and Thompson sampling make regret grow only like the logarithm of the number of pulls, which Lai and Robbins showed is the best possible rate. UCB's coefficient decides its regret on a given problem: the coefficient of the theory is safe on every bandit and slower on this one. In an MDP, random actions find a required sequence of actions with a probability that falls geometrically in its length, while optimistic values make the agent seek out untried pairs. When a verifier scores groups of sampled responses, groups that score alike carry no signal, and the policy's own sampling is its only exploration.

14.4.6 Exercises#

  1. [code] The exploration schedule. ●● On a zero-initialized table, epsilon_greedy breaks every tie uniformly at random, and every reward of FrozenLake is nonnegative.

    1. Show that every entry of the table stays exactly zero until the first successful episode, and that until then every ϵ\epsilon produces the same behavior. What does this imply for the episode of the first success as a criterion for choosing ϵ\epsilon?
    2. After the first success, which entries are positive? Show that an entry, once positive, stays positive, and that the greedy rule, ϵ=0\epsilon = 0, never again tries the zero entries of a row that contains a positive entry. The coverage experiment of this section shows this rule trying about nine pairs in ten on the calm lake and learning to succeed there. Predict whether it also learns a good policy on slippery ice.
    3. The optional argument epsilon of q_learning is a function from the episode index to the exploration rate, such as those returned by linear_schedule. On the slippery lake, with the 40004000 episodes of Section 14.3, run five seeds each with ϵ\epsilon held at 00, 0.10.1, 0.50.5 and 11, and with the schedule linear_schedule(1.0, 0.05, num_episodes // 2) that q_learning uses in Section 14.3. For each setting, report the mean training return, the success rate of the final greedy policy under d2l.evaluate over 10001000 episodes, the fraction of pairs tried, and max⁡s∣max⁡aQ^(s,a)−V∗(s)∣\max_s |\max_a \hat{Q}(s, a) - V^*(s)|. Was your prediction right?
    4. Rank the settings by training return and by greedy success rate. Where do the two rankings disagree, and what explains the disagreement?
  2. Logarithmic regret of UCB. ●● With κ=2\kappa = \sqrt{2}, the rule (14.4.2) is UCB1 of Auer et al. (2002), whose Theorem 1 bounds the expected regret after tt pulls of arms with rewards in [0,1][0, 1] by

∑a: Δa>08ln⁡tΔa+(1+π23)∑aΔa,Δa=μ∗−μa.(14.4.5)\sum_{a:\, \Delta_a > 0} \frac{8 \ln t}{\Delta_a} + \Big(1 + \frac{\pi^2}{3}\Big) \sum_a \Delta_a, \qquad \Delta_a = \mu^* - \mu_a. \tag{14.4.5}

Derive this bound. The tool is Hoeffding's inequality: the mean μ^n\hat{\mu}_n of nn independent rewards in [0,1][0, 1] with mean μ\mu satisfies

P(μ^n≥μ+h)≤e−2nh2,(14.4.6)P(\hat{\mu}_n \geq \mu + h) \leq e^{-2 n h^2}, \tag{14.4.6}

and likewise for the lower tail.

  1. Fix a suboptimal arm aa and let n0=⌈8ln⁡t/Δa2⌉n_0 = \lceil 8 \ln t / \Delta_a^2 \rceil. Show that if arm aa is pulled at a step u≤tu \leq t while n(a)≥n0n(a) \geq n_0, then either the index of the best arm lies at or below μ∗\mu^*, or the empirical mean of arm aa exceeds μa\mu_a by more than its bonus.
  2. For fixed pull counts of both arms, bound the probability of each of the two events at step uu by u−4u^{-4}. Take a union bound over the counts and sum over uu to show E[n(a)]≤n0+π2/3\mathbb{E}[n(a)] \leq n_0 + \pi^2/3 after tt pulls.
  3. Combine part (b) with the regret decomposition (14.4.1) to obtain the theorem. Evaluate the bound for arms at t=2000t = 2000 and compare it with the median regret near 200200 that the coefficient sweep of this section prints for κ=2\kappa = \sqrt{2}. Which step of the proof loses the most?
  4. Repeat the argument for a general coefficient κ\kappa. For which κ\kappa does the sum over steps in part (b) still converge, and what does the answer say about the section's choice κ=0.5\kappa = 0.5? (The union bound over counts is loose; a peeling argument of (Bubeck and Cesa-Bianchi, 2012) reaches every κ>1\kappa > 1.)
  1. Thompson's posterior. ● Thompson sampling draws each arm's mean from Beta(1+w,1+n−w)\mathrm{Beta}(1 + w, 1 + n - w) after ww wins in nn pulls.

    1. Show that this is the posterior of a Bernoulli mean under the uniform prior on [0,1][0, 1].
    2. Arm A has 33 wins in 44 pulls and arm B is untried. Show that Thompson sampling pulls B with probability 1/31/3. More generally, show that against an untried arm it pulls the untried one with probability one minus the posterior mean of the other.
    3. Show that, given the data, Thompson sampling pulls each arm with the posterior probability that this arm has the largest mean.
    4. [code] Replace the uniform prior by Beta(1,9)\mathrm{Beta}(1, 9), which expects arms to be bad, and by Beta(9,1)\mathrm{Beta}(9, 1), which expects them to be good. How does the median regret after 20002000 pulls change? Which of the two priors acts like optimistic initialization, and why?
  2. [code] Long horizons. ●● bandit_run plays one run at a time.

    1. Write a version that plays many runs at once: keep wins and count as arrays of shape (runs, arms) and choose the arms of all runs in one step, for UCB and for Thompson sampling. Check that its medians at 20002000 pulls agree with those of this section up to the spread over runs.
    2. Run 200200 runs of UCB with κ∈{0.15,0.3,0.5,2}\kappa \in \{0.15, 0.3, 0.5, \sqrt{2}\} and of Thompson sampling to 10510^5 pulls, and plot the median regret on logarithmic axes. Does κ=2\kappa = \sqrt{2} catch up with κ=0.5\kappa = 0.5?
    3. For κ=0.15\kappa = 0.15, plot the mean regret as well as the median. Why do they grow at different rates, and which of the two describes the risk to a user who runs the rule once?
  3. [extended] Deep exploration. ●● Take the chain of this section: states 0,…,N−10, \ldots, N-1, start in state 00, action 00 returns to state 00, action 11 advances one state, reaching state N−1N-1 gives reward 11 and terminates, and episodes are truncated after 2N2N steps.

    1. Show that the uniformly random policy succeeds in one episode with probability at most 2N⋅2−(N−1)2N \cdot 2^{-(N-1)}, so that the expected number of episodes up to the first success is at least 2N−2/N2^{N-2}/N, and compare the bound with the exact values that the chain cell prints. By part (a) of exercise 1, ϵ\epsilon-greedy Q-learning from a zero table acts uniformly at random until its first success, so the bound holds for it whatever the value of ϵ\epsilon.
    2. Trace the first episodes of the optimistic greedy learner on the chain with N=4N = 4: which entries does each episode lower? Use the pattern to argue that the number of episodes it needs grows at most polynomially in NN.
    3. Move both learners to gym.make('FrozenLake-v1', map_name='8x8', is_slippery=True), which truncates at 100100 steps. Compare the optimistic greedy learner with the zero-initialized learner under the schedule linear_schedule(1.0, 0.05, num_episodes // 2) at 40004000 and 2000020000 episodes, five seeds each. Report the success rate of the greedy policy under d2l.evaluate, the environment steps used, and, as a reference, the success rate of the optimal policy computed by value iteration. Does optimism keep its advantage on a stochastic map, and what does it cost?

    The project takes a few minutes of CPU time.

  4. Groups that teach nothing. ● On a prompt that the policy answers correctly with probability pp, a group of KK independent responses is identical with probability (1−p)K+pK(1 - p)^K + p^K.

    1. For a fixed K≥2K \geq 2, which pp makes a mixed group most likely, and how likely is it then?
    2. For small pp, show that the smallest KK for which a group is mixed with probability at least 1/21/2 is about ln⁡2/p\ln 2 / p. Evaluate it for the reference policy of the verifier task, p≈0.125p \approx 0.125, and for p=0.01p = 0.01.
    3. The prompts of a training set have success probabilities pp spread uniformly over [0,1][0, 1]. What fraction of the groups of size K=8K = 8 is identical, and which prompts produce them?
    4. On the two prompts of VerifierTask without a correct candidate, every group is identical for every KK and every sampling temperature. Name two changes, to the task or to the learner, that would let it learn on these prompts, and say what each needs that the verifier alone does not provide.

Discussions