0% found this document useful (0 votes)
148 views51 pages

Lecture 4: Model-Free Prediction: David Silver

This document summarizes Lecture 4 on model-free prediction in reinforcement learning. [1] It introduces Monte Carlo (MC) learning, which directly learns value functions from complete episodes without a model of the environment. MC uses the average return to estimate values. [2] It then describes Temporal Difference (TD) learning, which can learn online by bootstrapping - updating values based on other learned values. TD has lower variance than MC. [3] Examples are provided to illustrate the differences between MC and TD learning, including for policy evaluation in Blackjack and estimating travel times in the Driving Home example.

Uploaded by

Rajath av
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
148 views51 pages

Lecture 4: Model-Free Prediction: David Silver

This document summarizes Lecture 4 on model-free prediction in reinforcement learning. [1] It introduces Monte Carlo (MC) learning, which directly learns value functions from complete episodes without a model of the environment. MC uses the average return to estimate values. [2] It then describes Temporal Difference (TD) learning, which can learn online by bootstrapping - updating values based on other learned values. TD has lower variance than MC. [3] Examples are provided to illustrate the differences between MC and TD learning, including for policy evaluation in Blackjack and estimating travel times in the Driving Home example.

Uploaded by

Rajath av
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Lecture 4: Model-Free Prediction

Lecture 4: Model-Free Prediction

David Silver
Lecture 4: Model-Free Prediction

Outline

1 Introduction

2 Monte-Carlo Learning

3 Temporal-Difference Learning

4 TD(λ)
Lecture 4: Model-Free Prediction
Introduction

Model-Free Reinforcement Learning

Last lecture:
Planning by dynamic programming
Solve a known MDP
This lecture:
Model-free prediction
Estimate the value function of an unknown MDP
Next lecture:
Model-free control
Optimise the value function of an unknown MDP
Lecture 4: Model-Free Prediction
Monte-Carlo Learning

Monte-Carlo Reinforcement Learning

MC methods learn directly from episodes of experience


MC is model-free: no knowledge of MDP transitions / rewards
MC learns from complete episodes: no bootstrapping
MC uses the simplest possible idea: value = mean return
Caveat: can only apply MC to episodic MDPs
All episodes must terminate
Lecture 4: Model-Free Prediction
Monte-Carlo Learning

Monte-Carlo Policy Evaluation

Goal: learn vπ from episodes of experience under policy π

S1 , A1 , R2 , ..., Sk ∼ π

Recall that the return is the total discounted reward:

Gt = Rt+1 + γRt+2 + ... + γ T −1 RT

Recall that the value function is the expected return:

vπ (s) = Eπ [Gt | St = s]

Monte-Carlo policy evaluation uses empirical mean return


instead of expected return
Lecture 4: Model-Free Prediction
Monte-Carlo Learning

First-Visit Monte-Carlo Policy Evaluation

To evaluate state s
The first time-step t that state s is visited in an episode,
Increment counter N(s) ← N(s) + 1
Increment total return S(s) ← S(s) + Gt
Value is estimated by mean return V (s) = S(s)/N(s)
By law of large numbers, V (s) → vπ (s) as N(s) → ∞
Lecture 4: Model-Free Prediction
Monte-Carlo Learning

Every-Visit Monte-Carlo Policy Evaluation

To evaluate state s
Every time-step t that state s is visited in an episode,
Increment counter N(s) ← N(s) + 1
Increment total return S(s) ← S(s) + Gt
Value is estimated by mean return V (s) = S(s)/N(s)
Again, V (s) → vπ (s) as N(s) → ∞
Lecture 4: Model-Free Prediction
Monte-Carlo Learning
Blackjack Example

Blackjack Example

States (200 of them):


Current sum (12-21)
Dealer’s showing card (ace-10)
Do I have a “useable” ace? (yes-no)
Action stick: Stop receiving cards (and terminate)
Action twist: Take another card (no replacement)
Reward for stick:
+1 if sum of cards > sum of dealer cards
0 if sum of cards = sum of dealer cards
-1 if sum of cards < sum of dealer cards
Reward for twist:
-1 if sum of cards > 21 (and terminate)
0 otherwise
Transitions: automatically twist if sum of cards < 12
Lecture 4: Model-Free Prediction
Monte-Carlo Learning
Blackjack Example

