ai theory
Reinforcement Learning Basics (9) — Between Familiar and New Choices
Junyoung Park · 2024-05-10 · 9 min
Introduction
The first article briefly introduced exploration and exploitation through a choice of lunch.
- Exploitation: Visit the favorite restaurant that has been best so far.
- Exploration: Try a new restaurant we do not know well yet.
We also used an -greedy policy in SARSA and Q-Learning, but did not examine in detail why exploration is necessary.
Choosing the action with the largest Q-value seems reasonable. The problem is that a Q-value during training is not the true action value. It is an estimate constructed from the experience collected so far.
An action that happened to produce a high reward in the first two trials can appear to be the best. If the agent keeps choosing only the greedy action, it may never try the alternatives and may never discover that a better choice exists.
Exploration gives up part of the immediate reward to purchase information. Because that information can lead to better decisions later, it may be valuable in the long run.
We begin with the multi-armed bandit, the simplest environment, which has no state transitions.
The Multi-Armed Bandit
The bandit takes its name from a slot machine with several handles. Selecting an action, or arm, produces one reward from an associated probability distribution.
- : the set of available actions
- : the unknown reward distribution produced by action
Unlike an MDP, a bandit has no next state. We can think of the current state as remaining unchanged at every step. The agent repeatedly chooses one action and observes one reward.
The true value of action is its mean reward.
Because the reward distribution is unknown, we estimate the value with samples obtained by actually choosing the action.
is the number of times action has been selected by time .
In the figure, A has an estimated value of , while B has . A greedy choice selects A. But C has been tried only once, so its value is highly uncertain. Its true mean could be .
Exploration means trying C to reduce this uncertainty.
When Greedy Gets Trapped by a First Impression
Suppose the true mean rewards of two actions are
Now suppose the first samples happen to be
The current estimates are for A and for B, so a greedy policy keeps selecting A. Repeated samples bring A's estimate closer to its true mean of , but B is never selected again and remains at .
The agent never discovers that B is actually better. A greedy action is optimal only under the current estimate; it is not guaranteed to be optimal under the still-unknown reward distributions.
Regret Is the Cost of a Missed Opportunity
In a bandit problem, we can compare exploration methods by cumulative reward. The same idea is often expressed through regret.
The value of the best action is
The expected reward missed by choosing at time can be viewed as instantaneous regret.
Total regret is its sum over time.
If we knew the best action, regret would be zero. In reality, we do not know which action is best. Choosing a low-reward action for exploration creates regret immediately. But if we never explore, we may remain trapped in a bad action and accumulate regret for a long time.
A good exploration algorithm balances the cost of acquiring information against the cost of repeatedly making the wrong choice.
ε-Greedy: Occasionally Try Something at Random
-greedy is one of the simplest and most widely used exploration methods.
- With probability , select the current greedy action.
- With probability , select one of all actions at random.
If there are actions, the final probability of choosing the greedy action is
while the probability of each other action is
This is because the random choice can select the greedy action again.
For example, with four actions and ,
Each other action has probability
The current best action is therefore chosen of the time, and each of the remaining three actions is selected with probability .
Should ε Remain Constant?
With a fixed , the agent continues to choose random actions at the same rate even after training has run for a long time. Exploration never stops, even when every action is already well understood.
We can instead use a decaying : explore broadly with a large value at the beginning and gradually reduce it.
If it decays too quickly, the agent may settle on the wrong action before exploring enough. If it decays too slowly, it continues unnecessary exploration long after finding a good action.
GLIE, a condition that often appears in convergence results for tabular control, requires two properties together.
- Every state-action pair is explored infinitely often.
- In the limit, the policy becomes greedy.
It is difficult to apply an asymptotic condition literally to finite training, but it gives us the right direction: provide enough opportunities to explore, while eventually choosing good actions more often.
Optimistic Initialization
Another approach is to initialize Q-values higher than their likely true values.
Suppose the true mean of every action is expected to lie roughly between and , but we begin with
When one action produces reward , that action's estimate falls. Unselected actions remain at , so even a greedy policy naturally tries them next.
The optimistic initial estimate says, “this action might be extremely good,” and thereby encourages exploration.
It is easy to implement, but it has limitations.
- We must decide how optimistic the initial value should be.
- The exploration effect weakens after every action has been tried once.
- If the environment changes later, the initial optimism does not return.
UCB: Value an Action Because We Do Not Know It Well
Random exploration in -greedy does not distinguish a bad action tried many times from an action tried only a few times. During an exploration step, both are random candidates.
Upper Confidence Bound, or UCB, adds an uncertainty bonus to each action's estimate.
The first term represents exploitation.
The second is the exploration bonus.
- If is small, the bonus is large.
- As an action is chosen more often and grows, the bonus shrinks.
- A larger places more importance on uncertainty.
Consider the following two actions.
| Action | Estimate | Bonus | UCB |
|---|---|---|---|
| A | |||
| B |
A has the higher mean estimate, but B has rarely been tried and therefore has a large bonus. UCB selects B. Once B is observed, its uncertainty and bonus decrease.
This principle is called optimism in the face of uncertainty. We optimistically account for the possibility that an unknown choice may be good, then automatically reduce that optimism as experience accumulates.
Returning from Bandits to MDPs
A bandit has no state transitions, but the same exploration principles extend to MDPs.
In -greedy Q-Learning, the agent uses the Q-values in each state and occasionally tries a random action.
A UCB-like method can choose actions by adding a visit-count or uncertainty bonus to each state-action value.
Exploration in an MDP is harder than in a bandit, however. An action changes the immediate reward, the states the agent will visit, and the data it will collect later. In some environments, the agent must open a door before it can explore the new states behind it.
This is why sparse-reward environments, where random actions are not enough, use more structured methods such as count-based exploration, curiosity, and intrinsic rewards. We will revisit these ideas when we discuss practical reinforcement learning.
More Exploration Is Not Always Better
Exploration is not free.
- A bad action can be dangerous in medical or robotic environments.
- In a recommendation service, users may leave while the system shows poor items.
- In offline RL, the agent cannot actually try new actions.
“Sufficient exploration” therefore does not mean choosing as many random actions as possible. We must consider both the value of information that reduces uncertainty and the cost of the trial itself.
At an introductory stage, the following questions are useful checks.
- Is every action being experienced at least occasionally?
- Is becoming small too quickly?
- Is exploration noise still active during evaluation?
- Are there dangerous actions that the training environment cannot permit?
- Has a change in the rewards made the old Q-values stale?
What to Remember
The central idea of this article is
Exploration trades part of the immediate reward for information that can reveal better actions in the long run.
More specifically:
- A greedy policy can remain trapped forever by a wrong early estimate.
- -greedy usually chooses the greedy action and explores randomly with a small probability.
- A fixed keeps exploring even after training is complete.
- Optimistic initialization encourages unvisited actions through high initial values.
- UCB adds an uncertainty bonus to the estimated value.
- Exploration is more difficult in an MDP because an action also changes the states the agent will visit later.
The next article returns to the temporal length of the update target. TD(0) looks ahead one step, while Monte Carlo looks all the way to a terminal state. We will examine the 2-step and 3-step returns between them, TD(), which blends every length, and eligibility traces.