在上一篇文章强化学习中的无模型预测中,有说过这个无模型强化学习的预测问题,通过TD
、n-step TD
或者MC
的方法能够获得值函数,那拿到value
之后如何获取policy
呢?
Model-Free Control in Reinforcement Learning
在model free control
中的第一个概念就是on-policy
和 off-policy
,在现实中的很多问题都是这样的,我们得不到environment
的白盒模型,我们就需要去与环境互动,获取经验数据,然后基于经验数据进行学习。或者是MDP
的内部模型已知,但是状态空间、动作太大了无法对其采样。model free
的方法就可以去解决这个问题。
On-and Off-Policy Learning
- Two categories of model-free RL
在model-free
这个设定下面有两个概念,一个是on-policy learning一个是off-policy learning。
On policy
“Learn on the job” ,意思是说马上要给policy喂进来的数据都是刚刚采出来的数据。Learn about policy π from experience sampled from π。
Off-policy
“Look over someone’s shoulder” 当前学习的策略π的数据从另外一个策略μ采样出来的。Learn about policy π from experience sampled from another policy μ。
Model-Free Policy Iteration
如果我们用state value
去更新的话,我们就需要去知道state transition
,而基于state-action value
的情况就好很多:
- Greedy policy improvement over V(s) requires model of MDP:
πnew(s)=a∈Aargmax{R(s,a)+γs′∈S∑Psa(s′)Vπ(s′)}
But we don’t know the state transition probability. 如果我们基于state-action value更新的话我们就不需要知道state transition probability。
- Greedy policy improvement over Q(s,a) is model-free
πnew(s)=a∈AargmaxQ(s,a)
因此计算state-value
和计算state-action value
有本质的区别。之后的方法基本上都是基于state-action value
的。
Generalized Policy Iteration with Action-Value Function
在给定某一个policy
之后,我们就可以去算Qπ。以前的文章马尔可夫决策过程与动态规划算的是state value
,这里算的是state-action value
。得到这个Q之后我们就能够依据πnew(s)=a∈AargmaxQ(s,a)得到我们的策略。