Blackjack Value Function after Monte-Carlo Learning

Policy: stick if sum of cards ≥ 20, otherwise twist


Lecture 4: Model-Free Prediction
Monte-Carlo Learning
Incremental Monte-Carlo

Incremental Mean

The mean µ1 , µ2 , ... of a sequence x1 , x2 , ... can be computed


incrementally,
k
1X
µk = xj
k
j=1
 
k−1
1 X
= xk + xj 
k
j=1
1
= (xk + (k − 1)µk−1 )
k
1
= µk−1 + (xk − µk−1 )
k
Lecture 4: Model-Free Prediction
Monte-Carlo Learning
Incremental Monte-Carlo

Incremental Monte-Carlo Updates

Update V (s) incrementally after episode S1 , A1 , R2 , ..., ST


For each state St with return Gt

N(St ) ← N(St ) + 1
1
V (St ) ← V (St ) + (Gt − V (St ))
N(St )

In non-stationary problems, it can be useful to track a running


mean, i.e. forget old episodes.

V (St ) ← V (St ) + α (Gt − V (St ))


Lecture 4: Model-Free Prediction
Temporal-Difference Learning

Temporal-Difference Learning

TD methods learn directly from episodes of experience


TD is model-free: no knowledge of MDP transitions / rewards
TD learns from incomplete episodes, by bootstrapping
TD updates a guess towards a guess
Lecture 4: Model-Free Prediction
Temporal-Difference Learning

MC and TD

Goal: learn vπ online from experience under policy π


Incremental every-visit Monte-Carlo
Update value V (St ) toward actual return Gt

V (St ) ← V (St ) + α (Gt − V (St ))

Simplest temporal-difference learning algorithm: TD(0)


Update value V (St ) toward estimated return Rt+1 + γV (St+1 )

V (St ) ← V (St ) + α (Rt+1 + γV (St+1 ) − V (St ))

Rt+1 + γV (St+1 ) is called the TD target


δt = Rt+1 + γV (St+1 ) − V (St ) is called the TD error
Lecture 4: Model-Free Prediction
Temporal-Difference Learning
Driving Home Example

Driving Home Example

State Elapsed Time Predicted Predicted


(minutes) Time to Go Total Time
leaving office 0 30 30

reach car, raining 5 35 40

exit highway 20 15 35

behind truck 30 10 40

home street 40 3 43

arrive home 43 0 43
Lecture 4: Model-Free Prediction
Temporal-Difference Learning
Driving Home Example

Driving Home Example: MC vs. TD

Changes recommended by Changes recommended!


Monte Carlo methods (!=1)! by TD methods (!=1)!
Lecture 4: Model-Free Prediction
Temporal-Difference Learning
Driving Home Example

Advantages and Disadvantages of MC vs. TD

TD can learn before knowing the final outcome


TD can learn online after every step
MC must wait until end of episode before return is known
TD can learn without the final outcome
TD can learn from incomplete sequences
MC can only learn from complete sequences
TD works in continuing (non-terminating) environments
MC only works for episodic (terminating) environments
Lecture 4: Model-Free Prediction
Temporal-Difference Learning
Driving Home Example

Bias/Variance Trade-Off

Return Gt = Rt+1 + γRt+2 + ... + γ T −1 RT is unbiased


estimate of vπ (St )
True TD target Rt+1 + γvπ (St+1 ) is unbiased estimate of
vπ (St )
TD target Rt+1 + γV (St+1 ) is biased estimate of vπ (St )
TD target is much lower variance than the return:
Return depends on many random actions, transitions, rewards
TD target depends on one random action, transition, reward
Lecture 4: Model-Free Prediction
Temporal-Difference Learning
Driving Home Example

Advantages and Disadvantages of MC vs. TD (2)

MC has high variance, zero bias


Good convergence properties
(even with function approximation)
Not very sensitive to initial value
Very simple to understand and use
TD has low variance, some bias
Usually more efficient than MC
TD(0) converges to vπ (s)
(but not always with function approximation)
More sensitive to initial value
Lecture 4: Model-Free Prediction
Temporal-Difference Learning
Random Walk Example

