Exploration

You only learn about the choices you try. This page shows the cost of trying too little and too much.

Key idea
Exploration vs exploitation
Real world
You have a favourite restaurant. Going back is a safe good meal (exploit). Trying the new place down the road might be better or worse, but it's the only way to find out (explore).
In an LLM
Sampling temperature and the number of answers sampled per prompt. At temperature 0 the model always gives the same answer, so it never discovers a better one.

The dilemma

Every choice either exploits what you know or explores to learn more. Only exploiting locks you into the first thing that worked. Only exploring means you never use what you learned. Every RL agent needs some balance.

The same trade-off shows up outside RL:

  • A/B testing and ads: keep showing the best-performing ad, or give the new one some traffic?
  • Clinical trials: give patients the treatment that looks best so far, or keep testing the alternatives?
  • Games: AlphaGo Zero adds noise to its search during self-play so it keeps trying unusual moves.
  • Language models: a model that always solves maths problems the same way never finds out whether another approach, such as checking its work, scores higher.

The setup: four slot machines

Each arm pays 1 with a hidden probability, otherwise 0. One arm is best. You have to find it by pulling. This is a bandit: RL with one decision and no next state, the simplest place to study exploration.

Pull the arms yourself

Arm A
pulls0
Q̂–
Arm B
pulls0
Q̂–
Arm C
pulls0
Q̂–
Arm D
pulls0
Q̂–
Each arm pays 1 with a hidden probability. Q̂ is the average reward you have seen from that arm.

Let a strategy pull 400 times

ε-greedy: pick the arm with the best average so far. A fraction ε of the time, pick an arm at random instead. Move the slider and the 300 simulated runs re-run for that ε.

your ε = 0.10pure greedy, ε = 0best possible: always the 0.70 arm
your average reward0.637
greedy0.549
pulls on the best arm73%
A little exploration pays: 73% of pulls go to the best arm, against 38% for pure greedy.
Average reward per pull, over 300 simulated runs of 400 pulls each.
example · one ε-greedy decision

After some pulls, your average payouts are Q^=(0.4, 0.6, 0.3, 0.5)\hat Q = (0.4,\ 0.6,\ 0.3,\ 0.5), and ε = 0.1.

  • With probability 0.9 you exploit: pull arm 2, the best so far.
  • With probability 0.1 you pick one of the four at random, which can also land on arm 2.

So arm 2 gets 0.9+0.1/4=0.9250.9 + 0.1 / 4 = 0.925 and each other arm 0.0250.025.

You pull arm 2 and it pays 1. It had paid an average of 0.6 over 5 pulls, so the new average over 6 pulls is

Q^2←0.6+16(1−0.6)≈0.67\hat Q_2 \leftarrow 0.6 + \tfrac{1}{6}(1 - 0.6) \approx 0.67

That "old estimate plus a step toward the new sample" pattern comes back in value learning and every later algorithm.

example · temperature in a language model

For a maths problem, a model has three ways to start, with logits 2, 1 and 0: (A) answer straight away, (B) work step by step, (C) work step by step and then check. Suppose C scores best but the model rarely picks it. Temperature TT divides the logits before the softmax (which turns scores into probabilities):

TemperatureABCChance 8 samples never try C
0100%0%0%100%
0.587%12%1.6%88%
167%24%9%47%

At temperature 0 the best approach is never sampled, so RL can never reward it. At temperature 1 with a group of 8, more than half of the prompts include at least one try of C.

In language models

plays the role of ε. A model sampled at temperature 0 always gives its current favourite answer, so there is nothing to compare it with. RL methods for language models sample several answers per prompt at a temperature around 1, partly for this reason (GRPO).

going deeper

Smarter exploration

ε-greedy explores blindly. Two common alternatives explore where it is most useful:

Upper confidence bound (UCB) picks the arm with the highest optimistic estimate. Arms that have been tried less often get a larger bonus:

a=arg⁡max⁡i[Q^i+cln⁡tni]a = \arg\max_i \Big[\hat Q_i + c \sqrt{\tfrac{\ln t}{n_i}}\Big]

Thompson sampling keeps a belief distribution over each arm's payout, draws one sample from each, and pulls the arm with the highest draw. For 0/1 rewards the belief is a Beta(1+wins, 1+losses)\text{Beta}(1 + \text{wins},\ 1 + \text{losses}) distribution.

The incremental mean from the example, written generally:

Q^n=Q^n−1+1n(rn−Q^n−1)\hat Q_n = \hat Q_{n-1} + \frac{1}{n}(r_n - \hat Q_{n-1})

Related

  • rl-loop - An agent acts, the world responds with a new state and a reward, and the agent learns to collect more reward. Every later page builds on these five parts.
  • value - A reward often arrives many steps after the choice that earned it. A value function passes some of that credit back to earlier states.