Reinforcement Learning - TD


RL_4TD

Model-Free Incremental RL Algorithms(TD 是个算法大类)

在 Model-Free 的情况下,用 Experience 求解 Bellman Expectation Equation

所以 TD 的思想与 Stochastic Gradient Descent (SGD) 非常相近,算是某种 Stochastic Approximation Algorithm

另外,思想相近的算法还有 Newton's Method

 

 

 

1. TD(0)

TD Learning of State Value,是最最最基本的用于 Policy Evaluation 的 TD 算法

  • 只能估计 State Value of a Given Policy

  • 不能估计 Action Value

  • 不能搜索 Optimal Policy

 

 

1.1 Problem Formulation

  • Given

    1. Policy π

    2. Experience Samples generated by π in the form

      {(st,rt+1,st+1)}t={s0,r1,s1,,st,rt+1,st+1,}
  • Task

    Estimate State Values {vπ(s)}sS under π

    vπ(st)=E[Rt+1+γvπ(St+1)|St=st]st

 

 

1.2 Algorithm

核心机制类似于 Stochastic Gradient Descent

{vt+1(st)=vt(st)αt(st){vt(st)[rt+1+γvt(st+1)]}vt+1(s)=vt(s)sst

where

  • st The visited State at Time Step t

  • vt(st) Estimation of State Value vπ(st)

  • αt(st) Learning Rate depending on st at Time Step t (a small positive number)

 

1.2.1 关于第一个公式
vt+1(st)New Estimation=vt(st)Current Estimationαt(st){vt(st)[rt+1+γvt(st+1)]TD Target v¯t}TD Error δt
  • TD Target

    v¯t=rt+1+γvt(st+1)

    where

    • rt+1 当前通过 stst+1 的 Transition 得到的 Reward

    • λ Discount Rate

    • vt(st+1) State Value Estimation of st+1 at Time Step t

      vt(st+1)vt+1(st) 的意义是不同的!!!

      • 前者是在 Time Step t,对下一个 Time Step t+1 可能抵达的 State st+1 进行的 State Value Estimation!

      • 后者是在下一个 Time Step t+1,对当前 Time Step t 所 access 的 State st 进行的 State Value Estimation!

    TD Target v¯t(st) 是利用当前的 Experience Sample 对 vπ(st)单步估计样本

    将其与 Bellman Expectation Equation 对比有助于理解其含义

    • State Value 定义式

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

      Discounted Return Gt 是 State Value vπ(st) 的 Expectation

    • State Value 递推式

      vπ(st)=E[Rt+1+γvπ(St+1)|St=st]

      这也是 BEE 的一种形式!(但不是 Matrix-Vector Form)(名字是我自己取的,这不重要)

    • TD Target

      v¯t=rt+1+γvt(st+1)

      Discounted Return 公式展开是

      Gt=Rt+1+γvπ(St+1)

      公式结构的相似性说明,TD Target 类似是对 Discounted Return 的一次采样

      由于 Discounted Return 是 State Value 的 Expectation,所以 TD Target 成为与 vt(st) 作差的对象就非常合理咯

  • TD Error

    δt=vt(st)[rt+1+γvt(st+1)]
    • Intuition

      TD(0) 对 State Value Approximation 的 update 方式和 Newton's Method 非常像

      结合前文探讨的 TD Target 意义去看下面的这个改写公式,这个 TD Error 的写法是非常合理的

      δt=vt(st)v¯t

      It drives vt towards v¯t

      vt+1(st)=vt(st)αt(st)[vt(st)v¯t][vt+1(st)v¯t]=[vt(st)v¯t]αt(st)[vt(st)v¯t][vt+1(st)v¯t]=[1αt(st)][vt(st)v¯t]

      由于 αt(st) 是一个 small positive number,所以有

      0<1αt(st)<1

      所以

      |vt+1(st)v¯t|=[1αt(st)]|vt(st)v¯t|

      |vt+1(st)v¯t||vt(st)v¯t|

      所以每次 Time Step 更新vt(st) 一定会离 v¯t 更近

    • A Measure of the Deficiency between vt and vπ

      假设此时 vt=vπ,则

      δπ,t=vπ(st)[rt+1+γvπ(st+1)]E[δπ,t|St=st]=vπ(st)E[Rt+1+γvπ(St+1)|St=st]

      由于 TD Target 中提到的

      vπ(st)=E[Rt+1+γvπ(St+1)|St=st]

      所以有

      E[δπ,t|St=st]=vπ(st)E[Rt+1+γvπ(St+1)|St=st]=0

      即,当 TD Error =0 时,vt=vπ

 

1.2.2 关于第二个公式
vt+1(s)=vt(s)sst

对于所有在当前 Time Step 未被 access 的 State,其对应的 State Value 估计值不变

THAND GOD! 解释这个比上面那个烦人的公式方便多了...

 

 

1.3 Algorithm Derivation

这个算法的本质是用 Special Topics 里的 Robbins-Monro Algorithm 来解一个诡异的 BEE 的表达式

  • BEE 的 State Value 递推式

    1.2.1 中提到的

    vπ(st)=E[Rt+1+γvπ(St+1)|St=st]
  • RM Problem Formulation

    g(vπ(s))=vπ(st)E[Rt+1+γvπ(St+1)|St=st]=0
  • RM Algorithm

    vk+1(sk)=vk(sk)αkg~(vk(sk))=vk(sk)αk{vk(sk)[rk+γvπ(sk)]}

    where sk is the current state at Step k, sk is the next state transitioned to after an action

    做如下两个修改,以适应 RL 语境

    • {(sk,rk,sk)}{(st,rt+1,st+1)}

      • 前者为 RM Algorithm 用到的一组样本,运算过程中需要不断采样,而它们都是独立采样的,有 independent & identically distributed 这一假设

      • 但是显然做 RL 的时候你肯定没那个心情去一组一组采,TD 依赖的是 Experience Samples (see 1.1) ("Trajectory"),而只有后者的形式才能表示一条 Trajectory

    • vπ(sk)vπ(st+1)vk(st+1)

      我们不可能预先知道 vπ,所以只能用 vk,即其 Estimation 来代替

      这种替换仍然能确保 vk 收敛

    最后变成

    vk+1(st)=vk(st)αk{vk(st)[rt+1+γvk(st+1)]}

    然后,由于

    • kt 实际上是同一个东西

    • αt 在这个部分描述的算法中 depends on st

    所以...Voila~

    vt+1(st)=vt(st)αt(st){vt(st)[rt+1+γvt(st+1)]}

 

 

1.4 Convergence

  • Theorem

    By the algorithm introduced in the above sections:

    Given Policy π, if tαt(s)= and tαt2(s)< for all sS

    Then, it is almost for sure that vt(s)vπ(s) as t for all sS

  • 关于 Learning Rate

    实操中 αt(st) 一般是一个 small constant(哪怕这会导致不满足 tαt2(s)<

    因为如果放任 αt(st) 越来越小,到很久很久以后 Experience Samples 就会因为 Learning Rate 过小而失去作用

 

 

 

2. Sarsa Algorithms

TD Learning of Action Values,功能为 Policy Evaluation

  • 可以估计 Action Value

  • 可以搜索 Optimal Policy

    因为它终于开始涉及 Action 了,能评估不同 Action 的效果(求 Action Value),就能设法用 ϵ-Greedy 的方式去选出合适的 Action 来更新 Policy

 

 

2.1 Problem Formulation

  • Given

    1. Policy π

    2. Experience Samples generated by π in the form

      {(st,at,rt+1,st+1,at+1)}t={s0,a0,r1,s1,a1,,st,at,rt+1,st+1,at+1,}

      State, Action, Reward, State, Action = SARSA

  • Task

    Estimate Action Values {qπ(s,a)}sS,aA under π

 

 

2.2 Sarsa

相当于 Action Value 版本的 TD(0)

  • Mathematical Objective

    a stochastic approximation algorithm solving

    qπ(st,at)=E[Rt+1+γqπ(St+1,At+1)|St=st,At=at]st,at

    这是一种 BEE 用 Action Value 表达的写法

  • Algorithm

    每次 Update Action Value 需要的 Experience 为一组 {st,at,rt+1,st+1,at+1}

    {qt+1(st,at)=qt(st,at)αt(st,at){qt(st,at)[rt+1+γqt(st+1,at+1)]}qt+1(s,a)=qt(s,a)(s,a)(st,at)

    where

    • st The visited State at Time Step t

    • at The selected Action at Time Step t

    • qt(st,at) Estimation of Action Value qπ(st,at)

    • αt(st) Learning Rate depending on st,at at Time Step t (a small positive number)

  • Pseudo-Code

    pseudo sarsa

 

 

2.3 Expected Sarsa

与一般版本的 Sarsa 的区别在于 TD Target 稍微变了一下

qt(st+1,at+1)E[qt(st+1,A)]

这样能将每组 Experience 涉及的 Random Variable 减少

{st,at,rt+1,st+1,at+1}{st,at,rt+1,st+1}
  • Matwhematical Objective

    a stochastic approximation algorithm solving

    qπ(st,at)=E[Rt+1+γE[qπ(St+1,At+1)|St+1]|St=st,At=at]st,at

    这是也一种 BEE 用 Action Value 表达的写法

  • Algorithm

    每次 Update Action Value 需要的 Experience 为一组 {st,at,rt+1,st+1}

    {qt+1(st,at)=qt(st,at)αt(st,at){qt(st,at)(rt+1+γE[qt(st+1,A)])}qt+1(s,a)=qt(s,a)(s,a)(st,at)
  • Notes

    1. Needs more computation

    2. 由于减少了涉及的 Random Variable,所以能 reduce estimation variances

 

 

2.4 n-Step Sarsa

这是个介于 2.2 的一般 Sarsa 和 Monte-Carlo 之间的算法,关键在 Action Value 的公式

qπ(s,a)=E[Gt|St=s,At=a]
  • Intuition

    Discounted Return Gt can be written in different form as

    SarsaGt(1)=Rt+1+γqπ(St+1,At+1)Gt(2)=Rt+1+γRt+2+γ2qπ(St+2,At+2)n-Step SarsaGt(2)=Rt+1+γRt+2++γn1Rt+n+γnqπ(St+n,At+n)Monte-CarloGt()=Rt+1+γRt+2+γ2Rt+3+

    [ WARNING ]

    请千万记住,以上只是 written in different forms!

    Gt=Gt(1)=Gt(2)=Gt(n)=Gt()

    所有的 superscript 都只是用于表示 Gt 的分解程度

  • Mathematical Objective

    • Sarsa

      It aims to solve

      qπ(s,a)=E[Gt(1)|St=s,At=a]=E[Rt+1+γqπ(St+1,At+1)|St=s,At=a]
    • Monte-Carlo

      It aims to solve

      qπ(s,a)=E[Gt()|St=s,At=a]=E[Rt+1+γRt+2+γ2Rt+3+|St=s,At=a]
    • n-Step Sarsa

      It aims to solve

      qπ(s,a)=E[Gt(n)|St=s,At=a]=E[Rt+1+γRt+2++γn1Rt+n+γnqπ(St+n,At+n)|St=s,At=a]
      • n-Step Sarsa = Sarsa, when n=1

      • n-Step Sarsa = Monte-Carlo, when n=

  • Algorithm

    每次 Update Action Value 需要的 Experience 为一组 {st,at,rt+1,st+1,at+1,,rt+n,st+n,at+n}

    We need to wait until Time Step n+1 to update Action Value of (st,at)

    {qt+n+1(st,at)=qt+n(st,at)αt+n(st,at){qt+n(st,at)(rt+1+γrt+2++γn1rt+n+1+γnqt+n(st+n+1,At+n+1))}qt+1(s,a)=qt(s,a)(s,a)(st,at)
  • Notes

    Performance is a blend of Sarsa and Monte-Carlo

    • if n is large, then it has a large variance but a small bias

    • if n is small, then it has a large bias but a small variance

 

 

 

3. Q-Learning

TD Learning of Optimal Action Values

和 Sarsa 很像,但 TD Target 以及做 Policy Improvement 时所用方法的区别非常大

 

 

3.1 Algorithm

  • Mathematical Objective

    It aims to solve

    q(s,a)=E[Rt+1+γmaxaq(St+1,a)|St=s,At=a]

    This is a BOE instead of BEE expressed in terms of Action Values!

    所以 Q-Learning 能直接 estimate (optimal policy 对应的) optimal action value

  • Algorithm

    每次 Update Action Value 需要的 Experience 为一组 {st,at,rt+1,st+1}

    {qt+n+1(st,at)=qt+n(st,at)αt+n(st,at){qt+n(st,at)[rt+1+γmaxαAqt(st+1,a)]}qt+1(s,a)=qt(s,a)(s,a)(st,at)

    不需要 at+1 的原因是,它应该是通过 maxaA 得到的

  • Notes

    Q-Learning 有 On-Policy 和 Off-Policy 两种版本

 

 

3.2 On-Policy Ver. Pseudo-Code

pseudo On-Policy QLearning

 

 

3.3 Off-Policy Ver. Pseudo-Code

请注意,Off-Policy Learning 的算法在 Update Policy 时不需要考虑 Target Policy 的探索性,反正 Behavior Policy 能 handle 探索的问题

所以,Behavior Policy πb 与 Target Policy πT 的分离,导致在 Update Policy 的时候,可以舍弃 ϵ-Greedy 而直接用 Greedy 的方式更新

pseudo Off-Policy QLearning

 

 

 

[ Notes ] Universal Expression

  • Algorithm

    universal

  • Mathematical Objective

    mathematical objective

 

 

 

[ Notes ] On/Off-Policy Learning

Temporal-Difference Learning 的算法涉及 Generate ExperienceOptimize Policy 两件事

  • Behavior Policy is used to Generate Experience Samples

  • Target Policy is constantly updated towards Optimal Policy

所谓 On-Policy 与 Off-Policy 的区别便在于这两种 Policy 是否一致

  • On-Policy Learning Behavior Policy = Target Policy

    Policy 先被用来 Generate Experience,然后 Optimize,这两个步骤不断循环

  • Off-Policy Learning Behavior Policy Target Policy

    Policy A 专门用来 Generate Experience,用来 Optimize Policy B

比如 Sarsa 和 Monte-Carlo 都是 On-Policy,Q-Learning 一般是 Off-Policy 的,但也可以被 implement 成 On-Policy 的形式

 

 

 

Special Topics - Robbins-Monro

这个 RM Algorithm 是 Temporal Difference Method 的基础

 

1. Algorithm

一种用于 Stochastic Approximation 的算法

  • Problem Formulation

    很多问题都能写成这种形式:find the Root w of equation g that satisfies

    g(w)=0

    解的时候有两种情况:

    1. Model-Based

      已知 g 是什么,那么就会有各种 Numerical Method 任君挑选

    2. Model-Free

      未知 g 是什么,那么就用 Robbins-Monro (RM) Algorithm 来解

  • Algorithm

    wk+1=wkakg~(wk,ηk)

    where

    • wkkth estimation of root

    • ×ak 某 Positive Coefficient

    • ηk 观测时的 Noise,不理解可见 3.1 的例子

    • g~(wk,ηk)=g(wk)+ηk Noisy Observation

    还是那句话:没 Model 就得有 Data,没 Data 就得有 Model

  • Example

    g(w)=tanh(w1)

    Assume parameters

    • w1=3

    • ak=1/k

    • ηk=0 即假设没有 Noise,g~(wk,ηk)=g(wk)

    则 RM Algorithm 为

    wk+1=wkakg(wk)

    迭代效果如下图

    RM example

    实际上无论初始值的正负如何,最后总是能迭代收敛到 True Root w=1

 

 

 

2. Convergence Theorem

In RM Algorithm, if

  • 0<c1wg(w)c2

    g图像始终递增梯度有界,这样能确保一定有 g(w)=0

  • k=1ak= and k=1ak2<

    后者确保 "ak0 as k",前者确保 "ak 不会收敛得太快"

  • E[ηk|Hk]=0 and E[ηk2|Hk]< where Hk={wk,wk1,...}

    前者表示 "ηk 的 mean 为 0",后者表示 "ηk 有界",noise 不一定非要是 Gaussian

then wk converges with probability 1 (w.p.1) to the root w satisfying g(w)=0

“with probability 1” 表示的是概率意义上的收敛,因为涉及随机变量采样

 

 

 

3. More Examples

走过路过,不要错过!Mean Estimation!

 

 

3.1 嗯...

Solve for w with Independent & Identically Distributed Samples {x} of X

w=E[X]

Reformulate the problem to

g(w)=wE[X]=0

得到的 Samples {x} of X 相当于是 Noisy Observation

g~(w,η)=wx=(wE[X])+(E[X]x)=g(w)η

where the Noise is

η=E[X]x

那么 RM Algorithm Formulation 为

wk+1=wkak(wkxk)

 

3.2 哦吼吼~

增加点难度~

Solve for w with Independent & Identically Distributed Samples {x} of X

w=E[v(X)]

Reformulate the problem to

g(w)=wE[v(X)]=0

得到的 Samples {x} of X 相当于是 Noisy Observation

g~(w,η)=wv(x)=(wE[v(X)])+[E[v(X)]v(x)]=g(w)η

where the Noise is

η=E[v(X)]v(x)

那么 RM Algorithm Formulation 为

wk+1=wkak[wkv(xk)]

 

3.3 《 你 好 》

再加点佐料~

Solve for w with Independent & Identically Distributed Samples {x} of X and Samples {r} of R

w=E[R+γv(X)]

Reformulate the problem to

g(w)=wE[R+γv(X)]=0

得到的 Samples {x} of X 相当于是 Noisy Observation

g~(w,η)=w[r+γv(x)]=(wE[R+γv(X)])+{E[R+γv(X)][r+γv(x)]}=g(w)η

where the Noise is

η=E[R+γv(X)][r+γv(x)]

那么 RM Algorithm Formulation 为

wk+1=wkak{wk[rk+γv(xk)]}