Random Walk Example


Lecture 4: Model-Free Prediction
Temporal-Difference Learning
Random Walk Example

Random Walk: MC vs. TD


Lecture 4: Model-Free Prediction
Temporal-Difference Learning
Batch MC and TD

Batch MC and TD

MC and TD converge: V (s) → vπ (s) as experience → ∞


But what about batch solution for finite experience?

s11 , a11 , r21 , ..., sT1 1


..
.
s1K , a1K , r2K , ..., sTKK

e.g. Repeatedly sample episode k ∈ [1, K ]


Apply MC or TD(0) to episode k
Lecture 4: Model-Free Prediction
Temporal-Difference Learning
Batch MC and TD

AB Example

Two states A, B; no discounting; 8 episodes of experience


A, 0, B, 0!
B, 1!
B, 1!
B, 1!
B, 1!
B, 1!
B, 1!
B, 0!
What is V (A), V (B)?
Lecture 4: Model-Free Prediction
Temporal-Difference Learning
Batch MC and TD

AB Example

Two states A, B; no discounting; 8 episodes of experience


A, 0, B, 0!
B, 1!
B, 1!
B, 1!
B, 1!
B, 1!
B, 1!
B, 0!
What is V (A), V (B)?
Lecture 4: Model-Free Prediction
Temporal-Difference Learning
Batch MC and TD

Certainty Equivalence
MC converges to solution with minimum mean-squared error
Best fit to the observed returns
K XTk
X 2
Gtk − V (stk )
k=1 t=1

In the AB example, V (A) = 0


TD(0) converges to solution of max likelihood Markov model
Solution to the MDP hS, A, P̂, R̂, γi that best fits the data
Kk T
1 XX
a
P̂s,s 0 = 1(stk , atk , st+1
k
= s, a, s 0 )
N(s, a) t=1
k=1
Kk T
1 XX
R̂as = 1(stk , atk = s, a)rtk
N(s, a) t=1
k=1

In the AB example, V (A) = 0.75


Lecture 4: Model-Free Prediction
Temporal-Difference Learning
Batch MC and TD

Advantages and Disadvantages of MC vs. TD (3)

TD exploits Markov property


Usually more efficient in Markov environments
MC does not exploit Markov property
Usually more effective in non-Markov environments
Lecture 4: Model-Free Prediction
Temporal-Difference Learning
Unified View

Monte-Carlo Backup

V (St ) ← V (St ) + α (Gt − V (St ))

st

T!
T T! TT! T! T!
! ! !

TT! T TT! T! TT!


! ! !
Lecture 4: Model-Free Prediction
Temporal-Difference Learning
Unified View

Temporal-Difference Backup

V (St ) ← V (St ) + α (Rt+1 + γV (St+1 ) − V (St ))

st

rt +1
st +1

T! TT! TT! T! T!
! !

T! T
T! TT!! T! TT!
!
Lecture 4: Model-Free Prediction
Temporal-Difference Learning
Unified View

Dynamic Programming Backup

V (St ) ← Eπ [Rt+1 + γV (St+1 )]

st

rt +1
st +1

T! TT!! T! T! T!

TT! T! T! T! T!
Lecture 4: Model-Free Prediction
Temporal-Difference Learning
Unified View

Bootstrapping and Sampling

Bootstrapping: update involves an estimate


MC does not bootstrap
DP bootstraps
TD bootstraps
Sampling: update samples an expectation
MC samples
DP does not sample
TD samples
Lecture 4: Model-Free Prediction
Temporal-Difference Learning
Unified View

Unified View of Reinforcement Learning


Lecture 4: Model-Free Prediction
TD(λ)
n-Step TD

n-Step Prediction

Let TD target look n steps into the future


Lecture 4: Model-Free Prediction
TD(λ)
n-Step TD

n-Step Return

Consider the following n-step returns for n = 1, 2, ∞:


(1)
n=1 (TD) Gt = Rt+1 + γV (St+1 )
(2)
n=2 Gt = Rt+1 + γRt+2 + γ 2 V (St+2 )
.. ..
. .
(∞)
n = ∞ (MC ) Gt = Rt+1 + γRt+2 + ... + γ T −1 RT

