Vocabulary for this course

The terms the chapters assume, in plain words. You can open this from any chapter.

action
One choice the agent can make at a given moment, from a fixed set available to it.
agent
The learner: it sees a state, chooses an action and receives a reward.
discount factor
A number between zero and one that scales down later rewards, so a reward now is worth more than the same reward later.
environment
Everything outside the agent that responds to its actions and hands back a reward and a new state.
episode
One complete run from a starting state until the task ends, after which the agent starts again.
exploitation
Choosing the action that looks best right now, using only what has been learned so far.
exploration
Trying an action that may not be best, in order to learn whether something better exists.
Markov decision process
A task described by states, actions, rewards and the chances of moving between them, where the current state is enough to decide from.
policy
A rule that picks an action for each state; learning changes this rule, not the world.
Q-value
How good an action is in a given state: the average return from taking it and then following a fixed rule.
return
The total reward collected from this point onward, with later rewards scaled down by the discount factor.
reward
A number the environment sends after an action, saying how good that step was.
state
What the agent uses to decide; it must be complete enough that two situations needing different actions look different.
value
How good a state is: the average return from being there and then following a fixed rule.

Reinforcement learning from the ground up

Exploration versus exploitation

Every action is either information or income. The rules that balance the two decide how fast an agent learns, and how much it pays while learning.

Part 6 of 10

Advanced15 min readBack to Courses

The dilemma, stated plainly

The only way to learn that an action is good is to take it, and the only way to profit from knowing is to stop taking the others. That is the whole tension. Exploit what you know and you may miss something better; explore and you spend reward on information you may never use. Every algorithm in this course makes that choice somewhere, even when it hides it inside a value estimate. The trade-off is not a one-time decision; it is made again at every step.

The dilemma never fully disappears. Even a well-trained agent faces it whenever the world shifts, because the value it learned was a statement about the past. What changes is how expensive the answer is, not whether the question exists. A deployed agent is always slightly out of date, and exploration is how it catches up.

Decay is a schedule, not a fix

Epsilon-greedy with a decaying epsilon is the simplest answer. Explore a lot early, when estimates are meaningless, and almost never late, when they are good. A common schedule divides by the square root of the step, which explores often enough to keep learning but rarely enough to profit. The schedule is a guess about how fast the estimates improve, and a bad guess shows up as regret. A schedule that decays too slowly wastes pulls; one that decays too fast stops learning.

The schedule has to reach zero for the policy to converge, but reaching zero too early locks in whatever luck produced. The useful pattern is called greedy in the limit with infinite exploration: every action is tried infinitely often, and the exploration rate still tends to zero. The name is a mouthful, but the two conditions are the whole design: keep trying everything, and stop paying for it.

Thompson sampling with a real distribution

import numpy as np
from scipy.stats import beta

successes = np.ones(3)
failures = np.ones(3)
rates = [0.3, 0.5, 0.7]

for pull in range(500):
    arm = int(np.argmax(np.random.beta(successes, failures)))
    reward = np.random.binomial(1, rates[arm])
    successes[arm] += reward
    failures[arm] += 1 - reward

print(successes, failures)

Fixed epsilon against a decaying one

Run both agents and compare the regret. Then make the decay slower and see whether the final regret improves or gets worse.

Output
Click "Run" to execute your code

Optimism in the face of uncertainty

A more principled rule explores the action whose value could plausibly be highest, not the one chosen at random. Upper confidence bounds do this by adding a bonus that shrinks as an action is tried: the estimate plus a term proportional to the square root of the log of the total steps divided by that action’s count. The bonus is large when the count is small, so an untried action is always worth one pull. It is optimism with a budget, and the budget comes from the count.

The bonus is not a heuristic. It is a statement about how uncertain an average is after n samples, and the resulting rule has regret that grows logarithmically. Actions that have been tried little look promising because they are unknown, which is exactly the right reason to try them. The constant in front of the bonus sets how greedy the rule is, but the shape is what gives the guarantee. That guarantee is why UCB is the reference rule for tabular bandits.

In a tabular problem, count-based bonuses are hard to beat. In a large state space most states are visited once, so replace the count with a density model or a learned uncertainty estimate.

Random is not curious

Random exploration works in a bandit with ten arms and fails in a world with a million states. The chance of stumbling on the one useful behaviour by accident is negligible, so an agent that only adds noise never finds it. Exploration has to be directed by what the agent does not yet understand. In a continuous space, the probability of sampling the useful action is effectively zero.

That is the motivation for curiosity, disagreement between an ensemble of models, and goal proposals that maximise learning progress. Each is an attempt to answer a harder question than “which action is uncertain”: which action would teach me the most about the part of the world that matters. None of them is free, and all of them can be fooled by a reward that is merely novel. The hard part is defining what counts as progress rather than mere surprise.

Upper confidence bounds

Run both agents on the same arms. Then add a fourth arm with rate 0.9 and confirm that UCB finds it without being told.

Output
Click "Run" to execute your code

Check what you learned

One question at a time. A wrong answer explains itself — take another go.

Part 1 of 5

What is the exploration-exploitation trade-off?