Deep learning - the depth of reinforcement learning (DRL) -Policy Gradient and PPO notes

Policy Gradient

  CHANG initial learning reinforcement learning to speak, listen Taiwanese accent really went to great lengths, and later saw a sort of enthusiastic blog very detailed, so the reprint of view, as the review notes left with, in the end the description link . After reading notes CHANG go listen to the teacher's video, you can understand one about. Of course there are Mo Fan reinforcement learning more practical nature, listen Mo Fan lesson can basically take us started.

The basic ideas and terminology

basic component:

  1.actor (ie policy gradient object you want to learn, we can control is part of)

  2. Environmental environment (given, can not be controlled)

  3. reward function reward function (not control)

Policy of actor π:

  As shown below, the neural network can be understood as the Policy contains a parameter θ, the network will be observed as input variables of the model, based on an output corresponding to the probability of action action

  

Episode:

  A complete game round from start to finish

actor goal:

  Maximize total return reward

Trajectory τ :

  State action and state action sequences

Given neural network parameters θ probability action state sequence τ occurs:
  the product of the probabilities: the probability of the initial state appeared; given the current state, to take the probability a particular action; and thereafter take the action, based on the action probability and the current state returns the next state, formulated as:

 

  Given the state of an action sequence τ, we can get the revenue corresponding reward it by controlling the actor, we can get different benefits. Since the emergence of the next action actor taken and given a certain state environmental state is random, the ultimate goal is to find an actor with the greatest expected return (ie the following formula) of.

  Cumulative expected returns: probability of taking a certain action sequences τ of the state, and that state action sequence corresponding revenue reward the sum of the products.

  

Policy Gradient

  得出目标函数之后,就需要根据目标函数求解目标函数最大值以及最大值对应的policy的参数 θ。类比深度学习中的梯度下降求最小值的方法,由于我们这里需要求的是目标函数的最大值,因此需要采取的方法是梯度上升。也就是说,思想起点是一样的,即需要求出目标函数的梯度。

  求解梯度的步骤如下,以前文所述目标函数为基础,对参数 θ 求导,其中,对概率加权的reward求和就是求reward的期望,因此有红框部分的改写,又因为训练的过程中会进行采样训练,采样个数为N,因此公式可以近似表示为N词采样得到的reward的平均。

  

Tip 1: 添加基准线

  由于训练过程中采样是随机的,可能会出现某个行动不被采样的情况,这会导致采取该行动的概率下降;另外,由于采取的行动概率和为一,可能存在归一化之后,好的action的概率相对下降,坏的action概率相对上升的情况,因此需要引入一个基准线baseline b.
  具体的例子:当policy决定采取的三个action a,b,c均有正的reward时,比如3,4,5,在计算各个action的概率的时候,本来应该给action c分配较大的概率,但是归一化之后,a的概率反而可能上升,c的概率可能会下降,与对应reward应该被分配的概率分布不符。但是引入baseline之后,可能a的reward会变为负,这样的话,采取该行动的概率就会下降。

    

Tip 2: 进一步考虑各个时间点的累积收益计算方式

  考虑到在时间t采取的行动action与t时期之前的收益reward无关,因此只需要将t时刻开始到结束的reward进行加总。并且,由于行动action对随后各时间点的reward的影响会随着时间的推移而减小,因此加入折扣因子 γ 。
  这样就得到了一个考虑比较全面,比较完善的梯度计算方式。

从on-policy到off-policy (反复多次使用经验)

术语和基本思想
  On-policy: 学习到的agent以及和环境进行互动的agent是同一个agent
  Off-policy: 学习到的agent以及和环境进行互动的agent是不同的agent

为什么要引入 Off-policy:

  

  如果我们使用 πθ来收集数据,那么参数 θ 被更新后,我们需要重新对训练数据进行采样,这样会造成巨大的时间消耗。

  目标:利用 πθ' 来进行采样,将采集的样本拿来训练 θ, θ'是固定的,采集的样本可以被重复使用。

原理:Important sampling:

  当我们只有通过另外一个分布得到的样本时,期望值可以做出以下更改,更换分布之后,需要使用重要性权重p(x)/q(x)来修正f(x),这样就实现了使用q分布来计算p分布期望值。

    

  需要注意的是,两个分布p,q之间的差别不能太大,否则方差会出现较大的差别。

  先基于原始的分布p计算函数的方差,然后计算引入不同分布q之后得到的函数方差,可以发现两者得出的方差表达式后面一项相同,主要差别在于前面那一项,如果分布p和q之间差别太大,会导致第一项的值较大或较小,于是造成两者较大的差别。

  

  如果p,q 两个分布的差别过大,在训练的过程中就需要进行更多次数的采样。

  比如下图中,实际分布p和辅助分布q差别较大,横轴左边表示收益为负,右边表示收益为正。蓝色的线表示真实分布p的分布,主要集中在左边,也就是说,真实情况下reward的期望值应该是负的。

  但是由于辅助分布q,即绿色线,主要集中在右侧,因此在采样的时候采到右边的概率更大,可能会导致多轮采样之后,算出来的期望收益依旧为正,只有当采样到左侧的点,并且乘上较大的修正系数 p/q之后,算出的结果才会变成真实的符号,负号。尽管采样到右侧的点修正系数很小,最终结果可能依旧是正确的,但这样会导致在采样上耗费较大的时间,因此,p、q分布之间的差异依旧不宜过大。

    

  从上述important sampling的思想出发,可以使用该思想来达到上文所述的目标,即 “利用 πθ' ​来进行采样,将采集的样本拿来训练 θ, θ' 是固定的,采集的样本可以被重复使用” 表示为:

  

提醒 :

  1.Advantage function (收益reward减去基准baseline) 也应该随着新的采样参数而变化(从基于θ'的Aθ 变为基于θ′ 的Aθ′

  2.在不同的参数情况下,某一个状态state出现的概率几乎没有差别,因此可以将这一项近似地消掉
  3.stop criteria取决于两个分布之间的差别大小

  

  加入约束: (θ不能与 θ'差别过大)
  Tip: 这是一项加在行为上的约束,而不是加在参数上的约束

PPO / TRPO:

  PPO在原目标函数的基础上添加了KL divergence 部分,用来表示两个分布之前的差别,差别越大则该值越大。那么施加在目标函数上的惩罚也就越大,因此要尽量使得两个分布之间的差距小,才能保证较大的目标函数。

  TRPO 与 PPO 之间的差别在于它使用了 KL divergence 作为约束。但是这使得TRPO相对而言更难计算,因此较少使用。

 

PPO 算法

  1.初始化policy的参数θ0

  2.在每一次迭代中,使用θk来和环境互动,收集状态和行动并计算对应的advantage function

  3.不断更新参数,找到目标函数最优值对应的参数 θ 在训练的过程中采用适应性的KL惩罚因子:

    当KL过大时,增大beta值来加大惩罚力度

    当KL过小时,减小beta值来降低惩罚力度

  

PPO2:

Tips:

  1. PPO2引入了Clip函数,意味着第二项,即蓝色的虚线必须在 1-ϵ 和 1+ϵ 之间

    

  2. 红色的线表示取最小值之后整个函数值分布情况.

    

 

李宏毅老师的机器学习PPT,强化学习PPT在最后http://speech.ee.ntu.edu.tw/~tlkagk/courses_MLDS18.html

转载博客https://blog.csdn.net/cindy_1102/article/details/87905272

Guess you like

Origin www.cnblogs.com/yang901112/p/11985424.html