Define the n-step return


(n)
Gt = Rt+1 + γRt+2 + ... + γ n−1 Rt+n + γ n V (St+n )

n-step temporal-difference learning


 
(n)
V (St ) ← V (St ) + α Gt − V (St )
Lecture 4: Model-Free Prediction
TD(λ)
n-Step TD

Large Random Walk Example


Lecture 4: Model-Free Prediction
TD(λ)
n-Step TD

Averaging n-Step Returns


One backup

We can average n-step returns over different n


e.g. average the 2-step and 4-step returns
1 (2) 1 (4)
G + G
2 2
Combines information from two different
time-steps
Can we efficiently combine information from all
time-steps?
Lecture 4: Model-Free Prediction
TD(λ)
Forward View of TD(λ)

λ-return

The λ-return Gtλ combines


(n)
all n-step returns Gt
Using weight (1 − λ)λn−1

(n)
X
Gtλ = (1 − λ) λn−1 Gt
n=1

Forward-view TD(λ)
 
V (St ) ← V (St ) + α Gtλ − V (St )
Lecture 4: Model-Free Prediction
TD(λ)
Forward View of TD(λ)

TD(λ) Weighting Function


(n)
X
Gtλ = (1 − λ) λn−1 Gt
n=1
Lecture 4: Model-Free Prediction
TD(λ)
Forward View of TD(λ)

Forward-view TD(λ)

Update value function towards the λ-return


Forward-view looks into the future to compute Gtλ
Like MC, can only be computed from complete episodes
Lecture 4: Model-Free Prediction
TD(λ)
Forward View of TD(λ)

Forward-View TD(λ) on Large Random Walk


Lecture 4: Model-Free Prediction
TD(λ)
Backward View of TD(λ)

Backward View TD(λ)

Forward view provides theory


Backward view provides mechanism
Update online, every step, from incomplete sequences
Lecture 4: Model-Free Prediction
TD(λ)
Backward View of TD(λ)

Eligibility Traces

Credit assignment problem: did bell or light cause shock?


Frequency heuristic: assign credit to most frequent states
Recency heuristic: assign credit to most recent states
Eligibility traces combine both heuristics
E0 (s) = 0
Et (s) = γλEt−1 (s) + 1(St = s)
Lecture 4: Model-Free Prediction
TD(λ)
Backward View of TD(λ)

Backward View TD(λ)

Keep an eligibility trace for every state s


Update value V (s) for every state s
In proportion to TD-error δt and eligibility trace Et (s)
δt = Rt+1 + γV (St+1 ) − V (St )
V (s) ← V (s) + αδt Et (s)
Lecture 4: Model-Free Prediction
TD(λ)
Relationship Between Forward and Backward TD

TD(λ) and TD(0)

When λ = 0, only current state is updated

Et (s) = 1(St = s)
V (s) ← V (s) + αδt Et (s)

This is exactly equivalent to TD(0) update

V (St ) ← V (St ) + αδt


Lecture 4: Model-Free Prediction
TD(λ)
Relationship Between Forward and Backward TD

TD(λ) and MC

When λ = 1, credit is deferred until end of episode


Consider episodic environments with offline updates
Over the course of an episode, total update for TD(1) is the
same as total update for MC
Theorem
The sum of offline updates is identical for forward-view and
backward-view TD(λ)
T
X T
X  
αδt Et (s) = α Gtλ − V (St ) 1(St = s)
t=1 t=1
Lecture 4: Model-Free Prediction
TD(λ)
Forward and Backward Equivalence

MC and TD(1)
Consider an episode where s is visited once at time-step k,
TD(1) eligibility trace discounts time since visit,

Et (s) = γEt−1 (s) + 1(St = s)



0 if t < k
=
γ t−k if t ≥ k

TD(1) updates accumulate error online


T
X −1 T
X −1
αδt Et (s) = α γ t−k δt = α (Gk − V (Sk ))
t=1 t=k

By end of episode it accumulates total error

δk + γδk+1 + γ 2 δk+2 + ... + γ T −1−k δT −1


