Chapter Reinforcement Learning
Chapter Reinforcement Learning
3900
Shaunticlair Ruiz
Fall 2024
Contents
11 Reinforcement Learning 3
11.0.1 MDP Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
11.0.2 What if we don’t know as much? . . . . . . . . . . . . . . . . . . . . . 4
11.0.3 Learning about our MDP . . . . . . . . . . . . . . . . . . . . . . . . . 5
11.0.4 Reinforcement Learning . . . . . . . . . . . . . . . . . . . . . . . . . . 6
11.0.5 Supervised vs. Unsupervised vs. RL . . . . . . . . . . . . . . . . . . . 8
11.1 Reinforcement Learning Algorithms Overview . . . . . . . . . . . . . . . . . 9
11.1.1 Evaluating RL algorithms . . . . . . . . . . . . . . . . . . . . . . . . . 9
11.1.2 Different types of RL models . . . . . . . . . . . . . . . . . . . . . . . 10
11.1.3 Types of Reinforcement Learning . . . . . . . . . . . . . . . . . . . . . 11
11.2 Model-free methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
11.2.1 Q-learning: Computing Q from new data . . . . . . . . . . . . . . . . 14
11.2.2 Q-learning: Making an update rule . . . . . . . . . . . . . . . . . . . 15
11.2.3 Selecting our action: ϵ-greedy . . . . . . . . . . . . . . . . . . . . . . . 17
11.2.4 Q-learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
11.2.5 Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
11.2.6 Action and state space . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
11.2.7 An alternate view of Q-learning (Optional) . . . . . . . . . . . . . . . 22
11.2.8 Problems with Q-learning: Slow Convergence . . . . . . . . . . . . . 24
11.2.9 Deep Q-learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
11.2.10 Catastrophic Forgetting . . . . . . . . . . . . . . . . . . . . . . . . . . 30
11.2.11 Experience Replay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
11.2.12 Fitted Q-learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
11.2.13 Policy Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
11.3 Model-based RL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
11.3.1 Computing Tb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
1
MIT 6.390 Fall 2024 2
Reinforcement Learning
– Change the state of the world: what our system looks like.
• The transitions between states are probabilistic: we don’t know our exact next state,
but we know the odds of each possible next state.
Here’s our MDP: our model of a system that we can change over time.
3
MIT 6.390 Fall 2024 4
Given complete knowledge of our system, we wanted to come up with the best possible
strategy (policy) for getting the most reward, over time.
Our policy chooses different actions, based on what state our state machine gives us.
We evaluate our policies based on the average expected reward, for each state.
Combining these three parts (state machine, reward function, policy), we would find the
best policy, using value functions, and Q-value functions.
Concept 1
Value functions can only be computed if you have complete knowledge of your MDP:
Without this information, it’s not possible to compute the "value" of a policy, using our
previous techniques.
X
VπH s T s, π s , s ′ · VπH−1 s ′
= R s, π s + (11.1)
s′
• But in plenty of real situations, you won’t know exactly what effects your actions
might have.
Since we don’t know what’s inside, we reduce our MDP to a simple input-output machine.
The only way to learn our MDP is to exploring and gathering data.
The only way we can interact with our MDP is by taking actions. So, we do that:
– We get reward r1 .
Repeat.
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Concept 2
In reinforcement learning (RL), we want to learn more about our MDP, so we experi-
ment, by taking different actions.
h i h i
s0 s1 s2 · · · sn r1 r2 · · · rn
Example: You have a panel of buttons. You ask yourself, "what does this one do?", and
press one of them.
• Then, you might ask: what if I press them in a different order? In different situations?
• As you learn more, you gradually figure out a "better" way to play.
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
This is very similar to how you might play a video game when you first pick it up.
Definition 3
Our reinforcement learning (RL) problem can be divide into two main parts:
– The learner chooses which actions to take: they decide the policy.
– Based on what they observe from the environment, they learn to make dif-
ferent choices.
– Eventually, the learner’s goal is to make better choices, to get the most re-
wards.
• The environment: this is the "game" that the player is interacting with.
The learner is trying to learn about the environment, and discover the best policy.
"Learning a better pol-
icy" is what we wanted
The learner chooses the action, and the environment teaches the learner. So, they work in in the MDP chapter:
this time, it just takes
a feedback cycle: more work.
Our learner makes decisions, while the environment gives feedback on those decisions.
This feedback is used in the future to make better decisions.
Notation 4
In this chapter, we use capital R to represent the reward function, and lower-case rt to
represent a single reward at time t.
R st−1 , at = rt
If we expect our environment to behave like an MDP, that’s what we’d put inside the "en-
vironment" block. But, RL isn’t necessarily limited to that framework:
Clarification 5
So far, we’ve used MDPs as a concrete example for RL, but RL can be used for some
other related systems, as well.
Example: One alternative environment is the "partially observable (PO) MDP". This requires more in-
ference than we’ll cover
in this class.
Let’s review:
• Supervised learning: you’re explicitly given an input x(i) , and a desired output y(i)
– Example: This is similar to being given a test, with the answer key.
• Unsupervised learning: you’re given inputs, but you’re not given an output: you
have to look for patterns or structure without outside help.
– Example: You’re given a set of photos, and asked to sort them, based on what
object is in the image. You aren’t given labels.
• Semi-supervised learning: you’re given some answers, but not most of them.
Concept 6
Reinforcement learning (RL) provides data to the model differently from supervised
/ unsupervised frameworks:
• The model has some choice in which data it sees: it chooses action at , which
affects the feedback st and rt .
• This means the model doesn’t just learn by observing the data: it interacts with
it.
Over the course of training, our model can make different choices about what to learn,
based on what it’s already seen.
This approach forces our model to not only learn the structure of the data, but how to ask
questions. Just like how a stu-
dent learns when to
ask questions, and what
they need to practice.
• It has to not only choose the best rewards, but also choose what parts of the environ-
ment to explore.
But even so, it only really makes one decision: choosing actions. It’s still a type of policy.
Concept 7
A reinforcement-learning (RL) algorithm is a type of policy.
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
• However, our RL algorithm first has to explore different parts of the environ-
ment, to know what the rewards and transitions are.
When we’re finished training, it may be possible to just keep the policy π : S → A, and
discard all of our past data. This is similar to how,
when we finish training
our NN, we just give
people the model, not
the training data.
11.1.1 Evaluating RL algorithms
Our trained model con-
tains the information we
How do we evaluate our RL algorithm? There are a couple different ways: need to make decisions:
we don’t need all the
training data.
Concept 8
We can evaluate our learning algorithm based on how long it takes for it to learn a
mostly-optimal policy.
In this scenario, we ignore the rewards we get while learning: our model is allowed to
make mistakes.
Concept 9
We can also evaluate our learning algorithm based on expected rewards while train-
ing.
In this scenario we’re focused on rewards while learning: we don’t want our model
to make as many mistakes.
• We use the first one more often, because it’s often easier to design and measure: we
just train the model first, and keep track of the total time.
• The second one is more challenging: it’s difficult to create a model that can perform
reasonably well, while still learning.
But, sometimes the latter is necessary: you may need to train in real situations, where the
rewards really matter.
Concept 10
If we have a safe, cheap environment to train in, it’s easier to train first, and then figure
out performance later.
But if you’re training in a costly environment, you need to make sure your model
performs well, even while still training.
Example: Suppose that you want to train a car in real traffic environments: simulations
aren’t good enough.
• You really don’t want your car to make major mistakes in real traffic: even if you’re
"training", the accidents are very real.
Reinforcement learning is more restricted. We have limited data: some data points st and
rt .
In a model-based RL algorithm, we use our data to try to guess the MDP model: we
compute an approximation of T and R.
Definition 11
A model-based RL algorithm uses our previous MDP techniques to the find the opti-
mal policy. This approach requires knowledge of our model.
Our other approach is to use a model-free RL algorithm, where skip trying to compute T
and R.
Definition 12
A model-free RL algorithm gives up our previous MDP techniques. Meaning, we
don’t try to directly compute our model (T and R).
• Model-free methods
– Q-learning
– Policy Search
• Model-based methods
• Bandit problems
Definition 13
We can sort model-free methods into two basic types:
These two approaches aren’t necessarily completely separate from one another:
Clarification 14
Often, in more detailed models, the line between value-based and policy-based meth-
ods is blurry.
π∗ s = arg max Q s, a
a
Clarification 15
Value iteration and Q-learning can seem similar, because they both use our model to
compute Q.
• Value iteration is used when you fully understand your model (T and R).
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
How do we do Q-learning? Well first, let’s remind ourselves of how we traditionally com-
pute Q.
X
T s, a, s ′ · max ′ ′
Q s, a = R s, a + γ ′
Q s , a (11.2)
a
s′
• Then, we use the optimal policy starting from state s ′ : that’s why we take the max of
Q.
Our future rewards come from picking the best next action at+1 .
Qdata st−1 , at = rt + γ · max Qold st , at+1 (11.3)
at+1
P
Notice that we don’t average over possible states ( s′ T Q):
• This "expected value" was used because we didn’t know what our next state, s ′ ,
looked like.
Key Equation 16
When deriving our Q-value, we broke our reward into two parts:
Q s, a = immediate reward + future reward
Qdata st−1 , at = rt + γ · max Qold st , at+1
at+1
• We could update our Q-value every time we get new data for that state/action pair.
Our current equation doesn’t allow us to "update" our Q-value, though: it replaces it. We’ll
modify the above equation to get our update rule:
How much do we emphasize our new Q-value, versus our old one? We’ll represent this
with a learning rate α.
Definition 17
When we update our Q-value, our learning rate α (α ∈ (0, 1]) tells us how much we
emphasize our new Q-value, based on one data point.
• (1 − α) tells us how much we emphasize our old Q-value, based on all past data
points.
Qnew st−1 , at = α · Qdata st−1 , at + (1 − α) · Qold st−1 , at
Concept 18
If α is small (α ≈ 0), we care very little about new data.
• We call this a "learning rate", because it tells us how much we learn from new
data.
• But we could also think of it as a "forgetting rate": in order to learn from new
data, we pay less attention to older data.
Notation 19
We update all of our variable names:
• s = st−1 , s ′ = st , a = at , a ′ = at+1 , rt = r
Key Equation 20
In Q-learning, all of our Q-values start as 0 (similar to value iteration):
Qnew s, a = 0
Qnew s, a = (1 − α) · Q s, a + α · Qdata s, a
′ ′
Qnew s, a = (1 − α) · Q s, a + α · r + γ · max
′
Q s , a
a
• But we need a way to actually get our data: we need to start exploring the space.
We could try always exploring whichever action seems most optimal. But this is a bad
strategy when you’re starting out:
Concept 21
It’s not usually a good idea to always use the most "apparently optimal" strategy dur-
ing training.
• There may be plenty of strategies that don’t seem good at first, but will look more
rewarding after some exploration.
• You might take 3 steps, and give up: walking around takes work, and you’re not
immediately rewarded.
• You’ll miss out on that treasure, because you don’t know it’s there yet.
• It’s often more useful to search near high-reward areas: these are more likely to be
searched by (and be useful to) a good policy.
Definition 22
When we’re trying to find the best policy for exploring a space, we run into a problem
called Exploration versus Exploitation.
• Exploration: you’re trying to learn more about the space, and you’re not as fo-
cused on maximizing reward. You explore your options.
• Exploitation: based on what you’ve learned, you want to get the maximum re-
ward from it. You exploit your knowledge.
If you explore more, you might learn how to get better rewards. But if you explore for
too long, you’ll waste time you could’ve spent taking advantage of that knowledge.
• If you only have 10 seconds left in a game, it might not be worth it to explore any-
more: you might as well cash in what you know how to do.
• But if you have 5 hours, you’re more likely to find something useful before the game
ends: maybe you should explore more.
For Q-learning, our simplest option is to randomly alternate between the two modes: ex-
plore with probability ϵ, and exploit with probability (1 − ϵ).
Definition 23
The ε-greedy strategy for Q-learning chooses our actions for interacting with the en-
vironment, randomly:
• With probability (1 − ϵ), we choose the action that gives us the most reward,
based on what we know:
How long do we want to run our Q-learning algorithm? It depends on the situation:
Concept 24
We can choose our termination condition for Q-learning based on our needs.
11.2.4 Q-learning
Based on this, we now have a completed Q-learning algorithm:
Definition 25
Q-learning is a strategy for learning the Q-values of our MDP, so we can find the
optimal policy for our model.
Q s, a = 0
at = select_action(Q, s)
MDP st−1 , at = st , rt
′ ′
Q s, a ⇐= (1 − α) · Q s, a + α · r + γ · max
′
Q s , a
a
select_action isn’t a
specific function: in our
Q-learning is guaranteed to converge under surprisingly simple conditions: case, it could just be ϵ-
greedy. But we could
choose other options.
Theorem 26
Q-learning converges if
With this requirement, we ensure that our model doesn’t decide on a sub-optimal strat-
egy, without checking out other possibilities.
Okay, so an infinite
amount of time isn’t
Guaranteed convergence does require our learning rate α to decay, or gradually shrink exactly promised to us...
but it’s better than a lot
over time. of other stricter conver-
gence requirements!
• But typically, we set α to a constant, for convenience.
We can set it to de-
cay, but this also slows
down the learning pro-
cess.
Now that we have our completed Q-learning strategy, let’s go through some details that
we skipped over.
11.2.5 Initialization
When we’re starting our Q-learning process, we have to choose some initial state, s0 .
• For some problems (like a chess game), there’s a natural choice of initial state.
• For other problems (like a robot moving across terrain), there may be multiple pos-
sible "initial states".
Concept 27
When we’re uncertain what initial state s0 to use, we often randomly sample from our
state space.
This choice of initialization often biases what we learn about the state space: which sec-
tions we visit, what we learn, etc.
So, it’s often helpful to run Q-learning through several initializations: we have one "run"
of our MDP for each initialization.
• So we don’t lose all of our progress, we usually modify it so that our Q-table (com-
puted Q values) is carried over between different "runs".
Concept 28
To explore our state-action space (possible options) more thoroughly, we may take
several different paths through our MDP.
We share our Q-values between these "runs" of our MDP, so that we can build up a
more complete representation of the environment.
• But this might not always be a realistic assumption. We might need a continuous
space.
Concept 29
Our above approach to Q-learning is called tabular Q-learning.
It assumes that we have a discrete (typically finite) state space and action space.
• Other versions of Q-learning, on the other hand, allow this space it be continu-
ous.
We call it "tabular Q-
learning" because our
Example: A discrete state space might be 1, 2, 3, 4, 5, 6 . A continuous one might be 1, 6 . values could be stored
in a table.
√
• 1+ 2 is allowed in the latter, but not the former.
• It’s impossible to get the Q-values for all of these state-action pairs.
Many Q-learning variations enable continuous action/state spaces. Later, we’ll focus on
one example: Deep Q-learning.
Qnew s, a = Q s, a + α Qdata s, a − Q s, a (11.5)
The right term could be seen as the disagreement between our new data, and past experi-
ence.
• And thus, α tells us how much we care about that disagreement, and want to account
for it.
This is an update rule: the difference between our new and old answer decides how we
want to update.
Concept 30
We can view Q-learning as a direct update rule:
• We "update" our current Q value based on the difference from what the newest
data point predicts.
• Example: We’ll re-use our example of going down a long hallway, with treasure at
the end.
• Each "state" is one tile of the hallway. We’ll arrange them left-to-right: we can move
left or right down the hallway. We start on s0 .
Above the arrows, we can see the reward we get for going left/right in each state.
If we go left, we get a small reward. If we go right, we’ll eventually get a huge reward. Assume that every state
transition we don’t
Being able to see from above, it’s obvious to us that going right is better. But what does the show (left/right) is +0.
robot see?
So long as γ isn’t really
small: if our model is
• Go right once. No reward. really likely to fail after
1 or 2 steps, then the
• Go left once. Reward! right reward isn’t worth
it.
• We should go left!
Concept 31
At first, our Q-learning algorithm will prioritize short-term rewards over long-term
rewards.
Well, as the robot explores, it’ll learn to get the reward, right? Let’s see what happens as
we move right, to our Q values.
′ ′
Q s, a ⇐= (1 − α) · Q s, a + α · r + 0.9 · max
′
Q s , a (11.7)
a
Q s ′, a ′
Q s, a ⇐= r + 0.9 · max
′
(11.8)
a
Based on this model, our reward for going left (←) is simple:
Q s0 , ← = +1 (11.9)
We did it! We learned something, at the very end. Will our robot go the way we want?
• We start over from s0 . Let’s compare the left and right rewards.
Q s0 , ← = +1 Q s0 , → =0 (11.10)
Concept 32
Even once we find a reward, Q-learning will only update that single state-action pair.
• That means that nearby states, don’t know about that reward!
We have to run Q-learning through a nearby state again to find the reward.
Q d, a ′
Q c, → ⇐= r + 0.9 · max
′ a
+1000
z }| {
Q c, → ⇐= 0 + 0.9 · Q d, → = +900
We know that d is valuable. Thus, we’ve learned that c is valuable, because it’s attached to
d.
Concept 33
Each time that we run though a path to a reward, one more state learns about the
reward.
We can now see that b has a lot of value. (The bottom number is the "expected value" we
can get after reaching state s, if we make the best choice.)
Q s0 , ← = +1 Q s0 , → = +729 (11.11)
Finally, we go right!
This is already annoying, but it can get even worse: suppose we only reached the reward
after moving right 10 times.
It’s still worth it to go right, but it takes a painfully long time to figure that out.
Instead of making 4 trips right, we’ll have to make 10 trips right. Each trip is, thankfully,
shorter than the last.
But that’s still really
• And imagine if the "reward" for going right was -1 instead of +0.
slow.
• Our model would always prefer to go left, until the very end. Which means, it only
has an ϵ/2 chance of moving right. ϵ chance to move in a
random direction, and
• Going right n times in a row has a chance of (ϵ/2)n . 1/2 chance to randomly
move right.
If moving left from (b, c, d...) is still +0, our model will try to avoid going right. It’s even
harder to make progress, now.
Concept 34
The longer it takes to reach a distant reward, the more difficult it is to propagate that
information back to s0 .
• This shows how inefficient Q-learning can be: only updating one state-action pair
at a time, means that information travels slowly between states.
Definition 35
In Deep Q-Learning, we use deep neural networks to predict Q-values: a regression
problem.
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
To teach this network, we train it the way that we train any neural network, using data
we receive while exploring:
Our goal is to make the most accurate predictions of the Q-value. We determine Q based
on each data point,
Qdata st−1 , at = rt + γ · max Q st , at+1 (11.12)
at+1
Definition 36
Our deep Q-learning neural network will use squared error:
2
QNN (s, a) − Qdata (s, a)
In other words, our goal is for our NN (QNN ) to match the Q-values of our data points
(Qdata ), as close as possible.
!2
QNN (s, a) − r + γ · max QNN s ′ , a ′
at+1
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Note that, in our definition, we said states and/or actions: we might not have both as the
input to our neural network. How?
Concept 37
There are three main ways we can design our neural network: in all cases, Q(s, a) is
the output.
• We have one neural network shared by all inputs. State s and action a are con-
catenated into the input.
– This one is the most flexible, but it’s very hard to find arg maxa Q(s, a).
In one system, each state si has its own neural net. In the other system, each action aj has
its own neural net.
This version works for continuous state/action spaces, but comes with its own difficulties.
• When training a typical neural network, all of our data is IID: independent, and
coming from the same distribution.
Concept 38
In Q-learning, our data are correlated in time ("temporally correlated"). Meaning,
timing affects our data.
• Why? Because two states which are "near" each other, typically behave similarly.
• If the time between two data points is short, they’re probably nearby in state
space. So, they’re more likely to be similar.
• Example 1: If, at time t, our robot is on a mountain, it’s more likely to be on a moun-
tain at (t − 1) and (t + 1).
• Example 2: The 12 hours of daytime may seem very different from the 12 hours of
nighttime.
Why is this a problem? Because our neural network adjusts Q based on new data.
That means that our NN is capable of forgetting: if there’s been a long time since we’ve
used some information, it will be replaced by information from a different context.
Definition 39
Catastrophic forgetting occurs when our neural network hasn’t seen a certain type of
data in a long time, and forgets how to do a task.
• In deep Q-learning, it can occur when recent data doesn’t reflect past data.
• So, our model forgets about the portion of the state space it visited in the past.
When we return to the red region, we’ve forgotten what we learned the first time!
This is still a problem, even if we don’t return to the red region during this MDP run:
• Perhaps they talk about that memory, or you periodically mention it to them.
This is our solution: we keep track of these past experiences, and re-use them later: we
essentially "refresh" our NN, so that it doesn’t forget.
Definition 40
Experience replay is a technique for addressing catastrophic forgetting.
• We randomly pull n memories from our replay buffer, and "re-experience" them:
we re-train our model, based on these past events.
This storage can get painfully large, though. This can be problematic:
• If the memories are too far back, they may just not be relevant anymore: they’re in
an undesirable part of the state space.
Definition 41
Rather than storing every event in our replay buffer, we only keep the k most recent
memories, in a sliding window.
• This prevents our memory from getting too full, or focusing on memories that
are too old.
The best size for our sliding buffer depends on the problem, and what our state space
is like.
Another reason that we like experience replay is for improving on a weakness we men-
tioned before:
Concept 42
Randomly reviewing old memories has a second benefit: it allows us to propagate
rewards between states faster.
• This means that, when we get our reward, we only update the state sr we got
the reward in.
With experience replay, we’re more likely to revisit a state sn "near" our reward:
Our "forgetting" problem is caused by the fact that our data comes in a particular order:
older data is learned earlier, and risks being forgotten, all together.
Concept 43
We want to gather data before training Q (so we can shuffle it).
The solution? We have two Q functions: one we use to gather new data (but not train),
another we train afterwards.
• Qold : trained on all previous data. We use this function to decide our actions, and
gather more data. If we have no data yet,
Qold is just the "de-
fault" Q-value function:
– We do not re-train Qold as we receive new data: we want to avoid training our
Qold (s, a) = 0.
data in order.
• Qnew : once we’ve gathered enough data, we use all of our data (old and new) to
train a new Q function from scratch. Meaning, we start with
Qnew (s, a) = 0, and
then train.
– We shuffle our data, so that Qnew also avoids training our data in order.
Qnew can, then, be used to decide our future actions: it replaces Qold .
Concept 44
In typical Q-learning, we take an action, get data, and immediately update Q with that
new data.
• This means our Q-value function is trained in the order we receive the data.
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
In fitted Q-learning, we separate the data-gathering process (Qold ) from the training
process (Qnew ).
• We use the same Q-value function, Qold , to gather data for a while: we don’t
re-train Qold with our new data.
• Then, using all of our data shuffled (new and old), we train a new Q-value func-
tion, Qnew .
• Repeat.
• Repeat.
Using pseudocode:
Definition 45
Fitted Q-learning uses the following procedure:
x(i) = s, a
(11.14)
• Output y(i) : expected reward (based on reward r, and new state s ′ ) In other words, the Q-
value, based on this
data point.
y(i) = r + γ · max Q s ′, a ′
′
(11.15)
a
Our strategy is to represent our policy as a function with parameters we can optimize.
Definition 46
In policy search, we represent our policy π as a differentiable function f, with parame-
ters θ.
π s = f s; θ = a
Using this approach, we can optimize our parameters θ, to get the greatest average
reward.
• We need our function to be differentiable, for the same reasons as we needed for
gradient descent.
One possible problem: often, we have a discrete action space. Our output will be a cate-
gory: not a continuous variable.
Concept 47
Rather than outputting the chosen action, a, we output the probability of that action.
• Because we chose our action based on our state, it’s a conditional probability:
f s, a; θ = P a s = Prob of choosing action a, given state s
Key Equation 48
If θ is low-dimensional, we can use numerical gradient descent/ascent to train our
policy:
∂R ∆R R(θi + ε) − R(θi )
≈ =
∂θi ∆θi ε
∂R/∂θ1
∂R/∂θ2
∇θ R =
..
.
∂R/∂θn
θ ⇐ θ + η · ∇θ R
• Instead, we use other, more complex algorithms, like REINFORCE. But these algorithms are
often tricky.
Policy search works best in those lower-dimensional cases.
Concept 49
Policy search works best when
11.3 Model-based RL
Rather than try to directly compute π or Q, we could also re-use our previous techniques:
we just need to compute T and R.
Notation 50
We want to approximate T and R.
11.3.1 Computing Tb
To compute Tb, let’s remind ourselves: what does T represent?
Definition 51
Review from MDP Chapter, pt. 1:
• Entering state s ′ ,
T (s, a, s ′ ) = P St = s ′ St−1 = s, At = a
Key Equation 52
We can approximate the probability of event E happening, by counting the number of
times it does/doesn’t occur:
or,
#E
P E =
#Total events
#E = #(s, a, s ′ ) (11.17)
So, we get:
• If we have one data point, and didn’t get E, what is our probability? 0/1 = 0.
– If we have only one data point, why should we be so sure that E never happens?
Concept 53
The equation
#E
P E =
#Total events
• If we have no events E, it says that there’s a 0% chance that E will appear. Our
model shouldn’t be so confident.
Example: Imagine you flipped a coin 3 times, and happened to get heads 3 times. This will
happen 1/8 of the time, on a fair coin.
• But our model has decided that there’s a 0% chance of ever getting tails.
• We don’t want to give a 0% chance of E, when we don’t have enough data. We’ll add
something to the top.
#(s, a, s ′ ) + b
Tb(s, a, s ′ ) = (11.19)
#(s, a) + c
How do we decide these constants? Well, let’s return to the situation where we have no
data.
b
Tb(s, a, s ′ ) = (11.20)
c
We want b/c to be our "default" assumption: what do we think are the odds of transition-
ing to state s ′ , without any data?
• Without any data, we have no reason to prefer one state over another. So, we assume
all states to be equally likely.
1
Tb(s, a, s ′ ) = (11.21)
|S|
Definition 54
The laplace correction is an adjustment to our probability equation, that solves the
problems of
• Dividing by 0
The solution is to set a default probability for an event: we split probability evenly
between all of our N possible outcomes.
1
P E No data =
N
1 + #E
P E ≈
N + #Total
Example: Let’s say we have 5 possible outcomes, and the odds of our event E are 40%
(0.4). And let’s say our data
exactly matches our
probability, just to make
• We’ll compare the prediction for 5,50, and 500 data points.
things easier.
1+2 1 + 20 1 + 200
= 0.3 ≈ 0.381 ≈ 0.398
5+5 5 + 50 5 + 500
• As we get more data, the laplace correction becomes less and less important.
Key Equation 55
Our approximation for the transition function T is given by the equation
#(s, a, s ′ ) + 1
Tb(s, a, s ′ ) =
#(s, a) + |S|
11.3.3 Computing R
b
Our reward function R, on the other hand, is much simpler to "approximate", because it’s
deterministic:
• The same state-action pair (s, a) will always give the same reward.
• So, we don’t have to approximate our reward: if we get our reward once, we know
exactly what it’ll be.
Key Equation 56
Our "approximation" for the reward function R comes directly from our observations:
R(s,
b a) = rt if st−1 = s, at = a
R(s,
b a) = rt = R(s, a)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
• In which case, we can compute the reward probability function, or the expected re-
ward for our state-action pair.
MDP S, A, Tb, R,
b γ (11.22)
And we can just solve it like any other MDP, using a technique like value iteration.
Definition 57
Our model-based RL algorithm has three basic parts:
• Computing our model S, A, Tb, R,
b γ .
#(s, a, s ′ ) + 1
Tb(s, a, s ′ ) =
#(s, a) + |S|
R(s,
b a) = rt if st−1 = s, at = a
X
Tb s, a, s ′ · max ′ ′
Q s, a = R
b s, a + γ
′
Q s , a
a
s′
π∗ s = arg max Q s, a
a
The approach requires us to approximate T and R for every possible combinations of input
variables.
Concept 58
Model-based RL algorithms work best when we have a small, discrete state space S.
– And each lever has different odds of giving you a particular reward.
We can think of this problem like a "slot machine" with k levers: each one has different
odds of giving you a reward. Slot machines have, in
the past, been called
"one-armed bandits",
Concept 59 because they take your
money. This is why we
In a bandit problem, you have a set of k independent actions you can choose from. call these "bandit prob-
lems".
• Each action will give you a randomized reward.
You goal is to maximize the total rewards you get (while training!)
Again, note that states have been completely removed from the problem.
• What are the possible rewards we can get? That’s our set R.
a∈A r∈R
Finally, we need the probability of getting a reward, if we take an action. This is similar to
the transition function T :
• Rather than returning our next state s ′ , it instead gives us the odds of ending up in
state s ′ . If we take action a in
state s.
In the same spirit, we’ll have a function Rp , which gives us the probability of getting
reward r, from action a.
Rp (a, r) = P r a (11.24)
Based on our inputs and outputs, we can write this with function notation: Rp : (A × R) → R, where
R are real numbers, is
h i also acceptable.
Rp : A × R → 0, 1 (11.25)
Definition 60
A bandit problem has three parts:
• A set of actions A
• A set of rewards R
• A reward-probability function
h i
Rp : A × R → 0, 1
Bandit problems are very, very important to reinforcement learning, and computer science.
So, each action varies only by the chance that you get a reward. In other words, we
don’t have "different
types" of rewards.
Definition 61
The k-armed bandit problem is a simplified bandit problem, where
If you want to maximize your rewards, while still learning, you can’t just explore, and you
can’t "exploit" blindly without data.
There’s lots of interesting details we’ll skip here, but the basic idea is:
Concept 62
The longer your horizon h (or the larger γ is), the longer you should continue to ex-
plore.
• The same amount of exploring takes up smaller fraction of your time: so, it takes
away less of the exploitation reward.
You spend n turns "exploring", and then h − n turns "exploiting". You get 0 reward for
exploring. This example uses a lot
of huge simplifications.
Let’s say these as "aver-
• You get $10 for exploiting if you explored a little (n = 3)
age" benefits for explor-
ing/exploiting.
• You get $15 for exploiting if you explored a lot (n = 10)
If you have only a little time (h = 15), it’s not really worth it to explore more.
• If you get good luck, you’ll probably keep trying that (seemingly profitable)
lever: you’ll get lots of data to learn that it isn’t as good as you thought.
Definition 64
In a contextual bandit problem, we re-introduce states S.
11.5 Terms
• MDP (Review)
• Reinforcement Learning
• Learner
• Environment
• Model-based RL
• Model-free RL
• Q-learning
• Learning rate α
• ϵ-greedy strategy
• Tabular Q-learning
• Deep Q-learning
• Temporally Correlated
• Catastrophic forgetting
• Experience Replay
• Replay Buffer
• Sliding Window
• Fitted Q-learning
• Policy search
• Tb
• R
b
• Laplace Correction
• Bandit Problem
• Reward-probability function Rp