Control Policy
基于Q value
我们具体怎么去做control policy
?就是我们具体怎么来 choose action
?
Example of Greedy Action Selection
在Greedy Action
中每次都去选择state-action value
最大的那一个:
πnew(s)=a∈AargmaxQ(s,a)
但是这里有问题,就是当你采样到了一个动作之后,拿到了reward
,你之后就不会去选择探索其它的动作了。The policy would be suboptimal if there is no exploration.
ε-Greedy Policy Exploration
最简单的办法就是让这个policy
有一些随机,或者称之为exploration
。于是就变成了:
- With probability 1−ε, choose the greedy action.
- With probability ε, choose an action at random.
于是在状态s下选择动作a的概率π(a∣s)可表示为如下形式:
π(a∣s)={ϵ/m+1−ϵϵ/m if a∗=argmaxa∈AQ(s,a) otherwise
ε-Greedy Policy Improvement
那ε-Greedy Policy能不能往前推进我们的state value
呢?我们需要去证明一下:
-
Theorem:For any ε-greedy policy π ,the ε-greedy policy π′ w.r.t Qπ is an improvement, i.e. Vπ′(s)≥Vπ(s)。
Vπ′(s)=Qπ(s,π′(s))=a∈A∑π′(a∣s)Qπ(s,a)=mϵa∈A∑Qπ(s,a)+(1−ϵ)a∈AmaxQπ(s,a)≥mϵa∈A∑Qπ(s,a)+(1−ϵ)a∈A∑1−ϵπ(a∣s)−ϵ/mQπ(s,a)=a∈A∑π(a∣s)Qπ(s,a)=Vπ(s)
其中m表示 actions 个数,这里其实就是在证明,用π′得到的state value
与之前的π得到的state value
是有所改进的。上述公式中推导比较难懂的就是∑a∈A1−ϵπ(a∣s)−ϵ/m,这一步了。这里把:
π(a∣s)={ϵ/m+1−ϵϵ/m if a∗=argmaxa∈AQ(s,a) otherwise
带入到∑a∈A1−ϵπ(a∣s)−ϵ/m就会发现,其实这也是个概率,一旦它是个概率,那么一定会小于等于maxa∈AQπ(s,a)。这里也就证明了其实 ε-greedy 是做到了policy improvement 这一步。
Monte-Carlo Control

其算法也是大体分为两步:
- Policy evaluation: Monte-Carlo policy evaluation,Q≈Qπ。
- Policy improvement: ε-greedy policy improvement。
MC Control vs. TD Control
MC Control
中看一整个episode
,然后estimate value
然后做更新,TD Control
只是看一小个片段进行更新。
- Temporal-difference (TD) learning has several advantages over Monte-Carlo (MC) :
Lower variance
、Online
、Incomplete sequences
On-Policy SARSA
其算法流程分为以下几步:
- At state s, take action a ;
- Observe reward r ;
- Transit to the next state s′;
- At state s′, take action a′。
Updating action-value functions with SARSA:
Q(s,a)←Q(s,a)+α(r+γQ(s′,a′)−Q(s,a))

SARSA
算法的整个流程如下所示:

- NOTE: on-policy TD control sample actions by the current policy, i.e., the two ‘A’s in SARSA are both chosen by the current policy。
Off-Policy Learning
我们能否从 policy
μ(a∣s) 采出来的数据 {s1,a1,r2,s2,a2,…,sT}∼μ 去学习策略 π(a∣s) ?当μ(a∣s)=π(a∣s)这个算法就是on policy
的,因此off-policy
的算法都可以做on-policy
,其兼容性更强一点。
Why off-policy learning is important?
- Learn from observing humans or other agents
比如在自动驾驶中,我们可以先学习一点人类经验数据,相当于做个预训练。
- Re-use experience generated from old policies
虽然是自己之前的数据,但是由于现在的参数和之前的参数不一样,所以off policy
的方法可以重复使用之前的训练数据。
强化学习需要的data
是非常大的,因为它有三个自由度的分布函数R(s)、Psa(s′)、π(a∣s)。

因此使用之前的data
就比较重要了。
还有一点就是商业上面就没办法使用当前采样出来的数据直接进行训练。
Importance Sampling
那off-policy
怎么来做off policy
的learning
呢?Q
值的更新可表达为以下形式:
Q(s,a)←Q(s,a)+α(r+γQ(s′,a)−Q(s,a))
我们是拿后面采样出来的Q(s′,a),去更新前面采样所得到的Q(s,a),前面采样策略与后面采样策略不一样,那肯定就会有些问题。
Importance Sampling can estimate the expectation of a different distribution :
Ex∼p[f(x)]=∫xp(x)f(x)dx=∫xq(x)q(x)p(x)f(x)dx=Ex∼q[q(x)p(x)f(x)]
Re-weight each instance by β(x)=q(x)p(x) 。
这里把从分布p中采样的数据求期望,转变成了从分布q中去采样数据求期望。这里只是把每个数据的权重做了些许改变,因此在Importance Sampling
里面我们往往也会去计算β(x)=q(x)p(x),甚至有机器学习专门去学习这个β(x),然后去做一个比较好的Importance Sampling
。
在机器学习里面,我们经常会优化这样一个式子:
θmin∣D∣1x∈D∑L(y,fθ(x))=Ex∼p(x)[L(y,fθ(x))]
但很多时候我们拿到的数据并不满足p(x)分布,而满足q(x)分布,因此做个重要性采样就很好地解决这个问题。
Importance Sampling for Off-Policy Monte-Carlo
通过重要性采样我们仍然需要去计算值函数。比如我们如何使用策略μ所获得的return
去评估策略π? 依据Importance Sampling
我们可以weight return
Gt,对于一个episode
,
{s1,a1,r2,s2,a2,…,sT}∼μ
Multiply importance ratio along with episode:
Gtπ/μ=μ(at∣st)π(at∣st)μ(at+1∣st+1)π(at+1∣st+1)⋯μ(aT∣sT)π(aT∣sT)Gt
表示在策略π下会以更多或者更小的概率去看到Gt,然后再以这个新的Gt去更新算法:
V(st)←V(st)+α(Gtπ/μ−V(st))
- Cannot use if μ is zero when π non-zero
- Importance sample can dramatically increase variance
Importance Sampling for Off-Policy TD
将 Importance Sampling
用在TD
上面会更容易一点,因为TD
算法只走一步。因此只有TD target
r+γV(s′)是在做importance sampling,so only need a single importance sampling correction :
V(st)←V(st)+α(μ(at∣st)π(at∣st)(rt+1+γV(st+1))−V(st))
- Much lower variance than Monte-Carlo importance sampling
- Policies only need to be similar over a single step
上述importance sampling
过程是一个unbias
过程,但是variance
可能特别大,因为μ(at∣st)可能特别小,一除就变大了。
因此在做off-policy
的时候,我们很少会用传统的importance
的办法,它虽然可以做,但是不做改进的话还是不太好。
Q-Learning
Q-Learning focus
在state-action value
上面,这一点具有特别深远的意义。我们是在更新Q(s,a) 这样一个state-action value function
,因为如果我们更新V(s)的话,它上来就需要策略,Vπ(s)=∑aπ(a∣s)(R+Vπ(s′))。而如果更新的是Q(s,a),其实tack
这个action
跟policy
没有关系,无论policy
是什么,我都会去更新Q(s,a),也就是state
st和at和policy
是解耦合关系,之后环境给及时奖励 r 和下一个状态st+1,这都是和环境有关的东西,我可以采样,但跟policy
没关系,就不会像Importance sampling
那样还需要除一下。之后的at+1不一样可能会产生点区别,前面的 at 是用 π 采样出来的,还是μ采样出来的都没有关系。
上面说了这么多,其实就是需要注意:No importance sampling is required (why?)
Q(st,at)←Q(st,at)+α(rt+1+γQ(st+1,a′)−Q(st,at))
- The target policy π is greedy w.r.t. Q(s,a):
π(st+1)=a′argmaxQ(st+1,a′)
- The behavior policy μ is e.g. ε-greedy policy w.r.t. Q(s,a):
rt+1+γQ(st+1,a′)=rt+1+γQ(st+1,a′argmaxQ(st+1,a′))=rt+1+γa′maxQ(St+1,a′)
因此 Q-learning update:
Q(st,at)←Q(st,at)+α(rt+1+γa′maxQ(st+1,a′)−Q(st,at))
Why Q-learning is an off-policy control method?
- Learning from SARS generated by another policy μ
- The first action aand the corresponding reward rare from μ
- The next action a′ is picked by the target policy π(st+1)=a′argmaxQ(st+1,a′)。
这也就是为什么q-learning是off policy却不用importance sampling
SARSA vs. Q-Learning Experiments

Relationship Between DP and TD


n-Step Prediction





Averagingn-Step Returns

TD(λ) for Averagingn-Step Returns



TD(λ) vs. n-step TD

我的微信公众号名称:深度学习与先进智能决策
微信公众号ID:MultiAgent1024
公众号介绍:主要研究分享深度学习、机器博弈、强化学习等相关内容!期待您的关注,欢迎一起学习交流进步!