Lecture 4: Model-Free Prediction
TD(λ)
Forward and Backward Equivalence

Telescoping in TD(1)

When λ = 1, sum of TD errors telescopes into MC error,

δt + γδt+1 + γ 2 δt+2 + ... + γ T −1−t δT −1


= Rt+1 + γV (St+1 ) − V (St )
+ γRt+2 + γ 2 V (St+2 ) − γV (St+1 )
+ γ 2 Rt+3 + γ 3 V (St+3 ) − γ 2 V (St+2 )
..
.
+ γ T −1−t RT + γ T −t V (ST ) − γ T −1−t V (ST −1 )
= Rt+1 + γRt+2 + γ 2 Rt+3 ... + γ T −1−t RT − V (St )
= Gt − V (St )
Lecture 4: Model-Free Prediction
TD(λ)
Forward and Backward Equivalence

TD(λ) and TD(1)

TD(1) is roughly equivalent to every-visit Monte-Carlo


Error is accumulated online, step-by-step
If value function is only updated offline at end of episode
Then total update is exactly the same as MC
Lecture 4: Model-Free Prediction
TD(λ)
Forward and Backward Equivalence

Telescoping in TD(λ)

For general λ, TD errors also telescope to λ-error, Gtλ − V (St )

Gtλ − V (St ) = −V (St ) + (1 − λ)λ0 (Rt+1 + γV (St+1 )) 


+ (1 − λ)λ1 Rt+1 + γRt+2 + γ 2 V (St+2 ) 
+ (1 − λ)λ2 Rt+1 + γRt+2 + γ 2 Rt+3 + γ 3 V (St+3 )
+ ...
= −V (St ) + (γλ)0 (Rt+1 + γV (St+1 ) − γλV (St+1 ))
+ (γλ)1 (Rt+2 + γV (St+2 ) − γλV (St+2 ))
+ (γλ)2 (Rt+3 + γV (St+3 ) − γλV (St+3 ))
+ ...
= (γλ)0 (Rt+1 + γV (St+1 ) − V (St ))
+ (γλ)1 (Rt+2 + γV (St+2 ) − V (St+1 ))
+ (γλ)2 (Rt+3 + γV (St+3 ) − V (St+2 ))
+ ...
= δt + γλδt+1 + (γλ)2 δt+2 + ...
Lecture 4: Model-Free Prediction
TD(λ)
Forward and Backward Equivalence

Forwards and Backwards TD(λ)

Consider an episode where s is visited once at time-step k,


TD(λ) eligibility trace discounts time since visit,

Et (s) = γλEt−1 (s) + 1(St = s)



0 if t < k
=
(γλ)t−k if t ≥ k

Backward TD(λ) updates accumulate error online


T
X T
X  
αδt Et (s) = α (γλ)t−k δt = α Gkλ − V (Sk )
t=1 t=k

By end of episode it accumulates total error for λ-return


For multiple visits to s, Et (s) accumulates many errors
Lecture 4: Model-Free Prediction
TD(λ)
Forward and Backward Equivalence

Offline Equivalence of Forward and Backward TD

Offline updates
Updates are accumulated within episode
but applied in batch at the end of episode
Lecture 4: Model-Free Prediction
TD(λ)
Forward and Backward Equivalence

Onine Equivalence of Forward and Backward TD

Online updates
TD(λ) updates are applied online at each step within episode
Forward and backward-view TD(λ) are slightly different
NEW: Exact online TD(λ) achieves perfect equivalence
By using a slightly different form of eligibility trace
Sutton and von Seijen, ICML 2014
Lecture 4: Model-Free Prediction
TD(λ)
Forward and Backward Equivalence

Summary of Forward and Backward TD(λ)

Offline updates λ=0 λ ∈ (0, 1) λ=1


Backward view TD(0) TD(λ) TD(1)

=
Forward view TD(0) Forward TD(λ) MC
Online updates λ=0 λ ∈ (0, 1) λ=1
Backward view TD(0) TD(λ) TD(1)
=

6=

6=
Forward view TD(0) Forward TD(λ) MC
=

=
Exact Online TD(0) Exact Online TD(λ) Exact Online TD(1)

= here indicates equivalence in total update at end of episode.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy