├── update.sh ├── docs ├── img │ ├── fig14_1.png │ ├── fig14_2.png │ ├── fig14_3.png │ ├── fig14_4.png │ ├── fig1_1.png │ ├── fig1_2.png │ ├── fig3_1.png │ ├── fig3_2.png │ ├── fig3_3.png │ ├── fig3_4.png │ ├── fig3_5.png │ ├── fig5_1.png │ ├── fig5_2.png │ ├── fig6_1.png │ ├── fig6_2.png │ ├── fig6_3.png │ ├── fig8&9_1.png │ ├── fig8&9_2.png │ ├── fig8&9_3.png │ ├── fig11&12_1.png │ ├── fig14_alg_1.png │ ├── fig14_alg_2.png │ ├── fig14_alg_3.png │ ├── fig1_tab_1.png │ ├── fig3_alg_1.png │ ├── fig3_alg_2.png │ ├── fig3_alg_3.png │ ├── fig3_alg_4.png │ ├── fig3_alg_5.png │ ├── fig3_alg_6.png │ ├── fig3_tab_1.png │ ├── fig4_alg_1.png │ ├── fig4_alg_2.png │ ├── fig4_alg_3.png │ ├── fig4_alg_4.png │ ├── fig4_alg_5.png │ ├── fig4_alg_6.png │ ├── fig5_alg_1.png │ ├── fig5_tab_1.png │ ├── fig6_alg_1.png │ ├── fig7_alg_1.png │ ├── fig7_alg_2.png │ ├── fig7_alg_3.png │ ├── fig8&9_alg_1.png │ ├── fig8&9_alg_2.png │ └── fig11&12_alg_1.png ├── 1.md ├── 14.md ├── 7.md ├── 5.md ├── 4.md ├── 11&12.md ├── 6.md ├── 3.md ├── 8&9.md └── 10.md ├── README.md ├── .gitignore ├── CONTRIBUTING.md └── LICENSE /update.sh: -------------------------------------------------------------------------------- 1 | git add -A 2 | git commit -am "$(date "+%Y-%m-%d %H:%M:%S")" 3 | git push -------------------------------------------------------------------------------- /docs/img/fig14_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig14_1.png -------------------------------------------------------------------------------- /docs/img/fig14_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig14_2.png -------------------------------------------------------------------------------- /docs/img/fig14_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig14_3.png -------------------------------------------------------------------------------- /docs/img/fig14_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig14_4.png -------------------------------------------------------------------------------- /docs/img/fig1_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig1_1.png -------------------------------------------------------------------------------- /docs/img/fig1_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig1_2.png -------------------------------------------------------------------------------- /docs/img/fig3_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig3_1.png -------------------------------------------------------------------------------- /docs/img/fig3_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig3_2.png -------------------------------------------------------------------------------- /docs/img/fig3_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig3_3.png -------------------------------------------------------------------------------- /docs/img/fig3_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig3_4.png -------------------------------------------------------------------------------- /docs/img/fig3_5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig3_5.png -------------------------------------------------------------------------------- /docs/img/fig5_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig5_1.png -------------------------------------------------------------------------------- /docs/img/fig5_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig5_2.png -------------------------------------------------------------------------------- /docs/img/fig6_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig6_1.png -------------------------------------------------------------------------------- /docs/img/fig6_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig6_2.png -------------------------------------------------------------------------------- /docs/img/fig6_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig6_3.png -------------------------------------------------------------------------------- /docs/img/fig8&9_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig8&9_1.png -------------------------------------------------------------------------------- /docs/img/fig8&9_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig8&9_2.png -------------------------------------------------------------------------------- /docs/img/fig8&9_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig8&9_3.png -------------------------------------------------------------------------------- /docs/img/fig11&12_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig11&12_1.png -------------------------------------------------------------------------------- /docs/img/fig14_alg_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig14_alg_1.png -------------------------------------------------------------------------------- /docs/img/fig14_alg_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig14_alg_2.png -------------------------------------------------------------------------------- /docs/img/fig14_alg_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig14_alg_3.png -------------------------------------------------------------------------------- /docs/img/fig1_tab_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig1_tab_1.png -------------------------------------------------------------------------------- /docs/img/fig3_alg_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig3_alg_1.png -------------------------------------------------------------------------------- /docs/img/fig3_alg_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig3_alg_2.png -------------------------------------------------------------------------------- /docs/img/fig3_alg_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig3_alg_3.png -------------------------------------------------------------------------------- /docs/img/fig3_alg_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig3_alg_4.png -------------------------------------------------------------------------------- /docs/img/fig3_alg_5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig3_alg_5.png -------------------------------------------------------------------------------- /docs/img/fig3_alg_6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig3_alg_6.png -------------------------------------------------------------------------------- /docs/img/fig3_tab_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig3_tab_1.png -------------------------------------------------------------------------------- /docs/img/fig4_alg_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig4_alg_1.png -------------------------------------------------------------------------------- /docs/img/fig4_alg_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig4_alg_2.png -------------------------------------------------------------------------------- /docs/img/fig4_alg_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig4_alg_3.png -------------------------------------------------------------------------------- /docs/img/fig4_alg_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig4_alg_4.png -------------------------------------------------------------------------------- /docs/img/fig4_alg_5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig4_alg_5.png -------------------------------------------------------------------------------- /docs/img/fig4_alg_6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig4_alg_6.png -------------------------------------------------------------------------------- /docs/img/fig5_alg_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig5_alg_1.png -------------------------------------------------------------------------------- /docs/img/fig5_tab_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig5_tab_1.png -------------------------------------------------------------------------------- /docs/img/fig6_alg_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig6_alg_1.png -------------------------------------------------------------------------------- /docs/img/fig7_alg_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig7_alg_1.png -------------------------------------------------------------------------------- /docs/img/fig7_alg_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig7_alg_2.png -------------------------------------------------------------------------------- /docs/img/fig7_alg_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig7_alg_3.png -------------------------------------------------------------------------------- /docs/img/fig8&9_alg_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig8&9_alg_1.png -------------------------------------------------------------------------------- /docs/img/fig8&9_alg_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig8&9_alg_2.png -------------------------------------------------------------------------------- /docs/img/fig11&12_alg_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apachecn/stanford-cs234-notes-zh/HEAD/docs/img/fig11&12_alg_1.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 斯坦福 cs234 强化学习中文讲义 2 | 3 | > 课程:[CS234: Reinforcement Learning Winter 2019](https://web.stanford.edu/class/cs234/schedule.html) 4 | > 5 | > 协议:[CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/) 6 | > 7 | > 做事所花费的时间总是比你预期的要长,即使你的预期中考虑了侯世达定律。——侯世达定律 8 | 9 | + [斯坦福 CS229 机器学习中文笔记](http://ai-start.com/ml2014/) 10 | + [DeepLearning.ai 深度学习中文笔记](http://ai-start.com/dl2017/) 11 | + [斯坦福 CS224n 自然语言处理中文笔记](https://github.com/apachecn/stanford-cs224n-notes-zh) 12 | + [UCB CS294-112 深度强化学习中文笔记](https://github.com/apachecn/ucb-cs294-112-notes-zh) 13 | + [ApacheCN 机器学习交流群 629470233](http://shang.qq.com/wpa/qunwpa?idkey=30e5f1123a79867570f665aa3a483ca404b1c3f77737bc01ec520ed5f078ddef) 14 | + [ApacheCN 学习资源](http://www.apachecn.org/) 15 | 16 | ## 贡献指南 17 | 18 | 项目当前处于翻译阶段,请查看[贡献指南](CONTRIBUTING.md),并在[整体进度](https://github.com/apachecn/seaborn-doc-zh/issues/1)中领取任务。 19 | 20 | > 请您勇敢地去翻译和改进翻译。虽然我们追求卓越,但我们并不要求您做到十全十美,因此请不要担心因为翻译上犯错——在大部分情况下,我们的服务器已经记录所有的翻译,因此您不必担心会因为您的失误遭到无法挽回的破坏。(改编自维基百科) 21 | 22 | ## 联系方式 23 | 24 | ### 负责人 25 | 26 | + [飞龙](https://github.com/wizardforcel):562826179 27 | + [Everglow0214](https://github.com/Everglow0214) 28 | 29 | ### 其他 30 | 31 | * 认领翻译和项目进度-地址: 32 | * 在我们的 [apachecn/stanford-cs234-notes-zh](https://github.com/apachecn/stanford-cs234-notes-zh) github 上提 issue. 33 | * 发邮件到 Email: `apachecn@163.com`. 34 | * 在我们的 [组织学习交流群](http://www.apachecn.org/organization/348.html) 中联系群主/管理员即可. 35 | 36 | ## 赞助我们 37 | 38 | ![](http://data.apachecn.org/img/about/donate.jpg) 39 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | .DS_Store 103 | 104 | # gitbook 105 | _book 106 | 107 | # node.js 108 | node_modules 109 | 110 | # windows 111 | Thumbs.db 112 | 113 | # word 114 | ~$*.docx 115 | ~$*.doc 116 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # 贡献指南 2 | 3 | > 请您勇敢地去翻译和改进翻译。虽然我们追求卓越,但我们并不要求您做到十全十美,因此请不要担心因为翻译上犯错——在大部分情况下,我们的服务器已经记录所有的翻译,因此您不必担心会因为您的失误遭到无法挽回的破坏。(改编自维基百科) 4 | 5 | 负责人: 6 | 7 | + [飞龙](https://github.com/wizardforcel):562826179 8 | 9 | ## 章节列表 10 | 11 | + [Lecture 1](https://web.stanford.edu/class/cs234/slides/lnotes_intro.pdf) 12 | + [Lecture 2](https://web.stanford.edu/class/cs234/slides/lnotes2.pdf) 13 | + [Lecture 3](https://web.stanford.edu/class/cs234/slides/lnotes3.pdf) 14 | + [Lecture 4](https://web.stanford.edu/class/cs234/slides/lnotes4.pdf) 15 | + [Lecture 5](https://web.stanford.edu/class/cs234/slides/lnotes5.pdf) 16 | + [Lecture 6](https://web.stanford.edu/class/cs234/slides/lnotes6.pdf) 17 | + [Lecture 7](https://web.stanford.edu/class/cs234/slides/lnotes7.pdf) 18 | + [Lecture 8](https://web.stanford.edu/class/cs234/slides/lnotes8.pdf) 19 | + [Lecture 9](https://web.stanford.edu/class/cs234/slides/lnotes9.pdf) 20 | + [Lecture 10](https://web.stanford.edu/class/cs234/slides/lnotes10.pdf) 21 | + [Lecture 11](https://web.stanford.edu/class/cs234/slides/lnotes11.pdf) 22 | + [Lecture 12](https://web.stanford.edu/class/cs234/slides/lnotes12.pdf) 23 | + [Lecture 13](https://web.stanford.edu/class/cs234/slides/lnotes13.pdf) 24 | + [Lecture 14](https://web.stanford.edu/class/cs234/slides/lnotes14.pdf) 25 | + [Lecture 15](https://web.stanford.edu/class/cs234/slides/lnotes15.pdf) 26 | 27 | ## 流程 28 | 29 | ### 一、认领 30 | 31 | 首先查看[整体进度](https://github.com/apachecn/stanford-cs234-notes-zh/issues/1),确认没有人认领了你想认领的章节。 32 | 33 | 然后回复 ISSUE,注明“章节 + QQ 号”。 34 | 35 | ### 二、翻译 36 | 37 | 可以合理利用翻译引擎(例如[谷歌](https://translate.google.cn/)),但一定要把它变得可读! 38 | 39 | 如果遇到格式问题,请随手把它改正。 40 | 41 | ### 三、提交 42 | 43 | + `fork` Github 项目 44 | + 将文档(**Markdown 格式**)放在`docs`中。 45 | + `push` 46 | + `pull request` 47 | 48 | 请见 [Github 入门指南](https://github.com/apachecn/kaggle/blob/master/docs/GitHub)。 49 | -------------------------------------------------------------------------------- /docs/1.md: -------------------------------------------------------------------------------- 1 | # Lecture 1 Introduction to Reinforcement Learning 2 | 3 | # 课时1 强化学习介绍 2019.01.07 4 | 5 | ## 1. 引言 6 | 7 | 在强化学习(Reinforcement Learning)中,我们考虑在没有明确的指导的情况下,学习如何通过经验(experience)去进行动作(action)的问题。强化学习行为体(agent)需要同它所处的环境(environment)交互,并从中学习如何最大化随时间累积的奖励(reward)。 8 | 9 | 可能是由于 Deep Q-Network [[1]](#ref1) 取得了巨大进步,近些年来强化学习越来越收到欢迎。人工智能的其他领域也正尝试通过借鉴与利用强化学习中的概念去取得成功。例如,AlphaGo 通过强化学习的方法在围棋领域达到了超过人类的表现 [[3]](#ref3),强化学习的概念在生成对抗网络(Generative Adversarial Networks, GAN)的训练过程中也得以应用 [[2]](#ref2)。 10 | 11 | 很多人好奇强化学习与其他机器学习方式的不同之处。在有监督学习(Supervised Learning)中,已知包括了例子和标记的数据集,对于分类问题,例子对应的正确的标签已知;对于回归问题,例子对应的正确的输出已知。相反,无监督学习(Unsupervised Learning)指的是利用标记未知的数据集,找到数据中隐含的关系。在强化学习中,我们需要做出决策并比较可采取的动作,而不是做出预测。强化学习行为体可以和环境交互,并在每次交互中获得一些立即的、局部反馈信号,这些信号通常被称作奖励。但是,行为体不知道它采取的动作是否是它能够选择的“最好的“动作,它必须通过某种方式去学习通过选择一些动作来最大化长期的累积的奖励。因此,由于奖励信号提供的反馈很弱/不完整,我们可以认为强化学习介于有监督学习和无监督学习之间:有监督学习中的有标记的数据提供强有力的反馈,而无监督学习中没有标记或反馈。 12 | 13 | 强化学习引入了许多需要克服的困难,并且我们可能要在这些挑战中进行权衡。行为体必须能够优化它的动作以使其收到的奖励达到最大,但是,因为它需要通过与环境交互来学习,探索(exploration)也是需要的。这自然就导致了探索与利用(exploitation)的权衡,在这种情况下,行为体需要决定是寻找新的、更好的策略但要承担获得较低奖励的风险,还是利用它已经知道的策略。另一个我们需要面对的问题是,行为体是否能够归纳它的经验?也就是说,它能否学习到一些曾经没出现过的行为是好是坏。最后,我们还需要考虑行为体的延迟后果,也就是说,如果它获得了一个高回报,是因为它刚刚采取的行为,还是因为更早采取的行动。 14 | 15 | ## 2. 强化学习概述 16 | 17 | ### 2.1 序列决策制定 18 | 19 | 通常我们考虑制定一个好的决策序列的问题。为了标准化这个问题,在离散的设置中,一个行为体进行一个动作(actions)序列 $\lbrace a_t \rbrace$,观测到一个观测值(observations)序列 $\lbrace o_t \rbrace$ 并获得一个奖励(rewards)序列 $\lbrace r_t \rbrace$。我们定义在时刻 $t$ 的历史(history)为 $h_t=(a_1,o_1,r_1,...,a_t,o_t,r_t)$,行为体选择的下一个动作可以被视为历史的函数,也就是说,$a_{t+1}=f(h_t)$,并且序列决策制定的问题可以被认为是如何恰当地定义并计算这个函数。 20 | 21 |
22 | 23 |
24 | 图 1:行为体与环境交互。 25 |
26 |
27 | 28 | 当序列决策制定过程的模型已知时,在一些确定的和有限的设定下,类似 A* 搜索 (A* search)和最大最小(minimax)的 AI 技术可以被应用于寻找最优动作序列。 29 | 30 | 然而,如果存在非常多(或无限)可能的状态,或模型中有一定的随机性,暴力的搜索变得不再可行。在这样的设定下,我们有必要结合泛化去使得任务可行,就像在课堂中看到的 Atari 例子那样。此外,如果没有彻底搜索的能力,行为体必须决定如何有策略性地做出动作去平衡短期的和长期的奖励。 31 | 32 | ### 2.2 模型建立 33 | 34 | 定义 $S$ 为可能的状态的集合,$s_t$ 为观测到的状态序列,观测值以时间为下标,$A$ 为可能的动作的集合,$P(s_{t+1}|s_t,a_t,...,s_1,a_1)$ 为历史状态和历史动作的函数,表示在 $S$ 上的状态转移(transition dynamics)概率分布。在强化学习中,我们通常假设马尔可夫性质(Markov Property): 35 | $$ 36 | P(s_{t+1}|s_t,a_t,...,s_1,a_1)=P\left( s_{t+1}|s_t,a_t \right), 37 | \tag{1} 38 | $$ 39 | 在实践中,这一性质足以满足我们的要求。确保马尔可夫性质成立的一个小技巧是使用历史 $h_t$ 作为我们的状态。 40 | 41 | 通常我们认为奖励 $r_t$ 在状态转换时被获得 $s_t\stackrel{a_t}{\longrightarrow}s_{t+1}$,奖励函数(reward function)被用来预测奖励:$R(s,a,s')=\mathbb{E}[r_t|s_t=s,a_t=a,s_{t+1}=s']$。我们通常将奖励函数定义为以下的形式:$R(s)=\mathbb{E}[r_t|s_t=s]$ 或 $R(s,a)=\mathbb{E}[r_t|s_t=s,a_t=a]$。奖励函数有时也会存在退化情况,也就是说在给定 $s_t=s$ 时,$r_t$ 是一个固定值。 42 | 43 | 模型由上述状态转移和奖励函数组成。 44 | 45 | ### 2.3 强化学习行为体的组成 46 | 47 | 我们定义行为体状态(agent state)为历史的函数 $s_t^a=g(h_t)$。强化学习行为体通常包括以下三项中的一项或多项:策略(policy),值函数(value function)和可选的模型(model)。策略 $\pi$ 是从行为体状态到一个动作的映射 $\pi(s_t^a)\in A$,有时策略是在动作的随机分布 $\pi(a_t|s_t^a)$,当行为体要采取一个动作并且 $\pi$ 是随机的时,行为体以概率 $P(a_t=a)=\pi(a|s_t^a)$ 选择一个动作 $a\in A$。给定策略 $\pi$ 和衰减因子(discount factor)$\gamma\in[0,1]$,值函数(value function)$V^{\pi}$ 被定义为衰减奖励总额的期望 48 | $$ 49 | V^{\pi}(s)=\mathbb{E}_ {\pi}[r_t+\gamma r_{t+1}+\gamma^2r_{t+2}+...|s_t=s], 50 | \tag{2} 51 | $$ 52 | 这里 $\mathbb{E}_{\pi}$ 表示对通过使用策略 $\pi$ 遇到的状态求期望,衰减因子 $\gamma$ 用来对立即奖励和延迟奖励加权。最后,一个强化学习行为体可能有类似2.2节定义的模型,如果行为体拥有模型,我们称之为有模型(model-based)行为体;如果它不包含模型,则称之为无模型(model-free)行为体。 53 | 54 | 到目前为止,我们已经在一个非常常规的环境中考虑了定义,但我们尚未对 $o_t$ 和 $s_t$ 的关系做出任何假设。我们称 $o_t\neq s_t$ 的情况为部分可观测的(partially observable)。在部分可观测的设定中,为了定义可信状态(belief state)$s_t^a$,强化学习算法通常使用基于真实世界状态的概率分布。 55 | 56 | 然而,对于本课程的大部分内容,我们考虑完全可观测(fully observable)的情况,也就是说 $o_t=s_t$ 而且我们假设 $s_t^a=s_t$。 57 | 58 | ### 2.4 强化学习行为体的分类 59 | 60 | 有很多种方式进行分类,如表 1,并且在每种分类下,行为体的种类并不一定单一,例如,演员-评论家(actor critic)行为体也可以是无模型的。对行为体分类的方法概述如图 2 所示。 61 | 62 | 71 | 72 |
73 | 74 |
75 | 表 1:强化学习行为体分类,√ 表示行为体包含这一部分,X 表示行为体一定不包含这一部分,? 表示行为体可能含有这一部分。 76 |
77 |
78 | 79 |
80 | 81 |
82 | 图 2:强化学习行为体分类 [4](#ref4)。 83 |
84 | 85 | ### 2.5 连续空间 86 | 87 | 为简单起见,我们的讨论只集中在了具有离散时间步的离散状态和动作空间。然而,有许多应用,特别是在机器人和控制领域,最适合用连续状态和动作空间进行建模。对上述讨论进行推广,可以使其适用于这些连续空间的情况。 88 | 89 | ## 参考文献 90 | 91 | 1. V. Mnih et al., "Human-level control through deep reinforcement learning," *Nature* 518(7540): 529-533, 2015. 92 | 93 | 2. D. Pfau, and O. Vinyals, "Connecting generative adversarial networks and actor-critic methods," *arXiv preprint arXiv: 1610.01945*, 2016. 94 | 95 | 3. D. Silver et al., "Mastering the game of Go with deep neural networks and tree search," *Nature* 529(7587): 484-489, 2016. 96 | 97 | 4. D. Silver. "[Reinforcement Learning](http://www0.cs.ucl.ac.uk/staff/d.silver/web/Teaching_files/intro_RL.pdf)," 15 Jan. 2016. Reinforcement Learning, UCL. -------------------------------------------------------------------------------- /docs/14.md: -------------------------------------------------------------------------------- 1 | # Lecture 14 Model Based RL, Monte-Carlo Tree Search 2 | 3 | # 课时14 基于模型的强化学习,蒙特卡洛树搜索 2019.03.06 4 | 5 | ## 1. 介绍(Introduction) 6 | 7 | 本节课我们讨论基于模型的强化学习与基于仿真的树搜索方法。到目前为止我们已经讨论了从经历中尝试学习值函数或策略的方法,与这些方法相反,基于模型的方法首先从经历中学习环境的模型,然后使用这个模型做出规划和行动。基于模型的方法在某些情况下有更高的采样效率和更快的收敛速度。我们还将讨论 MCTS 及其变体,它们可以用于针对给出的模型做出规划。MCTS 是 AlphoGo 成功背后的主要思想之一。 8 | 9 |
10 | 11 |
12 | 图 1:学习、规划与动作执行之间的关系。 13 |
14 | 15 | ## 2. 模型学习(Model Learning) 16 | 17 | 我们用 $$ 来表示一个 MDP 的模型,由 $\mu$ 参数化。在模型学习中,我们假设状态空间 $S$ 和 动作空间 $A$ 已知,并且通常还假设状态转移与奖励是相互独立的,即 18 | 19 | $$ 20 | P[s_{t+1},r_{t+1}|s_t,a_t] = P[s_{t+1}|s_t,a_t]P[r_{t+1}|s_t,a_t]。 21 | $$ 22 | 23 | 因此,模型学习包括两个部分,分别为奖励函数 $R(\cdot|s,a)$ 和转移分布 $P(\cdot|s,a)$。 24 | 25 | 给定一个真实轨迹的集合 $\{S_t^k,A_t^k,R_t^k,...,S_T^k\}_{k=1}^{K}$,模型学习可以被视为一个监督学习问题,学习奖励函数 $R(s,a)$ 是一个回归问题,而学习转移函数 $P(s'|s,a)$ 是一个密度估计问题。首先我们选取一类合适的参数化模型,如查表模型、线性期望、线性高斯、高斯过程、深度神经网络等,然后我们选择一个恰当的损失函数,如均方误差、KL 散度等,通过最小化这个损失来优化参数。 26 | 27 | ## 3. 规划(Planning) 28 | 29 | 给定一个学习到的环境的模型,规划可以由基于值的方法、策略搜索的方法或树搜索的方法来实现。 30 | 31 | 一种比较的规划方法的思路为:仅使用该模型来生成采样轨迹,并使用 Q-学习、蒙特卡洛控制或 SARSA 等方法进行控制。这种基于样本的规划方法通常更具数据效率。 32 | 33 | 学习得到的模型可能是不准确的,因此,通过规划所学习的缩略也可能是次优的,即基于模型的 RL 的质量依赖于所学习的模型的质量。基于探索/利用的技术可用于在规划时明确解释模型中的这种不确定性。或者,如果我们确定模型在某些情况下是错误的,无模型 RL 方法也可以作为我们的后备方案。 34 | 35 | ## 4. 基于仿真的搜索(Simulation Based Search) 36 | 37 | 不管给出的模型是学习到的近似模型,还是像围棋这样的精确模型,这些方法都试图基于向前搜索或模拟来寻找最优动作。搜索树以当前的状态为根,使用模型生成其他节点。因为不需要求解整个 MDP 而只需要从当前状态开始求解子 MDP,所以这种方法可以节省大量的资源。一般来说,当我们收集了仿真经历 $\{S_T^K,A_t^k,R_t^k,...,S_T^K\}_{k=1}^{K}$后,我们可以将无模型方法应用于控制,如蒙特卡洛给出了蒙特卡洛搜索算法或 SARSA 给出了 TD 搜索算法。 38 | 39 | 更具体地说,在一个简单的 MC 搜索算法中,给出了一个模型 M 和一个模拟策略 $\pi$,对于每个动作 $a\in A$,我们模拟 $K$ 个 $\{S_T^K,a,R_t^k,...,S_T^K\}_{k=1}^{K}$ 形式的片段(在第一动作后遵循策略 $\pi$)。$Q(s_t,a)$ 值被估计为上述轨迹的平均回报,随后我们选择最大化这个估计的 $Q(s_t,a)$ 值的动作。 40 | 41 | ### 4.1 蒙特卡洛树搜索(Monte Carlo Tree Search) 42 | 43 | 这类算法基于两个原则:(1)状态的真实价值可以通过随机模拟的平均回报值来估计;(2)这些值可以用于迭代地调整策略,从而使我们能够关注搜索空间的高价值区域。 44 | 45 | 我们逐步构造一个以当前节点为根的部分搜索树。树由对应于状态 $s$ 的节点组成,此外,每个节点还存储统计信息,如总访问次数 $N(s)$、每个状态-动作对的访问次数 $N(s,a)$ 以及蒙特卡洛 $Q(s,a)$ 值估计等。一种典型的方法是一直构造这种树,直到某些预先定义的计算耗尽,价值估计会变得越来越准确。每个迭代大致可以分为四个阶段: 46 | 47 | 1. 选择:从根节点开始,我们在树中循环地选择子节点,直到达到非终结叶节点; 48 | 49 | 2. 扩展:将被选中的叶节点添加到搜索树中; 50 | 51 | 3. 仿真:从这一节点开始仿真来生成输出的估计值; 52 | 53 | 4. 反向传播:通过反向沿着根到选中叶节点的路径,更新遇到的节点的统计信息,从而将仿真中获得的值在树中反向传播。 54 | 55 | MCTS 的变体通常包含对涉及的两个主要策略的修改: 56 | 57 | $\bullet$ 树策略:根据存储的统计信息为树的节点选择动作,变体包括贪婪策略、UCB 58 | 59 | $\bullet$ 仿真策略:用于树中叶节点的仿真,变体包括随机模拟、AlphaGo 的默认策略网络 60 | 61 |
62 | 63 | 这些步骤总结在了算法 1 中,我们从当前状态开始,迭代地生长搜索树,在每次迭代时使用树策略来选择要模拟的叶节点,然后对仿真结果进行反向传播,最后输出从根节点估计得到最大值的动作。 64 | 65 | 该算法的一个简单变体如算法 2 所示,在第一阶段中贪婪地在树节点之间选择动作,并在仿真阶段使用随机策略进行仿真。 66 | 67 |
68 | 69 | 还有一些基于上述方案的修改,如通过添加有限的节点集到树、智能修剪来提升存储器的使用,通过使用节点中存储的更复杂的统计数据来改善树策略。 70 | 71 | 从[图 2](#fig2) 中可以看到整个的运行过程。我们从根节点开始,仿真一个轨迹,图中的情况下,这个轨迹返回一个 1 的奖励;然后我们使用树策略,即贪婪地选择要添加到树的节点,然后从它开始使用仿真(默认)策略仿真一个片段,这个片段返回一个 0 的奖励,我们更新树节点中的统计信息;然后重复此过程。为了检查你的理解,你应该在下面的示例中验证统计信息已经正确更新,以及树策略选择了正确的节点来扩展。 72 | 73 | MCTS 的主要优点包括: 74 | 75 | $\bullet$ 树结构使它具有很大的并行性 76 | 77 | $\bullet$ 状态评估是动态的,即从当前状态开始解决 MDP 问题,与 DP 不同 78 | 79 | $\bullet$ 不需要模型,只需要样本 80 | 81 | $\bullet$ 有效地结合规划和抽样来打破维度灾难(如围棋) 82 | 83 |
84 | 85 |
86 | 图 2:通用 MCTS 示意图。每个状态有两个可能的动作(左/右),每个仿真的结果为 $1$ 或 $0$。每次迭代添加一个新的节点(星星符号)到搜索树中,然后更新搜索树中每个节点的值以及总的访问次数。 87 |
88 | 89 | ### 4.2 置信上界树搜索(Upper Confidence Tree Search) 90 | 91 | 类似多臂老虎机,使用贪婪策略作为树策略往往是次优的,这会使我们即使是在一个糟糕的结果之后也避免采取行动,尽管其真实价值存在着很大的不确定性。作为一个例子,考虑[图 2](#fig2) 中最右边的节点,我们从该节点执行一个单独的仿真,收到一个值为 0 的奖励,然后再也不访问该节点,即使这个奖励可能只是因为运气不好(才是 0)。为了解决这个问题,我们可以使用 UCB 算法将不确定性下的乐观原则应用于 MCTS,更具体地说,树策略选择最大化动作值置信上界($Q(s,a)+\sqrt{\frac{2\log N(s)}{N(s,a)}}$)的动作,而非贪婪地选择动作。 92 | 93 | 算法 3 为 UCT 中使用树策略的伪代码,该算法可以插入到前面描述的通用 MCTS 算法。 $nextState(s,a)$ 函数使用了 MDP 模型在从状态 $s$ 选择动作 $a$ 时采样下一状态。 94 | 95 |
96 | 97 | ## 5. 案例学习:围棋(Case Study: Go) 98 | 99 | 围棋是世界上最古老的棋类游戏,解决这一问题一直是人工智能面临的一个长期挑战,而在 AlphaGo 之前,传统的游戏树搜索算法未能达到专业的人类级别的性能。围棋是一个在 $19\times 19$ 板(也有更小的板)上进行的双人游戏(黑子/白子,B/W),黑子和白子接连被放在板上,游戏的主要目标是包围和占领领地,另外,被对手包围的棋子会被移除。 100 | 101 |
102 | 103 |
104 | 图 3:围棋。 105 |
106 | 107 | 最简单的奖励函数可以按照如下规则设置:如果最终状态黑子胜则奖励为 $+1$,如果白子胜则为 $0$,而其他状态均为 $0$。那么黑子玩家的目的则为最大化奖励而白子玩家则要尽量最小化奖励。给定一个策略 $\pi=<\pi_{B},\pi_{W}>$(分别对黑子玩家和白子玩家),值函数为 $V_{\pi}(s)=\mathbb{E}_ {\pi}[R_{T}|s]=P[Black wins|s]$,最优值函数为 $V^{\ast}(s)=\mathop{\max}_ {\pi_{B}}\mathop{\min}_ {\pi_{W}}V_{\pi}(s)$。 108 | 109 | ### 5.1 基于 MCTS 的围棋(MCTS for Go) 110 | 111 | 作为一个双人游戏,围棋需要一些相当自然的扩展以应用前面提到的 MCTS 算法。我们现在构建一个极小极大树,白色节点寻求最小化奖励,黑色节点寻求最大化奖励。我们在黑色节点使用前面提到的 UCB,在白色节点使用 LCB(置信下界)即 $\mathop{\min}_{a}Q(s,a)-\sqrt{\frac{2\log N(s)}{N(s,a)}}$(因为它们寻求最小化奖励)。 112 | 113 | 考虑[图 4](#fig4)(a)树中的状态以及节点中记录的统计信息(胜利/总次数),不同的颜色代表不同的玩家。算法或树策略的第一阶段在节点中使用这些统计信息,将每个节点视作一个独立的 MAB 实例,并从根节点开始使用 UCB(LCB)依次选择动作,如粗体箭头所示($c=2$)。 114 | 115 | 一旦我们到达了树的叶节点,如[图 4](#fig4)(b)所示,我们就使用仿真策略来模拟一次游戏。然后,这次游戏的结果通过树反向传播([图 4](#fig4)(c)),同时我们更新统计信息。 116 | 117 | 继续执行这个过程直到结束,然后最佳动作便可以得到。有关详细的伪代码,可以参考 [[4]](#ref4),Python 实现可以参考 [[3]](#ref3)。 118 | 119 |
120 | 121 |
122 | 图 4:基于 MCTS 的围棋。(a)UCB1 算法在每个步骤中选择的位置和移动都用粗体进行表示。每个圆圈包含了获胜次数/游戏次数。(b)树底部标记为 $1/1$ 的位置下没有进一步的统计记录,因此我们选择一个随机的动作并为其添加一个新纪录(初始化为 $0/0$)。添加新纪录后,蒙特卡洛仿真开始(虚线箭头)。仿真中的动作可以是完全随机的,也可以为随机性添加权重。(c)仿真结束后,路径中的所有记录都将更新(粗体数字)。(图出处:[[3]](#ref3)) 123 |
124 | 125 | AlphaGo [[1]](#ref1) 在仿真阶段使用了一个深度策略网络,这使得仿真比仅仅使用随机仿真更加真实。在围棋这种复杂的游戏中,仿真直到结束是不合适的,AlphaGo 会提前停止仿真,同时还使用了一个价值网络来获得获胜概率。最近,AlphaGo Zero [[2]](#ref2) 被提出,它使用一个单一的网络来同时输出策略和价值函数,并且只使用自玩来训练而没有内置的专家知识。AlphaGo Zero 的表现比 AlphaGo 更加令人印象深刻。 126 | 127 | ## 参考文献 128 | 129 | 1. D. Silver et al, "Mastering the game of Go with deep neural networks and tree search," *Nature*, 2016. 130 | 131 | 2. D. Silver et al, "Mastreing the game of Go without human knowledge," *Nature*, 2017. 132 | 133 | 3. J. Bradberry, "[Introduction to Monte Carlo tree search](https://jeffbradberry.com/posts/2015/09/intro-to-monte-carlo-tree-search/)," 2015. 134 | 135 | 4. S. Gelly, and D. Silver, "Monte-Carlo tree search and rapid action value estimation in computer Go," *Artificial Intelligence*, 2011. -------------------------------------------------------------------------------- /docs/7.md: -------------------------------------------------------------------------------- 1 | # Lecture 7 Imitation Learning 2 | 3 | # 课时7 模仿学习 2019.01.30 4 | 5 | ## 8. 介绍(Introduction) 6 | 7 | 强化学习中有一些必须克服的理论和实践上的障碍,包括优化、延迟的影响、如何探索以及如何归纳。然而,重要的是,我们希望能够处理上述所有困难,同时保证数据效率和计算效率。 8 | 9 | 我们将在课程后后面部分讨论有效探索的一般方法,这些方法能够处理一般的 MDPs。但是,如果我们知道问题的结构,或者我们有可以使用的外部知识,我们就可以更有效地探索。本次课我们讨论如何模仿和学习人类(专家)在任务上的行为。 10 | 11 | ## 9. 模仿学习(Imitation Learning) 12 | 13 | 过去我们的目标是从奖励中学习策略,这些奖励通常非常稀疏(sparse),例如,一个简单的奖励信号可能是行为体是否赢得了一场游戏。这种方法在数据便宜且易于收集的情况下是成功的,然而,当数据收集速度较慢、必须避免失败(如自动驾驶)或必须保证安全时,这种方法就不是可行的。 14 | 15 | 缓解稀疏奖励问题的一种途径是手动设计在时间上密集的奖励函数,然而,这种方法需要人来手工设计奖励函数,并记住所需的行为,因此,最好通过模仿执行相关任务的行为体来学习。 16 | 17 | ### 9.1 从示范中学习(Learning from Demonstration) 18 | 19 | 一般来说,专家们提供了一组演示轨迹(demonstration trajectories),这些轨迹是状态和动作的序列。更正式地,我们假设已知: 20 | 21 | $\bullet$ 状态空间,动作空间; 22 | 23 | $\bullet$ 状态转移模型 $P(s'|s,a)$; 24 | 25 | $\bullet$ 奖励函数 $R$ 未知; 26 | 27 | $\bullet$ 一个或多个演示 $(s_0,a_0,s_1,a_1,...)$,这里动作遵循策略 $\pi^{\ast}$。 28 | 29 | ## 10. 行为克隆(Behavioral Cloning) 30 | 31 |
32 | 我们能否通过有监督学习来学习策略? 33 |
34 |
35 | 36 | 行为克隆是为了通过有监督学习去学习策略。具体来说,我们固定一个策略的类,目的是在已知数据 ${(s_0,a_0),(s_1,a_1),...}$ 时,学习一个将状态映射到动作的策略。一个值得注意的例子是 ALVINN,它学会了将传感器输入映射到转向角。 37 | 38 | 这种方法的一个挑战是状态空间的数据不是 i.i.d.(独立同分布,independent and identically distributed)的,i.i.d. 的假设是有监督学习文献和理论的标准。然而,在 RL 环境中,错误是复杂的;这些错误会随着事件过程不断累积。用于被学习的策略的数据会紧紧围绕专家的轨迹,如果犯了一个错误,而且这个错误使得行为体处于专家没有访问过的状态,那么行为体就没有可以从中学习策略的数据。在这种情况下,与标准 RL 中的线性标度相反,误差在片段中呈四次标度(the error scales quadratically)。 39 | 40 | ### 10.1 DAGGER:数据聚集(DAGGER: Dataset Aggregation) 41 | 42 | 数据聚集(DAGGER,算法 1,[[1]](#ref1))的目的是通过为新访问的状态添加数据来缓解错误的复杂化问题。与假设存在预先定义好的专家演示相反,我们假设我们可以通过专家来生成更多的数据。当然,这么做的局限之处在于,专家必须能够提供标签,有时还应该是实时的。 43 | 44 |
45 | 46 | ## 11. 逆强化学习(Inverse Reinforcement Learning, IRL) 47 | 48 |
49 | 我们能否复原奖励函数 $R$? 50 |
51 |
52 | 53 | 在逆强化学习中(也被称为逆最优控制(inverse optimal control)),我们的目标是基于专家演示来学习奖励函数(未知)。如果不假设演示的最优性,这个问题是很难解决的,因为任何奖励函数都可能产生观测到的轨迹。 54 | 55 | ### 11.1 线性特征奖励的逆强化学习(Linear Feature Reward Inverse RL) 56 | 57 | 我们考虑表示为特征的线性组合的奖励: 58 | $$ 59 | R(s)=\omega^{\text{T}}x(s), 60 | \tag{1} 61 | $$ 62 | 63 | 这里 $\omega\in \mathbb{R}^n$,$x:S\rightarrow \mathbb{R}^n$。在这种情况下,IRL 问题是给定一组演示时,确定权重向量 $\omega$。由此生成的关于策略 $\pi$ 的价值函数可以表示为: 64 | $$ 65 | V^{\pi}(s)=\mathbb{E}_ {\pi}[\sum_{t=0}^{\infty}\gamma^tR(s_t)|s_0=s] 66 | \tag{2} 67 | $$ 68 | 69 | $$ 70 | =\mathbb{E}_ {\pi}[\sum_{t=0}^{\infty}\gamma^t\omega^{\text{T}}x(s_t)|s_0=s] 71 | \tag{3} 72 | $$ 73 | 74 | $$ 75 | =\omega^{\text{T}}\mathbb{E}_ {\pi}[\sum_{t=0}^{\infty}\gamma^tx(s_t)|s_0=s] 76 | \tag{4} 77 | $$ 78 | 79 | $$ 80 | =\omega^{\text{T}}\mu(\pi), 81 | \tag{5} 82 | $$ 83 | 84 | 这里 $\mu(\pi|s_0=s)\in\mathbb{R}^n$ 为基于策略 $\pi$ 的状态特征 $x(s)$ 的衰减加权和。注意: 85 | $$ 86 | \mathbb{E}_ {\pi^{\ast}}[\sum_{t=0}^{\infty}\gamma^tR^{\ast}(s_t)|s_0=s]\geq \mathbb{E}_ {\pi}[\sum_{t=0}^{\infty}\gamma^tR^{\ast}(s_t)|s_0=s] \quad \forall\pi, 87 | \tag{6} 88 | $$ 89 | 90 | 这里 $R^{\ast}$ 为最优奖励函数。因此,如果专家演示是最优的(即动作是基于最优策略生成的),那么我们找到满足下式的 $\omega^{\ast}$ 就能够确定 $\omega$: 91 | $$ 92 | \omega^{\ast\text{T}}\mu(\pi^{\ast}|s_0=s) \geq \omega^{\ast\text{T}}\mu(\pi|s_0=s) \quad \forall\pi,\forall s。 93 | \tag{7} 94 | $$ 95 | 96 | 从某种意义上说,我们希望找到奖励函数的一种参数化形式,使得专家策略的表现优于其他策略。 97 | 98 | ## 12. 学徒制学习(Apprenticeship Learning) 99 | 100 |
101 | 我们能否通过复原的奖励函数来生成好的策略? 102 |
103 |
104 | 105 | 为使策略 $\pi$ 表现得和专家策略 $\pi^{\ast}$ 一样好,只需要这个策略的特征的衰减加权和的期望与专家策略相匹配 [[2]](#ref2)。更准确地说,如果 106 | $$ 107 | \lVert \mu(\pi|s_0=s)-\mu(\pi^{\ast}|s_0=s) \rVert _1 \leq \epsilon, 108 | \tag{8} 109 | $$ 110 | 111 | 那么对于所有满足 $\lVert \omega \rVert _\infty \leq 1$ 的 $\omega$,根据 Cauchy-Schwartz 不等式,我们有 112 | $$ 113 | |\omega^{\text{T}}\mu(\pi|s_0=s)-\omega^{\text{T}}\mu(\pi^{\ast}|s_0=s)|\leq\epsilon。 114 | $$ 115 | 116 | 由这一结论我们可以得到算法 2 ,通过算法 2 来学习一个表现得像专家策略一样好的策略(细节请参考 [[2]](#ref2))。 117 | 118 |
119 | 120 | 在实践中,这种方法存在一些问题: 121 | 122 | $\bullet$ 如果专家策略是次优的,那么得到的策略就是一些武断的策略的混合,这些策略在其凸包(convex hull)中具有专家策略的性质。在实践中,实践者可以选择这个集合中的最佳策略并选择对应的奖励函数; 123 | 124 | $\bullet$ 这种方法需要在给定奖励函数的情况下,能够计算出最优策略,然而这个要求可能是昂贵或不可能的; 125 | 126 | $\bullet$ 有无穷多的奖励函数具有相同的最优策略,并且有无穷多的策略能够匹配特征。 127 | 128 | ### 12.1 最大熵的逆强化学习(Maximum Entropy Inverse RL) 129 | 130 | 为解决歧义问题(problem of ambiguity),Ziebart 等人 [[3]](#ref3) 引入了最大熵(Maximum Entropy, MaxEnt)IRL。考虑确定性 MDP 中所有可能的 $H$-步轨迹,对于线性奖励模型,一个策略完全由其在轨迹上的分布来描述。鉴于此,给定 $m$ 个分布,我们应该选择哪种策略? 131 | 132 | 我们再次假设奖励函数为特征的线性函数 $R(s)=\omega^{\text{T}}x(s)$。记轨迹 $j$ 为 $\tau_j$,我们可以把这个轨迹的特征写成: 133 | $$ 134 | \mu_{\tau_j}=\sum_{s_i\in\tau_j}x(s_i)。 135 | \tag{9} 136 | $$ 137 | 138 | 对 $m$ 个轨迹求平均,我们可以将平均特征写为: 139 | $$ 140 | \tilde{\mu}=\frac{1}{m}\sum_{j=1}^{m}\mu_{\tau_j}。 141 | \tag{10} 142 | $$ 143 | 144 | 最大熵定理(Principle of Maximum Entropy)[[4]](#ref4) 鼓励选择这样一个分布:除了与示范数据集中的特征期望相匹配外,没有其他偏好,即 145 | $$ 146 | \mathop{\max}_P -\sum _{\tau}P(\tau)\log P(\tau), 147 | \tag{11} 148 | $$ 149 | 150 | $$ 151 | \text{s.t.} \quad \sum_{\tau}P(\tau)\mu_{\tau}=\tilde{\mu}, 152 | \tag{12} 153 | $$ 154 | 155 | $$ 156 | \sum_{\tau}P(\tau)=1。 157 | \tag{13} 158 | $$ 159 | 160 | 在线性奖励的情况下,这相当于指定权重 $\omega$,使 $\omega$ 产生有最大熵的策略同时与特征期望匹配。在观测数据的特征约束下,路径分布的熵的最大化意味着在最大熵(指数族)分布下观测数据可能性的最大化: 161 | $$ 162 | P(\tau_j|\omega)=\frac{1}{Z(\omega)}\text{exp}(\omega^{\text{T}}\mu_{\tau_j})=\frac{1}{Z(\omega)}\text{exp}(\sum_{s_i\in\tau_j}\omega^{\text{T}}x(s_i)), 163 | $$ 164 | 这里, 165 | 166 | $$ 167 | Z(\omega,s)=\sum_{\tau{s}}\text{exp}(\omega^{\text{T}}\mu_{\tau_s})。 168 | $$ 169 | 170 | 这就产生了对低成本路径的强烈偏好,而等成本路径是同等可能的。许多我们关注的 MDPs 是随机的,在这些情况下,路径的分布取决于奖励权重和动态 171 | $$ 172 | P(\tau_j|\omega,P(s'|s,a))\approx \frac{\text{exp}(\omega^{\text{T}}\mu_{\tau_j})}{Z(\omega,P(s'|s,a))} \prod_{s_i,a_i\in\tau_j}P(s_{i+1}|s_i,a_i)。 173 | $$ 174 | 175 | 通过最大化数据的可能性来学习权重 $\omega$: 176 | 177 | $$ 178 | \omega^{\ast}=\mathop{\arg\max}_ {\omega}L(\omega)=\mathop{\arg\max}_ {\omega}\sum_{\text{examples}}\log P(\tau|\omega)。 179 | $$ 180 | 181 | 梯度为经验特征的期望与学习者的特征期望之间的差异,学习者的特征期望可以表示为期望的状态访问频率: 182 | $$ 183 | \nabla L(\omega)=\tilde{\mu}-\sum_{\tau} P(\tau|\omega)\mu_{\tau} = \tilde{\mu}-\sum_{s_i} D(s_i)x(s_i), 184 | $$ 185 | 186 | 这里 $D(s_i)$ 表示状态访问频率。这种方法有深远的影响,它提供了一种在许多可能的奖励函数中进行选择的原则性方法。然而,算法的原始公式需要状态转移模型或能够通过仿真/实际实验来收集状态转移模型的样本。 187 | 188 |
189 | 190 | ## 参考文献 191 | 192 | 1. S. Ross, G. J. Gordon, and J. A. Bagnell, "A reduction of imitation learning and structured prediction to no-regret online learning," *Proceedings of the 14th International Conference on Artificial Intelligence and Statistics*, 2011. 193 | 194 | 2. P. Abbeel, and A. Y. Ng, "Apprenticeship learning via inverse reinforcement learning," *Proceedings of the 21st International Conference on Machine Learning*, 2004. 195 | 196 | 3. B. D. Ziebart et al., "Maximum entropy inverse reinforcement learning," *Association for the Advancement of Artificial Intelligence*, 2008. 197 | 198 | 4. E. T. Jaynes, "Information theory and statistical mechanics," *Physical Review*, 1957. -------------------------------------------------------------------------------- /docs/5.md: -------------------------------------------------------------------------------- 1 | # Lecture 5 Value Function Approximation 2 | 3 | # 课时4 值函数近似 2019.01.23 4 | 5 | ## 7. 介绍(Introduction) 6 | 7 | 到目前为止我们介绍了基于查表(lookup table)的值函数,这里每个状态都有一个对应条目,$V(s)$,或每个状态-行为对都有一个对应的条目,$Q(s,a)$。然而,这种方法可能不适合处理动作空间或状态空间非常大、或那些我们希望快速学习每个状态估计值的情况。函数近似是解决这个问题的常用办法: 8 | $$ 9 | v_{\pi}(s) \approx \hat{v}(s,\mathbf{w}) \text{ or } q_{\pi}(s,a) \approx \hat{q}(s,a,\mathbf{w}), 10 | $$ 11 | 这里 $\mathbf{w}$ 通常指近似函数的参数。以下是常见的近似函数: 12 | 13 | $\bullet$ 特征的线性组合(Linear combinations of features) 14 | 15 | $\bullet$ 神经网络(Neural networks) 16 | 17 | $\bullet$ 决策树(Decision trees) 18 | 19 | $\bullet$ 最近邻(Nearest neighbors) 20 | 21 | $\bullet$ 傅立叶/小波基(Fourier / wavelet bases) 22 | 23 | 我们讲进一步讨论可导近似函数:线性特征表示和神经网络,接下来的部分会提到要求函数可导的原因。 24 | 25 | ## 8. 线性特征表示(Linear Feature Representations) 26 | 27 | 在线性特征表示中,我们用一个特征向量去表示一个状态: 28 | $$ 29 | x(s)=[x_1(s)\ x_2(s)\ ...\ x_n(s)], 30 | $$ 31 | 32 | 然后我们用特征的线性组合来近似值函数: 33 | $$ 34 | \hat{v}(s,\mathbf{w})=x(s)^{\text{T}}\mathbf{w}=\sum_{j=1}^n x_j(s)\mathbf{w}_j。 35 | $$ 36 | 37 | 我们定义(二次的)目标函数(也被称为损失函数)为: 38 | $$ 39 | J(\mathbf{w})=\mathbb{E}_ {\pi}[(v_{\pi}(s)-\hat{v}(s,\mathbf{w}))^2]。 40 | $$ 41 | 42 | ### 8.1 梯度下降(Gradient Descent) 43 | 44 | 梯度下降(gradient descent)是最小化上述目标函数的一种常用方法。图 1 提供了直观的说明:我们从某个特定的点 $x_0$ 开始,对应于参数 $w$ 的某个初始值;然后计算在 $x_0$ 处的梯度,它告诉我们目标函数增加速度最快的方向;为了最小化我们的目标函数,我们沿着梯度向量的负方向以一定的步长前进,到达 $x_1$;将这个过程重复进行,直到到达某个收敛标准。 45 | 46 |
47 | 48 |
49 | 图 1:梯度下降可视化。我们希望求出使目标函数最小的点,我们跟着红色箭头走来实现这个目标,红色箭头指向梯度的负方向。 50 |
51 | 52 | 这个方法可以被总结为: 53 | $$ 54 | \nabla_{\mathbf{w}}J(\mathbf{w})=[\frac{\partial J(\mathbf{w})}{\partial \mathbf{w}_1}\ \frac{\partial J(\mathbf{w})}{\partial \mathbf{w}_2}\ ...\ \frac{\partial J(\mathbf{w})}{\partial \mathbf{w}_n}] \quad\text{计算梯度}; 55 | $$ 56 | 57 | $$ 58 | \Delta\mathbf{w}=-\frac{1}{2}\alpha\nabla_{\mathbf{w}}J(\mathbf{w}) \quad\text{用梯度下降计算更新步长}; 59 | $$ 60 | 61 | $$ 62 | \mathbf{w} \leftarrow \mathbf{w}+\Delta\mathbf{w} \quad\text{向局部最小值更新一步}。 63 | $$ 64 | 65 | ### 8.2 随机梯度下降(Stochastic Gradient Descent) 66 | 67 | 实际上,梯度下降并不是一个样本高效的优化器,随机下降梯度(stochastic gradient descent, SGD)的使用更为频繁。尽管最初的 SGD 算法指的是使用单个样本更新权重,但由于矢量化的便利性,人们通常也将小批量样本上的梯度下降称为 SGD。在(小批量)SGD 中,我们取一个小批量的样本,使用该样本计算目标函数并通过梯度下降更新权重。现在我们回到前面课程提到的几种算法,看看如何使用值函数近似。 68 | 69 | ### 8.3 蒙特卡洛线性值函数近似(Monte Carlo with Linear VFA) 70 | 71 | 算法 1 是对首次访问蒙特卡洛策略评估的修改,这里我们用线性 VFA 替代了值函数。注意,尽管回报 $Returns(s_t)$ 是无偏估计,但通常它是有噪声的。 72 | 73 |
74 | 75 | ### 8.4 时间差分线性值函数近似(Temporal Difference (TD(0)) with linear VFA) 76 | 77 | 在基于表格的设定中,我们通过引导和采样来近似 $V^{\pi}$,用下式来更新 $V^{\pi}(s)$: 78 | $$ 79 | V^{\pi}(s)=V^{\pi}(s)+\alpha(r+\gamma V^{\pi}(s')-V^{\pi}(s)), 80 | $$ 81 | 这里 $r+\gamma V^{\pi}(s')$ 为我们的目标(target)。我们用相同的 VFA 估计目标和值函数,后面课程中我们会考虑用不同的 VFA(例如在基于控制的设定中)。 82 | 83 | 使用 VFA,我们用 $\hat{V}^{\pi}$ 替换 $V^{\pi}$,如下式: 84 | $$ 85 | \hat{V}^{\pi}(s,\mathbf{w})=\hat{V}^{\pi}(s,\mathbf{w})+\alpha(r+\gamma \hat{V}^{\pi}(s',\mathbf{w})-\hat{V}^{\pi}(s,\mathbf{w}))\nabla_{\mathbf{w}}\hat{V}^{\pi}(s,\mathbf{w}) 86 | $$ 87 | 88 | $$ 89 | \hat{V}^{\pi}(s,\mathbf{w})+\alpha(r+\gamma \hat{V}^{\pi}(s',\mathbf{w})-\hat{V}^{\pi}(s,\mathbf{w}))\mathbf{x}(s)。 90 | $$ 91 | 92 | 在值函数近似中,虽然我们的目标是对真值 $V^{\pi}(s)$ 的有偏且近似的估计,但线性 TD($0$) 仍会收敛(接近)到全局最优解,下面我们进行证明。 93 | 94 | ### 8.5 策略评估的线性值函数近似的收敛性(Convergence Guarantees for Linear VFA for Policy Evaluation) 95 | 96 | 我们定义策略 $\pi$ 的线性 VFA 相对真值的均方误差为: 97 | $$ 98 | MSVE(\mathbf{w})=\sum_{s\in S}\mathbf{d}(s)(\mathbf{v}^{\pi}(s)-\hat{\mathbf{v}}^{\pi}(s,\mathbf{w}))^2, 99 | $$ 100 | 这里 $\mathbf{d}(s)$ 为真实决策过程中基于策略 $\pi$ 的状态的概率分布,$\hat{\mathbf{v}}^{\pi}(s,\mathbf{w})=\mathbf{x}(s)^{\text{T}}\mathbf{w}$ 为线性 VFA。 101 | 102 | **定理 8.1** 使用 VFA 的蒙特卡洛策略评估收敛到权重 $\mathbf{w}_{MC}$,使均方误差最小: 103 | 104 | $$ 105 | MSVE(\mathbf{w}_ {MC})=\mathop{\min}_ {\mathbf{w}}\sum_{s\in S}\mathbf{d}(s)(\mathbf{v}^{\pi}(s)-\hat{\mathbf{v}}^{\pi}(s,\mathbf{w}))^2。 106 | $$ 107 | 108 | **定理 8.2** 使用 VFA 的 TD($0$) 策略评估收敛到权重 $\mathbf{w}_{TD}$,使均方误差在最小均方误差的常值倍数内: 109 | 110 | $$ 111 | MSVE(\mathbf{w}_ {TD})=\frac{1}{1-\gamma} \mathop{\min}_ {\mathbf{w}}\sum_{s\in S}\mathbf{d}(s)(\mathbf{v}^{\pi}(s)-\hat{\mathbf{v}}^{\pi}(s,\mathbf{w}))^2。 112 | $$ 113 | 关于证明,读者可以参考文献 [[1]](#ref1)。 114 | 115 | ### 8.6 基于 VFA 的控制(Control Using VFA) 116 | 117 | 类似 VFA,我们也可以用近似函数来估计状态-行为值,即 $\hat{q}(s,a,\mathbf{w}) \approx q_{\pi}(s,a)$,然后我们可以通过 $\hat{q}(s,a,\mathbf{w})$ 进行策略评估(policy evaluation)和策略提升(policy improvement),下面我们用数学的方式表示上述过程。 118 | 119 | 首先,我们定义目标函数为 $J(\mathbf{w})$: 120 | $$ 121 | J(\mathbf{w})=\mathbb{E}_ {\pi}[(q_{\pi}(s,a)-\hat{q}(s,a,\mathbf{w}))^2]。 122 | $$ 123 | 124 | 类似我们之前所做的,我们可以通过梯度下降或随机梯度下降来最小化目标函数。例如,对于线性状态-行为值近似函数,我们可以: 125 | $$ 126 | x(s)=[x_1(s)\ x_2(s)\ ...\ x_n(s)] \quad\text{状态-行为值特征向量}; 127 | $$ 128 | 129 | $$ 130 | \hat{q}(s,a,\mathbf{w})=x(s,a)\mathbf{w} \quad\text{以特征的线性组合作为状态-行为值}; 131 | $$ 132 | 133 | $$ 134 | J(\mathbf{w})=\mathbb{E}_ {\pi}[(q_{\pi}(s,a)-\hat{q}(s,a,\mathbf{w}))^2] \quad\text{以特征的线性组合作为状态-行为值}; 135 | $$ 136 | 137 | $$ 138 | -\frac{1}{2}\nabla_{\mathbf{w}}J(\mathbf{w})=\mathbb{E}_ {\pi}[(q_{\pi}(s,a)-\hat{q}(s,a,\mathbf{w}))\nabla_{\mathbf{w}}\hat{q}(s,a,\mathbf{w})] \quad\text{计算梯度}; 139 | $$ 140 | 141 | $$ 142 | =(q_{\pi}(s,a)-\hat{q}(s,a,\mathbf{w}))x(s,a) 143 | $$ 144 | 145 | $$ 146 | \Delta\mathbf{w}=-\frac{1}{2}\alpha\nabla_{\mathbf{w}}J(\mathbf{w}) \quad\text{用梯度下降计算更新步长}; 147 | $$ 148 | 149 | $$ 150 | =\alpha(q_{\pi}(s,a)-\hat{q}(s,a,\mathbf{w}))x(s,a) 151 | $$ 152 | 153 | $$ 154 | \mathbf{w} \leftarrow \mathbf{w}+\Delta\mathbf{w} \quad\text{向局部最小值更新一步}。 155 | $$ 156 | 157 | 对于蒙特卡洛,我们用回报 $G_t$ 代替目标 $q_{\pi}(s,a)$,也就是说,我们的更新为: 158 | $$ 159 | \Delta\mathbf{w}=\alpha(G_t-\hat{q}(s,a,\mathbf{w}))\nabla_{\mathbf{w}}\hat{q}(s,a,\mathbf{w})。 160 | $$ 161 | 162 | 对于 SARSA,我们用 TD 目标代替 $q_{\pi}(s,a)$: 163 | $$ 164 | \Delta\mathbf{w}=\alpha(r+\gamma\hat{q}(s',a',\mathbf{w})-\hat{q}(s,a,\mathbf{w}))\nabla_{\mathbf{w}}\hat{q}(s,a,\mathbf{w})。 165 | $$ 166 | 167 | 对于 Q-学习,我们用 TD 目标的最大值代替 $q_{\pi}(s,a)$: 168 | $$ 169 | \Delta\mathbf{w}=\alpha(r+\gamma\mathop{\max}_ {a'}\hat{q}(s',a',\mathbf{w})-\hat{q}(s,a,\mathbf{w}))\nabla_{\mathbf{w}}\hat{q}(s,a,\mathbf{w})。 170 | $$ 171 | 172 | 注意,由于我们使用了值函数近似来完成卡尔曼备份,我们无法保证收敛性,推荐读者看一下 Baird 的 Counterexample,以获得更具体的说明。表1对不同算法是否收敛进行了总结。 173 | 174 |
175 | 176 |
177 | 表 1:基于 VFA 的控制方法的收敛性总结。(Yes) 表示结果在接近最优值函数时会抖动。 178 |
179 | 180 | ## 9. 神经网络(Neural Networks) 181 | 182 | 虽然线性 VFA 在给出了正确特征的情况下的表现通常很很好,但也很难去手动给出这些特征。神经网络提供了一个更为丰富的函数近似类,它可以直接以状态作为变量,而不需要明确的特征。 183 | 184 | 图 2 展示了一个通用的前馈神经网络。图中的神经网络由一个包含两个输出单元的输出层、一个包含四个输入单元的输入层、以及两个隐藏层(既不是输入层也不是输出层)组成。每个连接都与一个实值权重关联。这些单元通常是半线性的,即它们计算输入信号的加权和,然后对这个加权和应用一个非线性函数,这个函数通常被称为激活函数(activation function)。作业2中,我们研究了具有单个隐藏层的神经网络如何拥有“通用逼近(universal approximaiton)”的性质,实验和理论都表明,通过多个隐藏层的有层次的组合,我们可以更容易地逼近复杂的函数。 185 | 186 |
187 | 188 |
189 | 图 2:一个包含四个输入单元、两个输出单元、两个隐藏层的通用的前馈神经网络。 190 |
191 | 192 | 下节课我们将更深入地讨论关于用神经网络解决强化学习问题的一些理论和近期发现。 193 | 194 | ## 参考文献 195 | 196 | 1. J. N. Tsitsiklis, and B. V. Roy, "An analysis of temporal-difference learning with function approximation," *IEEE Transactions on Automation Control*, 1997. -------------------------------------------------------------------------------- /docs/4.md: -------------------------------------------------------------------------------- 1 | # Lecture 4 Model Free Control 2 | 3 | # 课时4 无模型控制 2019.01.16 4 | 5 | ## 5. 无模型控制(Model Free Control) 6 | 7 | 上次课我们讨论了如何在不知道世界的模型、仅与环境交互的情况下评估一个给定的策略,我们提出了几种无模型策略评估(model-free policy evaluation)方法:蒙特卡洛策略评估和 TD 学习。这节课我们讨论无模型控制(model-free control),这里我们要在相同的约束下(只有交互,不知道状态转移概率或奖励)学习好的策略。这个框架在以下两种情况中是很重要的: 8 | 1. MDP 模型未知,但我们可以从这个 MDP 中对轨迹采样,或 9 | 2. MDP 模型已知,但由于计算量的原因,我们无法通过有模型控制方法计算价值函数。 10 | 11 | 本节课我们仍基于表格的设定(tabular setting),也就是说我们可以将每个状态值或状态-行为值作为表格的元素。下节课我们将基于值函数近似(value function approximation)的设定,重新检查今天和之前的课中提及的算法,这包括尝试将一个函数转换为状态值函数或状态-行为值函数。 12 | 13 | ### 5.1 广义策略迭代(Generalized Policy Iteration) 14 | 15 | 首先回忆有模型策略迭代算法,如算法 1 所示。 16 | 17 |
18 | 19 | 上节课我们介绍了无模型策略迭代算法,所以我们可以用无模型的方式将第四行替换。然而,为了使整个算法是无模型的,我们必须找到一种方式去处理第五行。根据定义,我们有 $Q^{\pi}(s,a)=R(s,a)+\gamma \sum_{s'\in S}P(s'|s,a)V^{\pi}(s')$,因此,我们可以通过在模型策略迭代算法中使用这个值,从而得到无模型策略迭代算法(算法 2)。 20 | 21 |
22 | 23 | 由于我们在第五行做了替换,有一些事项需要注意: 24 | 1. 如果策略 $\pi$ 是确定的,或不是以正的概率生成每一个动作 $a$,那么我们无法确切地计算第五行中的最大值。 25 | 2. 通过策略迭代算法,我们得到的是 $Q^{\pi}$ 的估计,所以我们并不清楚第五行是否会像有模型情况那样单调地提升策略。 26 | 27 | ### 5.2 探索的重要性(Importance of Exploration) 28 | 29 | #### 5.2.1 探索(Importance) 30 | 31 | 上一部分里,我们看到无模型策略迭代算法的一个注意事项是策略 $\pi$ 需要对每个动作赋予一个正的概率,这样每个状态-行为值才能被确定。换句话说,策略 $\pi$ 应该探索动作,即使这些动作根据当前的 $Q$ 值估计来看可能不是最优的。 32 | 33 | #### 5.2.2 $\epsilon$-贪婪策略($\epsilon$-greedy Policies) 34 | 35 | 为了探索那些根据当前的 $Q$ 值估计来看可能不是最优的动作,我们需要需要一个系统的方式去平衡对非最优动作的探索与对最优(贪婪)动作的利用。一个简单的方法是以一个小的概率去采取一个随机动作,其余时间采用贪婪动作,这种探索方式被称为 $\epsilon$-贪婪策略($\epsilon$-greedy policy)。基于状态-行为值 $Q^{\pi}(s,a)$,我们可以用如下的数学方式来表示 $\epsilon$-贪婪策略: 36 | $$ 37 | \pi(a|s)= 38 | \begin{cases} 39 | a, & \text{with probability $\frac{\epsilon}{|A|}$} \\ 40 | \mathop{\arg\max}_{a} Q^{\pi}(s,a), & \text{with probability $1-\epsilon$} 41 | \end{cases} 42 | $$ 43 | 44 | #### 5.2.3 单调 $\epsilon$-贪婪策略提升(Monotonic $\epsilon$-greedy Policy Improvement) 45 | 46 | 第二节课中通过策略提升定理我们看到,如果基于当前的值采用贪婪动作,然后一直根据策略 $\pi$ 选取动作,那么这个策略相对策略 $\pi$ 来说是一个提升。那么,基于策略 $\pi$ 的 $\epsilon$-贪婪策略是否也是策略 $\pi$ 的一个提升呢?这个问题有助于解决算法 2 的第二个注意事项。幸运的是,关于 $\epsilon$-贪婪策略,有一个与策略提升定理类似的定理。下面我们介绍并推导这个定理。 47 | 48 | **定理 5.1** (单调 $\epsilon$-贪婪策略提升,Monotonic $\epsilon$-greedy Policy Improvement)令 $\pi_i$ 为一个 $\epsilon$-贪婪策略,那么,基于 $Q^{\pi_i}$ 的 $\epsilon$-贪婪策略,记为 $\pi_{i+1}$,是相对策略 $\pi_i$ 单调提升的,也就是说,$V^{\pi_{i+1}}\geq V^{\pi_{i}}$。 49 | 50 | 证明:我们首先证明对于所有状态 $s$,有 $Q^{\pi_i}(s,\pi_{i+1}(s)) \geq V^{\pi_{i}}(s)$。 51 | $$ 52 | Q^{\pi_i}(s,\pi_{i+1}(s)) = \sum_{a\in A}\pi_{i+1}(a|s)Q^{\pi_i}(s,a) 53 | $$ 54 | 55 | $$ 56 | = \frac{\epsilon}{|A|} \sum_{a\in A}Q^{\pi_i}(s,a) + (1-\epsilon)\mathop{\max}_{a'}Q^{\pi_i}(s,a') 57 | $$ 58 | 59 | $$ 60 | = \frac{\epsilon}{|A|} \sum_{a\in A}Q^{\pi_i}(s,a) + (1-\epsilon)\mathop{\max}_{a'}Q^{\pi_i}(s,a') \frac{1-\epsilon}{1-\epsilon} 61 | $$ 62 | 63 | $$ 64 | = \frac{\epsilon}{|A|} \sum_{a\in A}Q^{\pi_i}(s,a) + (1-\epsilon)\mathop{\max}_ {a'}Q^{\pi_i}(s,a') \sum_{a\in A}\frac{\pi_{i}(a|s)-\frac{\epsilon}{|A|}}{1-\epsilon} 65 | $$ 66 | 67 | $$ 68 | = \frac{\epsilon}{|A|} \sum_{a\in A}Q^{\pi_i}(s,a) + (1-\epsilon)\sum_{a\in A} \frac{\pi_{i}(a|s)-\frac{\epsilon}{|A|}}{1-\epsilon} \mathop{\max}_{a'}Q^{\pi_i}(s,a') 69 | $$ 70 | 71 | $$ 72 | \geq \frac{\epsilon}{|A|} \sum_{a\in A}Q^{\pi_i}(s,a) + (1-\epsilon)\sum_{a\in A} \frac{\pi_{i}(a|s)-\frac{\epsilon}{|A|}}{1-\epsilon} Q^{\pi_i}(s,a) 73 | $$ 74 | 75 | $$ 76 | = \sum_{a\in A}\pi_{i}(a|s)Q^{\pi_i}(s,a) 77 | $$ 78 | 79 | $$ 80 | = V^{\pi_i}(s) 81 | $$ 82 | 第一个等式是因为我们根据策略 $\pi_{i+1}$ 生成第一个动作,然后根据策略 $\pi_{i}$ 生成之后的动作。第四个等式是因为 $1-\epsilon = \sum_{a}[\pi_{i}(a|s)-\frac{\epsilon}{|A|}]$。 83 | 84 | 根据策略提升定理,我们知道 $Q^{\pi_i}(s,\pi_{i+1}(s)) \geq V^{\pi_{i}}(s)$ 意味着对于所有状态 $s$,$V^{\pi_{i+1}}(s) \geq V^{\pi_{i}}(s)$。 85 | 86 | 证明完毕。$\diamondsuit$ 87 | 88 | 因此,如果我们基于当前的 $\epsilon$-贪婪策略进行 $\epsilon$-贪婪动作,我们的策略实际上是提升的。 89 | 90 | #### 5.2.4 无限探索中的极限贪心策略(Greedy in the Limit of Infinite Exploration) 91 | 92 | 上面我们介绍了一种 $\epsilon$-贪婪策略的简单方式以平衡对新动作的探索和对已有知识的利用。我们还可以通过引入一种新的、保证算法收敛的探索策略来改善这种平衡,这种策略就是无限探索中的极限贪心策略(Greedy in the Limit of Infinite Exploration, GLIE)。 93 | 94 | **定义 5.1** (Greedy in the Limit of Infinite Exploration, GLIE)如果策略 $\pi$ 满足以下两条性质,那么它是无限探索中的极限贪心策略: 95 | 1. 所有的状态-行为对被访问无限多次,即对于所有的 $s\in S$,$a\in A$,有 96 | $$ 97 | \lim_{i\to\infty} N_i(s,a) \rightarrow \infty, 98 | $$ 99 | 这里 $N_i(s,a)$ 为直到(包括)片段 $i$ 在状态 $s$ 采用动作 $a$ 的次数。 100 | 2. 行为策略收敛到基于学习到的 $Q$ 函数的贪婪策略,即对于所有的 $s\in S$,$a\in A$,有 101 | $$ 102 | \lim_{i\to\infty} \pi_i(a|s) = \mathop{\arg\max}_{a} q(s,a) \quad\text{with probability $1$}。 103 | $$ 104 | 105 | GLIE 策略的一个例子是 $\epsilon_i=\frac{1}{i}$ 的 $\epsilon$-贪婪策略,这里 $i$ 为片段的序号,$\epsilon$ 逐渐减小到 $0$。可以看出,对所有的 $i$,$\epsilon_i>0$,在每个时间步我们都以一个正的概率进行探索,因此满足 GLIE 的第一个条件;由于当 $i \rightarrow \infty$,$\epsilon_i \rightarrow 0$,极限情况下这个策略是贪婪的,因此满足 GLIE 的第二个条件。 106 | 107 | ### 5.3 蒙特卡洛控制(Monte Carlo Control) 108 | 109 | 现在我们将上述探索策略和上节课讨论的无模型策略评估算法结合起来,以得到一些无模型控制方法。算法 3 展示了首次访问在线蒙特卡洛控制;如果不检查第七行中的首次访问条件,我们就得到了每次访问在线蒙特卡洛控制。 110 | 111 |
112 | 113 | 像前面说过的,GLIE 策略保证我们的无模型控制方法收敛,特别地,我们有以下结果: 114 | 115 | **定理 5.2** GLIE 蒙特卡洛控制收敛到最优状态-行为值函数,即 $Q(s,a)\rightarrow q(s,a)$。 116 | 117 | ### 5.4 时间差分控制(Temporal Difference Methods for Control) 118 | 119 | 上节课我们还介绍了另一种无模型策略评估方法:TD(0)。现在我们将上述的探索策略与 $TD(0)$ 结合起来:在线策略(on-policy)和离线策略(off-policy)。我们首先介绍在线策略的方法,这种方法也被称为 SARSA,如算法 4 所示。 120 | 121 |
122 | 123 | 算法 4 的第十行为策略评估更新,第十一行为策略提升。SARSA的名字源于用于更新方程的轨迹,为了更新状态-行为对 $(s,a)$ 的 $Q$ 值,我们需要奖励、下一状态以及下一动作,因此我们需要 $(s,a,r,s',a')$。SARSA 是在线策略的方法,因为用于更新方程的动作 $a$ 和$a'$ 都源于更新时存在的(最新的)策略。 124 | 125 | 像蒙特卡洛那样,我们可以得到 SARSA 的收敛性,但我们需要一个额外的条件: 126 | 127 | **定理 5.3** 对于有限状态和有限动作的 MDP,如果满足以下两个条件,那么 SARSA 会收敛到最优状态-行为值,即 $Q(s,a)\rightarrow q(s,a)$: 128 | 1. 策略 $\pi$ 的序列满足 GLIE。 129 | 2. 步长 $a_t$ 满足罗宾斯-蒙罗(Robbins-Munro)序列,使得: 130 | $$ 131 | \sum_{t=1}^{\infty} \alpha_t=\infty, 132 | $$ 133 | 134 | $$ 135 | \sum_{t=1}^{\infty} \alpha_t^2<\infty。 136 | $$ 137 | 138 | **练习 5.1** 算法 4 的第十一行,每次更新后都做一次策略更新的好处是什么?降低执行策略提升步骤的频率有什么好处? 139 | 140 | ### 5.5 离线策略 TD 的重要性采样(Importance Sampling for Off-Policy TD) 141 | 142 | 在推导离线策略 TD 控制前,我们先看一下离线 TD 策略评估的一种方式。回想以下,我们的 TD 更新采用了以下形式: 143 | $$ 144 | V(s)\leftarrow V(s)+\alpha(r+\gamma V(s')-V(s))。 145 | $$ 146 | 现在我们假设,就像离线策略蒙特卡洛策略评估那样,我们有根据策略 $\pi_b$ 产生的数据,并且我们希望估计策略 $\pi_e$ 的值。我们可以以样本的频率来为目标赋予权重,然后通过重要性采样评估策略,然后用下式来更新: 147 | $$ 148 | V^{\pi_e}(s)\leftarrow V^{\pi_e}(s)+\alpha[\frac{\pi_e(a|s)}{\pi_b(a|s)}(r+\gamma V^{\pi_e}(s')-V^{\pi_e}(s))]。 149 | $$ 150 | 注意,我们只使用一个采样轨迹而不是像蒙特卡洛那样采样所有轨迹,所以我们只从一个步骤考虑似然比,同样地,这个方法的方差比蒙特卡洛小得多。 151 | 152 | 另外,$\pi_b$ 不必每步都一样,但我们需要知道每步的动作的概率。就像蒙特卡洛那样,我们需要这两个策略有同样的原则,即如果 $\pi_e(a|s) \times V^{\pi_e}(s')$,那么$\pi_b(a|s)>0$。 153 | 154 | ### 5.6 Q-学习(Q-learning) 155 | 156 | 现在我们讨论适用于 TD 控制的离线策略方法。上面我们再次利用了重要性采样,但在 TD 控制中,我们不需要依赖于此,相反,我们可以维持 $Q$ 的估计并且与最好的未来动作相联系。SARSA 用下式来更新: 157 | $$ 158 | Q(s_t,a_t)\leftarrow Q(s_t,a_t)+\alpha_t[r_t+\gamma Q(s_{t+1},a_{t+1})-Q(s_t,a_t)], 159 | $$ 160 | 但我们可以通过引导下一状态的 $Q$ 值来获得以下更新: 161 | $$ 162 | Q(s_t,a_t)\leftarrow Q(s_t,a_t)+\alpha_t[r_t+\gamma \mathop{\max}_ {a'} Q(s_{t+1},a')-Q(s_t,a_t)]。 163 | $$ 164 | 这就产生了 Q-学习(Q-learning),如算法 5,在下一状态,我们取使下一状态的值最大的动作,这个动作不一定和由当前策略产生的动作一样,因此,Q-学习是离线策略算法。 165 | 166 |
167 | 168 | ## 6. 最大化偏差(Maximization Bias) 169 | 170 | 最后我们来讨论最大化偏差(maximization bias)现象。我们首先通过一个小例子查看这个现象。 171 | 172 | ### 6.1 硬币例子(Example: Coins) 173 | 174 | 假设有两枚相同的公平的硬币,但我们不知道它们是否相同或公平。如果一枚硬币落地时头朝上,我们就得到一美元;反之则输掉一美元。我们希望回答以下两个问题: 175 | 1. 哪枚硬币在未来能赢来更多的钱? 176 | 2. 问题1中,赢/输的钱的期望是多少? 177 | 178 | 为了回答这两个问题,我们将两枚硬币各抛一次,然后选择赢来更多钱的硬币,以此作为第一个问题的回答;不论这枚硬币给我们带来/损失多少钱,我们都以这个数字来回答第二个问题。例如,如果硬币1头朝上而硬币2尾朝上,我们将硬币1作为第一问的答案,将一美元作为第二问的答案。 179 | 180 | 让我们检查以下此过程可能的结果。如果至少有一枚硬币是头朝上,那么对于第二个问题我们的回答是一美元。如果两枚硬币都是尾朝上,回答则变为负一美元。因此,对问题二的回答的期望值为 $\frac{3}{4}\times(1)+\frac{1}{4}\times(-1)=0.5$。这意味着抛更好的硬币得到的期望价值的估计高于抛那枚硬币得到的实际期望价值,换句话说,我们认为这个硬币比实际的要好。 181 | 182 | 这个问题源于这样一个事实,即我们基于估计值来选择更好的硬币并估计其价值。我们可以将这两步分开来解决这个问题。一种方法是将流程改为如下所示的步骤:选择更好的硬币后,再次抛这枚更好的硬币并用这个值作为问题二的回答,此时这个回答的期望值为 $0$,这与抛任意一枚硬币的实际期望价值相同。 183 | 184 | ### 6.2 强化学习中的最大化偏差(Maximization Bias in Reinforcement Learning) 185 | 186 | 让我们在一个有两个动作的单状态 MDP 中正式地介绍上面所看到的内容。假设我们处于状态 $s$,有两个动作 $a_1$ 和 $a_2$,每个动作的平均奖励都为 $0$,那么真实的状态值和状态-行为值都为 $0$,即 $Q(s,a_1)=Q(s,a_2)=V(s)=0$。假设我们对采取每个动作的奖励进行了采样,以此得到了状态-行为值的估计 $\hat{Q}(s,a_1)$ 和 $\hat{Q}(s,a_2)$。还假设这些采样是无偏的因为它们是由蒙特卡罗方法生成的,即 $\hat{Q}(s,a_i)=\frac{1}{n(s,a_i)}\sum_{j=1}^{n(s,a_i)}r_j(s,a_i)$,$n(s,a_i)$ 是状态-行为对 $(s,a_i)$ 的采样数。令 $\hat{\pi}=\mathop{\arg\max}_{a}\hat{Q}(s,a)$ 为基于 $Q$ 值估计的贪婪策略,则有: 187 | $$ 188 | \hat{V}(s)=\mathbb{E}[\mathop{\max}(\hat{Q}(s,a_1),\hat{Q}(s,a_2))] 189 | $$ 190 | 191 | $$ 192 | \geq \mathop{\max}(\mathbb{E}[\hat{Q}(s,a_1)],\mathbb{E}[\hat{Q}(s,a_2)]) \quad\text{by Jensen's inequality} 193 | $$ 194 | 195 | $$ 196 | =\mathop{\max}(0,0) \quad\text{since each estimate is unbiased and $Q(s,\cdot)=0$} 197 | $$ 198 | 199 | $$ 200 | =0=V^{*}(s)。 201 | $$ 202 | 因此,我们的 $s$ 的状态值估计至少和真实的状态值一样大,我们高估了已有的有限样本的状态值。 203 | 204 | ### 6.3 双 Q-学习(Double Q-learning) 205 | 206 | 上一小节中我们看到,当我们有有限个样本时,状态值估计可能会出现最大化偏差。正如我们在硬币例子中讨论的,将取最大值和估计最大值分开进行可以避免这种现象。在 Q-学习中,我们可以保持两个独立的无偏估计 $Q_1$ 和 $Q_2$,用其中一个选择最大值,用另一个估计这个最大值,这就产生了双 Q-学习(double Q-learning),如算法 6 所示,当我们提及关于 $Q_1+Q_2$ 的 $\epsilon$ 贪婪策略时,我们指的是状态 $s$ 的最优动作是 $\mathop{\arg\max}_{a} Q_1(s,a)+Q_2(s,a)$ 的 $\epsilon$-贪婪策略。 207 | 208 |
209 | 210 | 双 Q-学习比标准 Q-学习更快地消除次优动作,可以显著加快训练,Sutton and Barto [[1]](#ref1) 6.7节的玩具 MDP 就是一个很好的例子。 211 | 212 | ## 参考文献 213 | 214 | 1. R. S. Sutton, and A. G. Barto, *Introduction to Reinforcement Learning*. 2nd ed., MIT Press, 2017. Draft. http://incompleteideas.net/book/the-book-2nd.html. -------------------------------------------------------------------------------- /docs/11&12.md: -------------------------------------------------------------------------------- 1 | # Lecture 11&12 Exploration and Exploitation 2 | 3 | # 课时11&12 探索与利用 2019.02.20 & 2019.02.25 & 2019.02.27 4 | 5 | ## 1. 介绍(Introduction) 6 | 7 | 我们之前讨论过强化学习算法的设计,特别地,除了渐近收敛之外,我们还希望获得良好的性能。在教育、医疗或机器人等许多实际应用中,渐近收敛速度并不是比较强化学习算法的有效指标。为实现良好的现实世界中的表现,我们希望能够快速收敛到好的策略,这有赖于良好的、有策略性的探索。 8 | 9 | 在线决策涉及到探索(exploration)与利用(exploitation)之间的基本权衡。利用(通过最大化未来收益来)制定最佳的可能的策略,而探索则采取次优动作来收集信息。虽然次优动作必然会导致近期的奖励减少,但它可能使得我们学习更好的策略,从长远来看能够改进策略。 10 | 11 | ## 2. 多臂赌博机问题(Multi-Armed Bandits) 12 | 13 | 我们首先讨论在多臂赌博机(multi-armed bandits, MABs)背景下,而非完全 MDPs 背景下的探索。MAB 是元组 $(A,R)$,这里 $A$ 表示动作的集合,$R$ 为每个动作对应奖励的概率分布 $R^{a}(r)=P[r|a]$。在每个时间步,行为体选择一个动作 $a_{t}$。像在 MDPs 中那样,行为体的目的是最大化累积的奖励。但由于不存在状态转移,所以不存在延迟的奖励或结果的概念。 14 | 15 | 令 $Q(a)=\mathbb{E}[r|a]$ 表示采取动作 $a$ 的真实期望奖励。我们考虑估计 $\hat{Q}_{t}(a)\approx Q(a)$ 的算法,该值通过蒙特卡洛评估来估计: 16 | 17 | $$ 18 | \hat{Q}_ {t}(a) = \frac{1}{N_{t}(a)}\sum_{t=1}^{T} r_{t} {\bf{1}} (a_{t}=a) = \hat{Q}_ {t-1}(a)+\frac{1}{N_{t}(a)}(r_{t}-\hat{Q}_ {t-1}(a)), 19 | \tag{1} 20 | $$ 21 | 22 | 这里 $N_{t}(a)$ 为动作 $a$ 在时间 $t$ 被采用过的次数。第二个等式用于递增地计算 $\hat{Q}_{t}$。 23 | 24 | 贪婪策略(greedy algorithm)选择有最大估计价值的动作:$a_{t}^{\ast}=\mathop{\arg\max}_ {a\in A} \hat{Q}_ {t}(a)$。然而,贪婪的做法可能使得次优的动作永远无法被采用。像在 MDPs 中那样,我们也可以使用(固定的)$\epsilon$-贪婪算法($\epsilon$-greedy algorithm),即以 $1-\epsilon$ 的概率选择贪婪动作,以 $\epsilon$ 的概率选择随机动作。另一个算法是衰减 $\epsilon_{t}$-贪婪算法(decaying $\epsilon_{t}$-greedy algorithm),这里 $\epsilon_{t}$ 按照一定规律衰减。 25 | 26 | 一个简单的基于 $\epsilon$-贪婪算法的方法是乐观初始化(optimistic initialization),它将所有 $a\in A$ 的 $\hat{Q}_ {0}(a)$ 初始化为大于真值 $Q(a)$ 的某个值,也就是说,我们开始时对所有的动作选择“非常乐观”。在每一步我们可以使用贪婪(或 $\epsilon$-贪婪)的方法来选择动作,由于真正的奖励都低于我们的初始估计,所以被采用过的动作的估计值 $\hat{Q}$ 就会减小,这就鼓励了行为体对那些未被采用过的、$\hat{Q}$ 值仍旧大的动作进行探索。因此,所有的动作都会被至少尝试一次,可能多次。此外,我们可以初始化 $N_{0}(a)>0$ 以调整乐观初始化向真值收敛的速度。 27 | 28 | ### 2.1 遗憾(Regret) 29 | 30 | 这些探索策略自然会产生一个问题,即我们应该使用哪种标准来比较它们。可能的标准包括经验性的表现(尽管这依赖于环境)、渐近收敛的保证、有限采样的保证或 PAC 的保证。在 MAB 文献中的标准通常是遗憾(regret),我们现在定义遗憾以及相关的量。 31 | 32 | $\bullet$ 动作值(action-value)$Q(a)=\mathbb{E}[r|a]$ 33 | 34 | $\bullet$ 最优值(optimal value)$V^{\ast}=Q(a^{\ast})=\mathop{\max}_{a\in A}Q(a)$ 35 | 36 | $\bullet$ 差距(gap)$\Delta_{a}=V^{\ast}-Q(a)$ 37 | 38 | $\bullet$ 遗憾(regret)$l_{t}=\mathbb{E}[V^{\ast}-Q(a_{t})]$ 39 | 40 | $\bullet$ 总遗憾(total regret)$L_{t}=\mathbb{E}[\sum_{\tau=1}^{t}(V^{\ast}-Q(a_{\tau}))]=t\cdot V^{\ast} - \mathbb{E}[\sum_{\tau=1}^{t}Q(a_{\tau})]$ 41 | 42 | 因此,最小化总遗憾等价于最大化累积的奖励。如果我们定义 $\overline{N}_{t}(a)$ 为动作 $a$ 的期望选择次数,那么总遗憾就是差距和动作选择次数的函数: 43 | 44 | $$ 45 | L_{t} = \mathbb{E}[\sum_{\tau=1}^{t}(V^{\ast}-Q(a_{\tau}))] 46 | \tag{2} 47 | $$ 48 | 49 | $$ 50 | = \sum_{a\in A} \mathbb{E}[N_{t}(a)] (V^{\ast}-Q(a)) 51 | \tag{3} 52 | $$ 53 | 54 | $$ 55 | = \sum_{a\in A} \overline{N}_ {t}(a) \Delta_{a} 56 | \tag{4}。 57 | $$ 58 | 59 | 高质量的算法可以保证对于大的差距,动作选择次数比较小。然而,差距并不能被事先知道,而且必须通过与 MAB 交互被习得。 60 | 61 | ### 2.2 遗憾界限(Regret Bounds) 62 | 63 | 我们希望保证某些算法的遗憾是可以量化并且有界的。遗憾界限有两种类型:与问题相关的遗憾界限和与问题无关的遗憾界限。与问题相关的遗憾界限是动作选择次数与差距的函数,与问题无关的遗憾界限是 $T$ 的函数,这里 $T$ 为算法执行的总步骤数。 64 | 65 | 永远探索或永远选择次优操作的算法都会经历线性遗憾(linear regret)。因此,达到次线性遗憾(sublinear regret)是可取的。前面讨论过的算法的遗憾界限如下: 66 | 67 | $\bullet$ 贪婪(greedy):线性总遗憾 68 | 69 | $\bullet$ 常值 $\epsilon$-贪婪(constant $\epsilon$-greedy):线性总遗憾 70 | 71 | $\bullet$ 衰减 $\epsilon$-贪婪(decaying $\epsilon$-greedy):次线性遗憾但 $\epsilon$ 的衰减进度需要差距的知识 72 | 73 | $\bullet$ 乐观初始化(optimistic initialization):如果初始值足够乐观则为次线性遗憾,否则为线性遗憾 74 | 75 | 为了解问题的严重性,我们先来探讨遗憾的下界。一般来说,任何算法的性能都取决于最优动作与其他动作的相似程度。困难的问题会有相似的动作,但方式略有不同,这可以由差距 $\Delta_{a}$ 和分布的相似程度(通过 KL 散度)$KL(R^{a}\lVert R^{a^{\ast}})$ 来描述,然后,我们可以对渐近总遗憾建立一个界限。 76 | 77 | **定理 1**(Lai and Robbins,1985)对于 MAB,任何算法在总遗憾上的渐近下界至少为 78 | 79 | $$ 80 | \lim_{t\to\infty} L_{t}\geq \log t \sum_{a|\Delta_{a}>0}\frac{\Delta_{a}}{KL(R^{a}\lVert R^{a^{\ast}})}。 81 | $$ 82 | 83 | ### 2.3 乐观面对不确定性(Optimism in the Face of Uncertainty) 84 | 85 |
86 | 87 |
88 | 图 1:动作 $a_1$,$a_2$,$a_3$ 的奖励分布。 89 |
90 | 91 | 考虑图 1,它显示了一些动作的估计分布。我们应该选择哪个动作?面对不确定性时的乐观原则是,我们应该把选择偏向于可能是好的的动作。直觉上,这将使我们了解到这个动作要么的确会带来高额回报,要么并不如我们期望的那么好,而且也会使我们了解到关于我们的问题的有价值的信息。 92 | 93 | 在这个方法的基础上,置信上界算法(Upper Confidence Bound Algorithm)产生了,其过程如下。首先,我们对每个动作值估计一个置信上界 $\hat{U}_ {t}(a)$ 使得大概率 $Q(a)\leq\hat{Q}_ {t}(a)+\hat{U}_ {t}(a)$ 成立,这依赖于动作 $a$ 被选择的次数。然后我们选择最大化置信上界的动作 94 | 95 | $$ 96 | a_{t}=\mathop{\arg\max}_ {a\in A}(\hat{Q}_ {t}(a)+\hat{U}_ {t}(a)), 97 | \tag{5} 98 | $$ 99 | 100 | 这可以由 Hoeffding 不等式(Hoeffding's inequality)推导得出。 101 | 102 | **定理 2**(Hoeffding 不等式)令 $X_{1},...,X_{t}$ 为在区间 $[0,1]$ 中的独立同分布(i.i.d.)随机变量,$\overline{X}=\frac{1}{t}\sum_{\tau=1}^{t}X_{\tau}$ 为平均值,$u$ 为一个常量。那么, 103 | 104 | $$ 105 | P[ \mathbb{E}[x]>\overline{X}_{t}+u] \leq e^{-2tu^{2}}。 106 | $$ 107 | 108 | 对 MAB 问题应用[定理 2](#thm2),我们得到: 109 | 110 | $$ 111 | P[Q(a)>\hat{Q}_ {t}(a)+U_ {t}(a)] \leq e^{-2N_{t}(a)U_{t}(a)^{2}}。 112 | \tag{6} 113 | $$ 114 | 115 | 选择一个概率 $p$ 使得 116 | 117 | $$ 118 | e^{-2N_{t}(a)U_{t}(a)^{2}} = p, 119 | \tag{7} 120 | $$ 121 | 122 | $$ 123 | U_{t}(a) = \sqrt{\frac{-\log p}{2N_{t}(a)}}。 124 | \tag{8} 125 | $$ 126 | 127 | 随着我们观察到更多的奖励,我们将减小 $p$,特别地,选择 $p=t^{-4}$ 便得到了 UCB1 算法: 128 | 129 | $$ 130 | a_{t} = \mathop{\arg\max}_ {a\in A}(Q(a)+\sqrt{\frac{2\log t}{N_{t}(a)}}), 131 | \tag{9} 132 | $$ 133 | 134 | 这样保证了渐近最优动作选择,即它将 [[1]](#ref1) 下界匹配到常数因子。 135 | 136 | **定理 2**(Auer et al., 2002, [[2]](#ref2))UCB 算法得到的渐近总遗憾满足: 137 | 138 | $$ 139 | \lim_{t\to\infty} L_t \leq 8 \log t \sum_{a|\Delta_{a}>0}\Delta_{a}。 140 | $$ 141 | 142 | ### 2.4 贝叶斯方法(Bayesian Bandits) 143 | 144 | 除了假设奖励有界,到目前为止我们还没对奖励分布 $R$ 做出任何假设。贝叶斯方法(Bayesian bandits)利用奖励的先验知识来计算下一时间步奖励的概率分布,即 $P[R]$,这里 $h_t=(a_1,r_1,...,a_{t-1},r_{t-1})$,然后根据这个后验知识来选择动作。例如,考虑应用了贝叶斯方法的 UCB,我们假设奖励分布为高斯分布,$R^{a}=N(r;\mu_{a},\sigma_{a}^{2})$,那么基于 $\mu_{a}$ 和 $\sigma_{a}^{2}$ 的后验概率可以用以下方式计算: 145 | 146 | $$ 147 | P[\mu_{a},\sigma_{a}^{2}|h_t] \propto P[\mu_{a},\sigma_{a}] \prod_{t|a_{t}=a} N(r_{t};\mu_{a},\sigma_{a}^{2}), 148 | \tag{10} 149 | $$ 150 | 151 | 我们可以选择最大化 $Q(a)$ 的标准差的动作: 152 | 153 | $$ 154 | a_{t}=\mathop{\arg\max}_ {a\in A}(\mu_a + c\frac{\sigma_{a}}{\sqrt{N(a)}})。 155 | \tag{11} 156 | $$ 157 | 158 | 另一种方式是概率匹配(probability matching),即根据某一动作是最优动作的概率选择动作, 159 | 160 | $$ 161 | \pi(a|h_t) = P[Q(a)>Q(a'),\forall a'\neq a|h_t], 162 | \tag{12} 163 | $$ 164 | 165 | 面对不确定性,这样做是乐观的,因为不确定的动作有更高的概率是最优动作,然而,这个概率可能很难用解析的方法来计算。 166 | 167 | 实现概率匹配的一种方法是汤普森采样(Thompson sampling),这里奖励分布 $\hat{R}$ 由对每个 $a\in A$ 采样得到,然后我们可以得到动作值函数 $\hat{Q}(a)=\mathbb{E}[\hat{R}^{a}]$,并选择最大值对应的动作,这个方法达到了 Lai-Robbins 下界 [[1]](#ref1),并且在实践中非常高效。 168 | 169 | 可以证明,如果先验分布为 Beta 分布,那么所需的后验分布也是 Beta 分布。因此,我们可以在下一个时间步从 Beta 先验开始,然后更新后验的参数。更具体地说,令 $S_i$ 为到目前为止对动作 $i$ 后一秒观测到的值,$F_i$ 为后零秒观测到的值,那么下面的伪代码实现了这个算法。 170 | 171 |
172 | 173 | 类似的方法也适用于单位方差高斯 MAB,经过时间 $t$ 后,对于动作 $k$ 的后验值为 $N(\mu_{k},\frac{1}{S_k+F_k+1})$,这里 $\mu_k$ 为经验性的奖励。作为练习,请证明这一结论。 174 | 175 | ### 2.5 PAC 方法(PAC Bandits) 176 | 177 | 上面讨论的方法都试图达到用 $T$ 表示的遗憾界限,但这并不能让我们了解到算法所犯的错误类型,它可能不经常犯大错误,也可能经常犯小错误。在许多应用中,我们可能会关心限制大错误的数量。 178 | 179 | 通常,PAC 算法选择一个值为 $\epsilon$-最优的动作,即对于除了一个多项式的数(通常以 $\epsilon$,$\delta$,$N$ 的形式)的时间步外,$Q(a)\geq Q(a_{\ast})-\epsilon$ 的概率至少为 $1-\delta$。在不确定情况下和汤普森采样情况下,还有应用了这种 PAC 保证的乐观初始化方法的变体。 180 | 181 | ## 3. 信息状态搜索(Information State Search) 182 | 183 | 探索与利用之间的根本冲突源于这样一个事实,即探索获得的信息可能有助于未来,但此刻来看是次优的。如果我们能量化这种“信息的价值(value of information)”,即我们应该准备为获取这些信息付出多少努力,那么我们就能更有效地平衡探索与利用。作为一个具体的例子,请参考幻灯片中的地震学家的例子。 184 | 185 | ### 3.1 信息状态空间(Information State Space) 186 | 187 | 到目前为止,我们将 MAB 视作有一个状态的完全可观测的 MDP。 188 | 189 | 主要思路:将 MAB 问题定义为一个部分可观测的 MDP (partially observable MDP),其中隐藏状态是每个动作的实际奖励,动作仍像以前一样对应于拉动手臂,我们得到的观测量为从隐藏状态中采样的奖励。因此,该 POMDP 的最优策略即为最优 bandit 算法,也就是说,MAB 可以简化为 POMDP 规划。 190 | 191 | POMDP 规划的一个主要思想是置信状态(belief state)$\tilde{s}$,它可以被看作我们上下文中的信息状态,这是 POMDP 隐藏状态的后验,即真实的平均回报。$\tilde{s}$ 是一个使用历史计算得到的统计值,即 $\tilde{s}_ {t}=f(h_{t})$。在信息(置信)状态空间中,每个动作及其相应的观测(奖励)都会导致状态以某个概率转移到一个新的状态 $\tilde{s}_ {t+1}$。这样的结果是扩充信息状态空间上的 MDP。 192 | 193 | ### 3.2 伯努利 Bandits(Bernoulli Bandits) 194 | 195 | 在简单的伯努利 Bandits 下,信息状态为每只手臂 1,0 奖励的计数。由于这些计数是无限的,我们现在在这些信息状态上有一个无限的 MDP。这个 MDP 可以由 RL 来解决,例如使用无模型方法如 Q-学习,或基于模型的方法如贝叶斯方法。这种方法被称作贝叶斯自适应 RL(Bayesian-adaptive RL),这里我们寻求贝叶斯最优的探索/利用权衡,即给定当前信息,选择最大化期望奖励的动作。 196 | 197 | 前面描述的用于伯努利 Bandits 的汤普森采样算法可以用这些术语来考虑,每个手臂的 $S_k$ 值和 $F_k$ 值表示信息状态以及对它们的更新和转换。 198 | 199 | #### 3.2.1 Gittins 指数(Gittins Index) 200 | 201 | 上述的贝叶斯自适应 MDP 可以通过动态规划方法求解,得到的结果称为 Gittins 指数(Gittins index)。由于状态空间的大小,这一问题的精确解通常很难得到,最近基于仿真的搜索被应用到这一问题中来尝试获得很好的结果。我们将在第 14 课学习这种仿真方法。 202 | 203 | ## 4. 应用于 MDP(Application to MDPs) 204 | 205 | 上述所有方法都可以扩展到成熟 MDP。 206 | 207 | ### 4.1 乐观初始化(Optimistic Initialization) 208 | 209 | 在无模型 RL 的情况下,使用价值函数的乐观初始化可以很好地鼓励系统性的探索。我们可以将 $Q(s,a)$ 初始化为 $\frac{R_{max}}{1-\gamma}$,然后执行 Q-学习、SARSA、蒙特卡洛等算法。较高的初始值可以确保每个 $(s,a)$ 对被探索多次,以获得对真实值更好的估计。 210 | 211 | 类似的想法对基于模型的方法也有效,这里我们构建一个乐观的 MDP 模型,并初始化状态转移到能够得到最多奖励的最终状态。RMax 算法 [[3]](#ref3) 就是这类算法的一个例子。 212 | 213 | ### 4.2 UCB:有模型的(UCB: Model Based) 214 | 215 | 类似于 MAB 情况,我们可以在可用动作中,选择一个最大化置信度上界的动作。 216 | 217 | $$ 218 | a_t = \mathop{\arg\max}_ {A} Q(s_t,a)+U_{1}(s_t,a)+U_{2}(s_t,a), 219 | $$ 220 | 221 | 这里 $U_1$ 为策略评估中的不确定量,易于量化,$U_2$ 源于策略提升,通常难以计算。 222 | 223 | ### 4.3 汤普森采样:基于模型的(Thompson Sampling: Model Based) 224 | 225 | 像之前那样,这个方法实现了概率匹配,即 226 | 227 | $$ 228 | \pi(a|h_t) = P[Q(s,a)>Q(s,a'),\forall a'\neq a|h_t]。 229 | \tag{13} 230 | $$ 231 | 232 | 我们可以利用贝叶斯定律来计算后验 $P[P,R|h_t]$,然后从该分布中采样一个 MDP,用规划算法求解并据此采取动作。 233 | 234 | ### 4.4 信息状态搜索(Information State Search) 235 | 236 | 与 MAB 情况一样,我们可以构造一个附加了 MDP 信息状态的扩展 MDP,从而得到一个扩展的状态空间来得到贝叶斯自适应 MDP。解决这个问题将给我们带来最优的探索/利用权衡。然而,状态空间的大小限制了们只能使用基于仿真的搜索方法。 237 | 238 | ## 5. 结论(Conclusion) 239 | 240 | 总的来说,有几种不同的探索方法,有些比其他的更有原则性。 241 | 242 | $\bullet$ 普通的探索方法:$\epsilon$-贪婪方法 243 | 244 | $\bullet$ 乐观初始化:想法很简单但通常效果很好 245 | 246 | $\bullet$ 乐观面对不确定性:偏好价值不确定的动作,如 UCB 247 | 248 | $\bullet$ 概率匹配:选择有最大概率是最优的动作,如 汤普森采样 249 | 250 | $\bullet$ 信息状态空间:建立并解决扩展 MDP,因此直接包含了信息的价值 251 | 252 | ## 参考文献 253 | 254 | 1. T. L. Lai, and H. Robbins, "Asymtotically efficient adaptive allocation rules," *Advances in Applied Mathematics*, 1985. 255 | 256 | 2. P. Auer, C. B. Nicolo, and P. Fischer, "Finite-time analysis of the multiarmed bandit problem," *Maching Learning*, 2002. 257 | 258 | 3. R. I. Brafman, and M. Tennenholtz, "R-max - a general polynomial time algorithm for near-optimal reinforcement learning," *Journal of Maching Learning Research*, 2002. -------------------------------------------------------------------------------- /docs/6.md: -------------------------------------------------------------------------------- 1 | # Lecture 6 CNNs and Deep Q-learning 2 | 3 | # 课时6 卷积神经网络与深度 Q-学习 2019.01.28 4 | 5 | ## 7. 基于值的深度强化学习(Value-based Deep Reinforcement Learning) 6 | 7 | 这一部分我们介绍三种常见的基于值的深度强化学习(RL)算法:深度 Q-网络(Deep Q-network)[[1]](#ref1),双 DQN(Double DQN)[[2]](#ref2) 和对抗 DQN(Dueling DQN)[[3]](#ref3)。通过端到端的(end-to-end)强化学习,这三种神经网络架构都可以直接从像电子游戏的预处理像素这样的高维输入(high-dimensional inputs)中学习到成功的策略,它们在 Atari 2600 的 49 个游戏中达到了相当于专业的游戏测试员的水平 [[4]](#ref4)。 8 | 9 | 这些架构都使用了卷积神经网络(Convolutional Neural Networks, CNNs)[[5]](#ref5) 来从像素输入中提取特征。理解 CNNs 特征提取的机制有助于我们理解 DQN 的工作方式。 斯坦福大学 CS231N 课程网站提供了关于 CNNs 的详尽的介绍和例子,通过这个链接,读者可以阅读更多细节:[CS231N](http://cs231n.github.io/convolutional-networks/)。本节的其余部分我们将关注 RL 和 基于值的深度 RL 算法的泛化。 10 | 11 | ### 7.1 概述:状态-行为值函数近似(Recap: Action-value Function Approximation) 12 | 13 | 上节课我们用参数化的近似函数来表示状态-行为值函数(Q-函数),若我们将参数记为 $\mathbf{w}$,那么这种设定中,Q-函数可以被表示为 $\hat{q}(s,a,\mathbf{w})$。 14 | 15 | 假设我们知道 $q(s,a)$,通过最小化真实状态-动作值函数 $q(s,a)$ 和近似估计之间的均方误差 $J(\mathbf{w})$,我们可以得到近似的 Q-函数: 16 | $$ 17 | J(\mathbf{w})=\mathbb{E}[(q(s,a)-\hat{q}(s,a,\mathbf{w}))^2]。 18 | \tag{1} 19 | $$ 20 | 21 | 我们可以通过随机梯度下降(stochastic gradient descent, SGD)找到 $J$ 的关于 $\mathbf{w}$ 的局部最小值,并且以下式来更新 $\mathbf{w}$: 22 | 23 | 24 | $$ 25 | \Delta(\mathbf{w})=-\frac{1}{2}\alpha\nabla_{\mathbf{w}}J(\mathbf{w})=\alpha\mathbb{E}[(q(s,a)-\hat{q}(s,a,\mathbf{w}))\nabla_{\mathbf{w}}\hat{q}(s,a,\mathbf{w})], 26 | \tag{2} 27 | $$ 28 | 29 | 这里 $\alpha$ 为学习率(learning rate)。通常情况下,真实的状态-动作值函数 $q(s,a)$ 是未知的,所以用一个近似的学习目标(learning target)来代替式([2](#eq2))中的 $q(s,a)$。 30 | 31 | 在蒙特卡洛方法中,对于片段式的 MDPs,我们用无偏的回报 $G_t$ 来代替学习目标: 32 | $$ 33 | \Delta(\mathbf{w})=\alpha(G_t-\hat{q}(s,a,\mathbf{w}))\nabla_{\mathbf{w}}\hat{q}(s,a,\mathbf{w})。 34 | \tag{3} 35 | $$ 36 | 37 | 对于 SARSA 方法,我们使用基于推导的 TD 目标 $r+\gamma\hat{q}(s',a',\mathbf{w})$: 38 | $$ 39 | \Delta(\mathbf{w})=\alpha(r+\gamma\hat{q}(s',a',\mathbf{w})-\hat{q}(s,a,\mathbf{w}))\nabla_{\mathbf{w}}\hat{q}(s,a,\mathbf{w}), 40 | \tag{4} 41 | $$ 42 | 这里 $a'$ 为在下一状态 $s'$ 执行的动作,$\gamma$ 为衰减因子,TD 目标利用了当前的函数近似值。 43 | 44 | 对于 Q-学习,我们使用 TD 目标 $r+\gamma\mathop{\max}_{a'}\hat{q}(s',a',\mathbf{w})$,并以下式来更新 $\mathbf{w}$: 45 | 46 | $$ 47 | \Delta(\mathbf{w})=\alpha(r+\gamma\mathop{\max}_ {a'}\hat{q}(s',a',\mathbf{w})-\hat{q}(s,a,\mathbf{w}))\nabla_{\mathbf{w}}\hat{q}(s,a,\mathbf{w})。 48 | \tag{5} 49 | $$ 50 | 51 | 下面的部分我们将介绍如何使用深度神经网络来近似 $\hat{q}(s,a,\mathbf{w})$,以及如何通过端到端的训练来学习神经网络的参数 $\mathbf{w}$。 52 | 53 | ### 7.2 泛化:深度 Q-网络(Generalization: Deep Q-network)[[1]](#ref1) 54 | 55 | 线性近似函数的表现非常依赖于特征的质量,一般来说,手动给出合适的特征很困难,也很耗时。为了在大的空间(large domians)(比如大的状态空间)进行决策并实现特征自动提取,深度神经网络(DNNs)通常被选作近似函数。 56 | 57 | #### 7.2.1 DQN 架构(DQN Architecture) 58 | 59 | 图1展示了 DQN 的结构,图中的网络将 Atari 游戏环境的预处理像素图(预处理见 7.2.2 节)作为输入,为每个可行的动作赋予一个 Q 值,将这些 Q 值作为一个向量输出。预处理的像素输入代表了游戏状态 $s$,单个的输出单元表示动作 $a$ 的 $\hat{q}$ 函数。总的来说,$\hat{q}$ 可以被记为 $\hat{q}(s,\mathbf{w})\in\mathbb{R}^{|A|}$,简单起见,我们仍用 $\hat{q}(s,a,\mathbf{w})$ 来表示对 $(s,a)$ 的状态-行为值估计。 60 | 61 | 结构细节:输入为一张 $84\times 84\times 4$ 的图片;第一个卷积层有 32 个大小为 $8\times 8$、步幅(stride)为 $4$ 的滤波器,对输入图片进行卷积操作后,将结果输入非线性整流器(ReLU)[[6]](#ref6);第二个隐藏层有 64 个大小为 $4\times 4$、步幅为 $2$ 的滤波器,同样地,后面接非线性整流器;第三个隐藏层有 64 个大小为 $3\times 3$、步幅为 $1$ 的滤波器,同样地,后面接 ReLU;最后的隐藏层为包含 512 个整流器(ReLU)单元的全连接层(fully-connected layer);输出层为全连接线性层。 62 | 63 |
64 | 65 |
66 | 图 1:深度 Q 网络:网络的输入为一张 $84\times 84\times 4$ 的预处理过的图片,后面接三个卷积层和两个全连接层,每个可行动作都对应一个输出,每个隐藏层都接一个非线性整流器(ReLU)[[6]](#ref6)。 67 |
68 | 69 | #### 7.2.2 原始像素预处理(Processing Raw Pixels) 70 | 71 | 原始的 Atari 2600 画面的大小为 $210\times 160\times 3$,最后的维度指的是 RGB 的通道数。[[1]](#ref1) 中采用的预处理步骤的目的是降低输入的维度以及处理游戏模拟器的一些工件。我们将预处理过程总结如下: 72 | 73 | $\bullet$ 单帧图片编码(single frame encoding):为编码单帧图片,我们将返回所编码帧和上一帧每个像素颜色值的最大值,也就是说,我们返回两帧连续的原始图片的每个像素的最大值(pixel-wise nax-pooling); 74 | 75 | $\bullet$ 降维(dimensionality reduction):从编码的 RGB 图像中提取 Y 通道(也称为亮度,luminance),将其重新缩放到 $(84\times 84\times 1)$; 76 | 77 | 对 4 帧最新的原始 RGB 图像应用上述处理过程,并将编码后的图片堆叠在一起,以生成 Q-网络的输入(大小为 $(84\times 84\times 1)$)。将最新的帧堆叠在一起来作为游戏状态也是将游戏环境转换为(近似)马尔科夫世界的一种方法。 78 | 79 | #### 7.2.3 DQN 的训练算法(Training Algorithms for DQN) 80 | 81 | 由于没有理论性的保证,而且学习和训练往往很不稳定,因此过去常常避免使用大型的深度神经网络来近似状态-行为值函数。为使用大型非线性近似函数和大规模在线 Q-学习,DQN 引入了两个主要变化:使用经验回放(experience replay)和一个单独的目标网络(target network),算法 1 展示了完整的算法。本质上,Q-网络是通过最小化以下均方误差来学习的: 82 | 83 | $$ 84 | J(\mathbf{w})=\mathbb{E}_ {(s_t,a_t,r_t,s_{t+1})}[(y_t^{DQN}-\hat{q}(s_t,a_t,\mathbf{w}))^2], 85 | \tag{6} 86 | $$ 87 | 88 | 这里 $y_t^{DQN}$ 为提前一步学习目标(one-step ahead learning target): 89 | $$ 90 | y_t^{DQN}=r_t+\gamma\mathop{\max}_ {a'}\hat{q}(s_{t+1},a',\mathbf{w}^-), 91 | \tag{7} 92 | $$ 93 | 94 | $\mathbf{w}^-$ 表示目标网络的参数,在线网络的参数 $\mathbf{w}$ 通过从曾经的状态转移 $(s_t,a_t,r_t,s_{t+1})$ 的小批量中采样梯度来更新。 95 | 96 | (注意:尽管目标网络可以根据 $\mathbf{w}^-$ 计算学习的目标,当我们更新 $\mathbf{w}$ 时,仍认为目标 $y_t^{DQN}$ 是固定的。) 97 | 98 |
99 | 100 | 经验回放(Experience Replay): 101 | 102 | 行为体在每个时间步的经历(或状态转移)$e_t=(s_t,a_t,r_t,s_{t+1})$ 被存储在一个固定大小的数据集(或回放存储器,replay buffer)中:$D_t={e_1,...,e_t}$,回放存储器保留着最新的 $k=1 million$ 次经历(图2解释了回放存储器)。通过从最小批数据中采样梯度来更新 Q-网络,最小批中的每个状态转移样本都是从经验存储池中随机均匀采样得到的:$(s,a,r,s')\sim U(D)$。对于标准的在线 Q-学习,这个方法有以下优点: 103 | 104 | $\bullet$ 更高的数据效率(greater data efficiency):每一步的经验都可能用于多次更新,从而提高数据效率; 105 | 106 | $\bullet$ 去除样本相关性(removing sample correlations):对状态转移的随机采样打破了连续样本间的联系,因此能降低更新的方差并稳定训练过程; 107 | 108 | $\bullet$ 避免震荡或发散(avoiding oscillations or divergence):表现分布根据许多的历史状态和状态转移做了平均,因此可以平滑学习过程,避免参数的震荡或发散。(注意,在使用经验回放时,应当使用离线策略方法,如 Q-学习,因为当前的参数与用于生成样本的参数不同。) 109 | 110 | 经验回放的限制(limitations of experience replay):回放存储器并不区分重要的或能提供有用信息的状态转移;由于存储容量固定,最旧的状态转移总是会被最新的状态转移覆盖;类似地,从存储器均匀采样赋予了存储的经历以同等的重要性。[[7]](#ref7) 提出了一种更为复杂的回放策略,即优先回放(Prioritized Replay),它更频繁地回放重要的状态转移,因此行为体的学习效率更高。 111 | 112 |
113 | 114 |
115 | 图 2:回放存储器:从回放存储器中对状态转移 $(s,a,r,s')$ 均匀采样,以更新 Q 网络。 116 |
117 | 118 | 目标网络(Target Network): 119 | 120 | 为了进一步提高学习的稳定性和处理非平稳的学习目标,在 Q-学习的更新中,DQN 采用了一个独立的目标网络来生成目标 $y_i$(算法 1 第 12 行)。具体来说,通过从在线网络 $\hat{q}(s,a,\mathbf{w})$ 复制参数值 $\mathbf{w}^-=\mathbf{w}$,每 $C$ 次更新/时间步更新一次目标网络 $\hat{q}(s,a,\mathbf{w}^-)$,在接下来的 $C$ 次更新中,目标网络保持不变并生成 $y_i$。这项改动使算法相对标准的在线 Q-学习 而言更加稳定,在原始的 DQN 中,$C=10000$。 121 | 122 | #### 7.2.4 训练细节(Training Details) 123 | 124 | 原始的 DQN 论文 [[1]](#ref1) 用每个游戏以相同的结构训练了一个不同的网络(或行为体)来学习算法和超参数。作者们将游戏环境中的正面奖励都设置为 $+1$,负面奖励都设置为 $-1$,这使得行为体可以在不同的游戏中都使用相同的学习率。对于有生命计数器的游戏(例如 Breakout),模拟器还返回游戏中剩余的生命数,然后通过明确地设置未来奖励为零来标志训练期间一个片段的结束。他们还使用了一种简单的跳帧技术(或动作重复):行为体每 4 帧选择一次动作并重复这个动作直到下一次选择,而非每帧都选择。这降低了做决策的频率而不会对性能造成太大影响,并且使行为体在训练期间可以多玩大约 4 倍的游戏。 125 | 126 | [[1]](#ref1) 使用了 RMSProp(https://www.cs.toronto.edu/~tijmen/csc321/slides/lecture_slides_lec6.pdf )来训练 DQN,最小批的大小为 32。训练过程中,他们应用了 $\epsilon$-贪婪策略,前一百万时间步中,$\epsilon$ 从 $1.0$ 线性地减小到 $0.1$,后面 $\epsilon$ 一直保持为 $0.1$。回放存储器存储最新的一百万次状态转移。测试过程中,他们使用 $\epsilon=0.05$ 的 $\epsilon$-贪婪策略。 127 | 128 | ### 7.3 降低偏移:双深度 Q-网络(Reducing Bias: Double Deep Q-network)[[2]](#ref2) 129 | 130 | DQN 中的最大值算子(算法 1 第 12 行)使用了相同的网络值来选择并评估一个动作,这个设定使得选择高估计值的可能性更大,并导致目标值估计过于乐观。 H. Van 等人在 [[2]](#ref2) 也展示了 DQN 算法在 Atari 2600 的某些游戏中存在严重的高估问题。为了避免高估并降低偏差,我们可以将动作选择与动作评估分离。 131 | 132 | 回忆双 Q-学习(Double Q-learning),有两个状态-行为值函数,我们通过随机分配状态转移来更新这两个函数中的一个,从而产生两组不同的函数参数,分别记为 $\mathbf{w}$ 和 $\mathbf{w}'$。为了计算目标,我们用其中一个函数来选择贪婪动作,用另一个函数来评估其价值: 133 | $$ 134 | y_t^{DoubleQ}=r_t+\gamma \hat{q}(s_{t+1},\mathop{\arg\max}_ {a'}\hat{q}(s_{t+1},a',\mathbf{w}),\mathbf{w}')。 135 | \tag{8} 136 | $$ 137 | 138 | 注意,我们根据函数参数 $\mathbf{w}$ 选择动作($\mathop{\arg\max}$),根据另一组参数 $\mathbf{w}'$ 评估动作价值。 139 | 140 | 在计算目标的过程中的通过将动作选择与动作评估分离从而避免高估的想法也可以被扩展到深度 Q-学习。DQN 结构中的目标网络在不引入额外网络的情况下,为第二个状态-行为值函数提供了一个自然的候选,同样地,贪婪动作是根据参数为 $\mathbf{w}$ 的在线网络生成的,但其值是根据参数为 $\mathbf{w}^-$ 目标网络估计的,这样产生的算法被称作双 DQN (Double DQN),它仅仅用下式替换了算法 1 的第 12 行: 141 | $$ 142 | y_t^{DoubleDQN}=r_t+\gamma \hat{q}(s_{t+1},\mathop{\arg\max}_ {a'}\hat{q}(s_{t+1},a',\mathbf{w}),\mathbf{w}^-)。 143 | \tag{9} 144 | $$ 145 | 146 | 目标网络的更新与 DQN 保持不变,并定期地复制在线网络参数 $\mathbf{w}$,DQN 算法的其余部分保持不变。 147 | 148 | ### 7.4 :将值函数与优势函数分开:对抗网络(Decoupling Value and Advantage: Dueling Network)[[3]](#ref3) 149 | 150 | #### 7.4.1 对抗网络结构(The Dueling Network Architecture) 151 | 152 | 在探索对抗式结构前,我们首先介绍一个重要的量:优势函数(advantage function),这个函数与状态值函数和 Q 函数有关(假设遵循策略 $\pi$): 153 | $$ 154 | A^{\pi}(s,a)=Q^{\pi}(s,a)-V^{\pi}(s)。 155 | \tag{10} 156 | $$ 157 | 158 | 因为 $V^{\pi}(s)=\mathbb{E}_ {a\sim\pi(s)}[Q^{\pi}(s,a)]$,我们有 $\mathbb{E}_{a\sim\pi(s)}[A^{\pi}(s,a)]=0$。直观地说,优势函数从 Q 函数减去了状态值函数,得到了每个动作的重要性的相对度量。 159 | 160 | 就像 DQN 那样,对抗网络也是一个为了学习 Q 函数的 DNN 近似函数。不同的是,它通过将值函数和优势函数分开来近似 Q 函数,图 3 比较了对抗网络的结构和 DQN 的结构。 161 | 162 |
163 | 164 |
165 | 图 3:深度 Q 网路(顶部)和对抗 Q 网络(底部)。对抗网络分别估计状态值函数 $V(s)$(标量)和优势函数 $A(s,a)$(对每个动作);绿色的输出模块执行式([13](#eq13))以将这两个输出合并。两个网络都为每个动作输出 Q 值。 166 |
167 | 168 | 对抗网络的底层是卷积神经网络,然而,它使用了两个全连接层来估计 Q 值,而不是用一个全连接层。其中一个层提供状态值函数,另一个层对每个可行的动作估计优势函数,最后,将这两个层合并以得到近似的 Q 函数。像 DQN 那样,网络的输出为 Q 值向量,每个元素对应一个动作。 169 | 170 | 注意,由于对抗网络的输入和最终输出(两个层的合并)和原始的 DQN 一样,所以上面介绍的训练算法(算法 1)也可以被用来训练对抗网络。分开的两个层是根据作者的观察或灵感来设计的: 171 | 172 | $\bullet$ 对于许多状态,我们并不一定要估计所有可能的动作的价值。在某些状态时,动作的选择非常重要;而在很多状态下,选择什么动作对接下来发生的事情并没有影响。另一方面,对每个状态的价值估计对基于引导的算法(如 Q 学习)而言非常重要。 173 | 174 | $\bullet$ 确定价值函数所需的特征可能不同于用于准确估计动作价值的特征。 175 | 176 | 将两个全连接层合并来估计 Q 值并不是一项简单的任务,实际上,这个合并模块(aggregating module)(图 3 中的绿线)需要非常周到的设计,我们将在下一小节介绍这一点。 177 | 178 | #### 7.4.2 Q 值估计(Q-value Estimation) 179 | 180 | 根据优势函数(10)的定义,我们有 $Q^{\pi}(s,a)=A^{\pi}(s,a)+V^{\pi}(s)$,以及 $\mathbb{E}_ {a\sim\pi(s)}[A^{\pi}(s,a)]=0$。另外,对于确定性策略(deterministic policy)(常用于基于值的深度 RL),$a^{\ast}=\mathop{\arg\max}_{a'\in A}Q(s,a')$,由于 $Q(s,a^{\ast})=V(s)$,我们有 $A(s,a^{\ast})=0$,这种情况下,贪婪动作的优势函数为零。 181 | 182 | 考虑图 3 中的对抗网络结构,记其中一个全连接层的标量值函数输出为 $\hat{v}(s,\mathbf{w},\mathbf{w}_ {\mathbf{V}})$,记另一个全连接层的向量优势函数输出为 $A(s,a,\mathbf{w},\mathbf{w}_ {\mathbf{A}})$,这里我们用 $\mathbf{w}$ 表示卷积层的共享参数,用 $\mathbf{w}_ {\mathbf{V}}$ 和 $\mathbf{w}_ {\mathbf{A}}$ 表示两个全连接层的参数,那么。合并模块的最简单的设计方式可能就是下式这样: 183 | 184 | 185 | $$ 186 | \hat{q}(s,a,\mathbf{w},\mathbf{w}_ {\mathbf{A}},\mathbf{w}_ {\mathbf{V}})=\hat{v}(s,\mathbf{w},\mathbf{w}_ {\mathbf{V}})+A(s,a,\mathbf{w},\mathbf{w}_ {\mathbf{A}})。 187 | \tag{11} 188 | $$ 189 | 190 | 191 | 这样设计的主要问题是式([11](#eq11))不是无法确认的,给定 $\hat{q}$,我们不能唯一地得到 $\hat{v}$ 和 $A$,比如,将 $\hat{v}$ 加上一个常数并将 $A$ 减去一个相同的常数,我们能得到一样的 Q 值估计。无法确认的问题在实践中会导致差的表现。 192 | 193 | 为了使 Q 函数是可以确认的,回忆上面讨论的确定性策略的情况,我们可以强制优势函数在选择的动作下的估计为零,那么,我们有: 194 | 195 | 196 | $$ 197 | \hat{q}(s,a,\mathbf{w},\mathbf{w}_ {\mathbf{A}},\mathbf{w}_ {\mathbf{V}})=\hat{v}(s,\mathbf{w},\mathbf{w}_ {\mathbf{V}})+(A(s,a,\mathbf{w},\mathbf{w}_ {\mathbf{A}}) - \mathop{\max}_ {a'\in A}A(s,a',\mathbf{w},\mathbf{w}_{A}))。 198 | \tag{12} 199 | $$ 200 | 201 | 202 | 对于一个确定性策略,$a^{ast}=\mathop{\arg\max}_ {a'\in A}\hat{q}(s,a',\mathbf{w},\mathbf{w}_ {\mathbf{A}},\mathbf{w}_ {\mathbf{V}})=\mathop{\arg\max}_ {a'\in A}A(s,a',\mathbf{w},\mathbf{w}_ {\mathbf{A}})$,式([12](#eq12))说明 $\hat{q}(s,a^{ast},\mathbf{w},\mathbf{w}_ {\mathbf{A}}=\hat{v}(s,\mathbf{w},\mathbf{w}_ {\mathbf{V}})$,因此,全连接层输出的 $\hat{v}$ 为值函数的估计,另一个全连接层的输出 $A$ 为优势函数的估计。 203 | 204 | [[3]](#ref3) 中作者还提出了另一种合并模块的设计:用平均算子代替最大值算子, 205 | 206 | 207 | $$ 208 | \hat{q}(s,a,\mathbf{w},\mathbf{w}_ {\mathbf{A}},\mathbf{w}_ {\mathbf{V}})=\hat{v}(s,\mathbf{w},\mathbf{w}_ {\mathbf{V}})+(A(s,a,\mathbf{w},\mathbf{w}_ {\mathbf{A}}) - \frac{1}{|A|}A(s,a',\mathbf{w},\mathbf{w}_{A}))。 209 | \tag{13} 210 | $$ 211 | 212 | 213 | 尽管这种设计在某种意义上失去了 $\hat{v}$ 和 $A$ 的原始含义,但作者认为它提升了学习的稳定性:优势函数只需要随着平均值的变化而变化,而不必对最优动作的优势函数的改变做出补偿,因此,对抗网络 [[3]](#ref3) 中的合并模块是根据式([13](#eq13))实现的,当做出动作时,通过评估优势函数便足以做出决策。 214 | 215 | 对抗网络的优点在于它能够有效地逼近价值函数,动作的数量越多,这一优点相对单个全连接层的 Q 网络而言就越为明显,并且在 2016 年,对抗网络就在 Atari 游戏上取得了最佳结果。 216 | 217 | ## 参考文献 218 | 219 | 1. V. Mnih et al., "Human-level control through deep reinforcement learning," *Nature* 518(7540): 529-533, 2015. 220 | 221 | 2. H. van Hasselt, A. Guez, and D. Silver, "Deep reinforcement learning with double q-learning," *AAAI*, 2016. 222 | 223 | 3. Z. Wang et al., "Dueling network architectures for deep reinforcement learning," *arXiv preprint arXiv: 1511.06581*, 2015. 224 | 225 | 4. M. G. Bellemare et al., "The arcade learning environment: an evaluation platform for general agents," *IJCAI*, 2015. 226 | 227 | 5. A. Krizhevsky, I. Sutskever, and G. F. Hinton, "Imagenet classification with deep convolutional neural networks," *NIPS*, 2012. 228 | 229 | 6. V. Nair, and G. E. Hinton, "Rectified linear units improve restricted boltzmann machines," *ICML*, 2010. 230 | 231 | 7. T. Schaul et al., "Prioritized experience replay," *arXiv preprint arXiv: 1511.05952*, 2016. -------------------------------------------------------------------------------- /docs/3.md: -------------------------------------------------------------------------------- 1 | # Lecture 3 Model Free Policy Evaluation: Policy Evaluation Without Knowing How the World Works 2 | 3 | # 课时3 无模型策略评估 2019.01.14 4 | 5 | ## 4. 无模型策略评估 6 | 7 | 回忆上节内容,我们讨论了三个愈加复杂的问题: 8 | 9 | 1. 马尔可夫过程(MP)是一个具有马尔可夫性质的随机过程。 10 | 11 | 2. 马尔可夫奖励过程(MRP)是一个每个时间步都有奖励的马尔可夫过程,随时间衰减的奖励的累积成为价值(values)。 12 | 13 | 3. 马尔可夫决策过程(MDP)是一个在每个状态下都有选择或动作(actions)的马尔可夫奖励过程。 14 | 15 | 在上节课的下半部分,我们讨论了在 MDP 中评估一个策略的两种方法,分别是:直接解线性方程组和动态规划(dynamic programming);我们也讨论了在 MDP 中寻找最优策略的三种方法,分别是:暴力策略搜索(brute force policy search)、策略迭代(policy iteration)和价值迭代(value iteration)。 16 | 17 | 这些方法都隐含这样一个假设,即每个状态转移的奖励和概率已知。然而,在许多情况下,这些信息并不容易被获取,因此我们需要无模型算法(model-free algorithms)。本课时我们讨论无模型策略评估(model-free policy evaluation),也就是说,在不知道奖励或状态转移概率的情况下,给定一个策略,我们对其价值进行评估。本课时我们暂时不讨论如何在无模型的情况下提升策略。 18 | 19 | ### 4.1 符号摘要 20 | 21 | 在深入讨论无模型策略评估方法之前,我们首先回顾一下本次课需要的有关 MDP 的一些符号。 22 | 23 | 我们将 MRP 的回报定义为随时间衰减的奖励的累积,起于时间步 $t$,止于 $H$,这里 $H$ 可能是无穷大,用数学的方式表示为 24 | $$ 25 | G_t=\sum_{i=t}^{H-1}\gamma^{i-t}r_i, 26 | \tag{1} 27 | $$ 28 | 这里 $0\leq t\leq H-1$,$\gamma$ 为衰减因子,$0<\gamma\leq 1$,$r_i$ 为在时间步 $i$ 的奖励。对于一个 MDP,回报 $G_t$ 被唯一定义,奖励 $r_i$ 根据策略 $\pi(a|s)$ 生成。 29 | 30 | 状态值函数 $V^{\pi}(s)$ 是起始于状态 $s$、遵照固定的策略 $\pi$ 得到的期望回报,我们可以将其表示为 31 | $$ 32 | V^{\pi}(s)=\mathbb{E}_{\pi}[G_t|s_t=s] 33 | \tag{2} 34 | $$ 35 | 36 | $$ 37 | =\mathbb{E}_ {\pi}[\sum_{i=t}^{H-1}\gamma^{i-t}r_i|s_t=s]。 38 | \tag{3} 39 | $$ 40 | 41 | 状态-行为值函数 $Q^{\pi}(s,a)$ 是起始于状态 $s$ 和动作 $a$、然后遵照固定的策略 $\pi$ 得到的期望回报,我们可以将其表示为 42 | $$ 43 | Q^{\pi}(s,a)=\mathbb{E}_{\pi}[G_t|s_t=s,a_t=a] 44 | \tag{4} 45 | $$ 46 | 47 | $$ 48 | =\mathbb{E}_ {\pi}[\sum_{i=t}^{H-1}\gamma^{i-t}r_i|s_t=s,a_t=a]。 49 | \tag{5} 50 | $$ 51 | 52 | 在整个课程中,我们假设时间步有限,并且假设奖励、转移概率和策略是固定的,这些假设使得状态值函数和状态-行为值函数是独立于时间的,就像上节课推导的那样。 53 | 54 | 接下来我们介绍一个新的定义:历史(history)。 55 | 56 | **定义 4.1** 历史(history)是行为体经历的状态、动作和奖励的排列。在情节域(episodic domains)中,我们也称之为片段(episode)。在考虑到许多交互时,我们用以下方式表示历史:第 $j$ 个历史为 57 | $$ 58 | h_j=(s_{j,1},a_{j,1},r_{j,1},s_{j,2},a_{j,2},r_{j,2},...,s_{j,L_j}), 59 | $$ 60 | 这里 $L_j$ 是交互的长度,$s_{j,t}$、$a_{j,t}$、$r_{j,t}$分别是历史 $j$ 中的在时间步 $t$ 时的状态、动作和奖励。 61 | 62 | ### 4.2 动态规划(Dynamic Programming) 63 | 64 | 回忆上节课的用来计算有限 MDP 的值的动态规划算法。 65 | 66 |
67 | 68 | 以这种方式来写时,我们可以以多种方式来考虑 $V_k$。首先,$V_k(s)$ 是起始于状态 $s$,接下来 $k$ 次状态转移遵循策略 $\pi$ 的确切的值。其次,对于 $k$ 值很大或算法 1 终止的情况,$V_k(s)$ 是真实的无穷时间步情况下的 $V^{\pi}(s)$ 的估计。 69 | 70 | 我们可以通过图 1 来表示该算法。我们自上而下阅读这幅图,这里白色圆圈代表状态,黑色圆圈代表动作,弧线表示取期望值。这个图展示了起始于状态 $s$ 并转移两个时间步的分支效果,还说明了在状态 $s$ 依据策略 $\pi$ 采取一个动作后,我们如何去计算下一状态的值的期望。在动态规划中,我们使用当前的估计值 $V_{k-1}(s')$ 引导(bootstrap)或估计下一状态的值。 71 | 72 |
73 | 74 |
75 | 图 1:动态规划策略评估算法的备份示意图。 76 |
77 |
78 | 79 | ### 4.3 蒙特卡洛在线策略评估(Monte Carlo On Policy Evaluation) 80 | 81 | 我们现在通过一个常见的计算方法来描述第一个无模型策略评估算法,这个方法被称为蒙特卡洛方法。我们首先抛开强化学习,详细叙述一个使用蒙特卡洛方法的例子,然后比较全面地讨论蒙特卡洛方法,最后在强化学习中应用蒙特卡洛方法。需要强调的是,这种方法仅在情节的环境中有效,我们将在本节中更仔细地研究算法时看到为什么会这样。 82 | 83 | 假设我们想估计今天从你家到斯坦福大学的通勤时间。假设我们有一个模拟器,这个模拟器可以模拟交通、天气、施工延误和其他变量的不确定性以及它们之间的相互作用。估计预期通勤时间的一种方法是在模拟器上模拟许多次,然后对这些仿真的通勤时间取平均值。这被称为我们的通勤时间的蒙特卡洛估计。 84 | 85 | 一般来说,我们在现实生活或仿真中通过观察多个迭代得到某个量的蒙特卡洛估计,然后将这些观测值取平均。根据大数定律,这个平均值会收敛到这个量的期望值。 86 | 87 | 在强化学习中,我们希望估计的量是 $V^{\pi}(s)$,也就是起始于状态 $s$、遵循策略 $\pi$ 的回报 $G_t$ 的平均值。因此,我们可以通过三步来得到 $V^{\pi}(s)$ 的蒙特卡洛估计: 88 | 1. 执行策略 $\pi$,直到终止。多次进行这个过程(rollout)。 89 | 2. 记录我们观察到的回报 $G_t$(起始于状态 $s$)。 90 | 3. 对我们获取的 $G_t$ 取平均值来估计 $V^{\pi}(s)$。 91 | 92 | 图 2 展示了蒙特卡洛策略评估,新的蓝色的线表示我们起始于状态 $s$,对整个片段进行采样直到终止。 93 | 94 |
95 | 96 |
97 | 图 2:蒙特卡洛策略评估算法的备份示意图。 98 |
99 |
100 | 101 | 蒙特卡洛在线策略评估有两种方式,取决于每个 rollout 中,我们是只在第一次遇到一个状态时取平均值,还是每次遇到一个状态时都取平均值。这两种方式分别被称为首次访问(First-Visit)蒙特卡洛策略评估和每次访问(Every-Visit)蒙特卡洛策略评估。 102 | 103 | 更正式地,我们用算法 2 描述首次访问蒙特卡洛,用算法 3 描述每次访问蒙特卡洛。 104 | 105 |
106 | 107 |
108 | 109 | 注意,在算法 2 和 3 的 for 循环中,我们可以移去向量 $S$,并且以 110 | $$ 111 | V^{\pi}(s_{j,t})\leftarrow V^{\pi}(s_{j,t})+\frac{1}{N(s_{j,t})}(G_{j,t}-V^{\pi}(s_{j,t})) 112 | \tag{6} 113 | $$ 114 | 代替 $V^{\pi}(s_{j,t})$ 的更新。这是因为新的平均值可以用以下方式来计算: 115 | $$ 116 | \frac{V^{\pi}(s_{j,t})\times (N(s_{j,t})-1)+G_{j,t}}{N(s_{j,t})}=V^{\pi}(s_{j,t})+\frac{1}{N(s_{j,t})}(G_{j,t}-V^{\pi}(s_{j,t}))。 117 | \tag{7} 118 | $$ 119 | 用 $\alpha$ 替代 $\frac{1}{N(s_{j,t})}$,我们就得到了更一般的增量蒙特卡洛策略评估(Incremental Monte Carlo On Policy Evaluation)。算法 4 和算法 5 分别详细描述了首次访问和每次访问蒙特卡洛。 120 | 121 |
122 | 123 |
124 | 125 | 若我们使 $\alpha=\frac{1}{N(s_{j,t})}$,便得到了算法 2 和算法 3 中展示的原始的蒙特卡洛策略评估;若$\alpha>\frac{1}{N(s_{j,t})}$,则表示我们为新数据赋予更大的权重,这有助于学习非线性空间。如果我们是在确切的马尔可夫空间,每次访问蒙特卡洛将会是更加数据高效的,因为每次我们访问一个状态都会更新我们的回报平均值。 126 | 127 | **练习 4.1** 回忆上节课提到的火星探测器(Mars Rover)MDP,如图 3 所示。假设我们当前对各个状态的估计值为 $0$,如果我们经历历史 128 | $$ 129 | h=(S3,TL,+0,S2,TL,+0,S1,TL,+1,terminal), 130 | $$ 131 | 那么: 132 | 1. 每个状态的 $V$ 的首次访问蒙特卡洛估计是什么? 133 | 2. 每个状态的 $V$ 的每次访问蒙特卡洛估计是什么? 134 | 3. 当 $\alpha=\frac{2}{3}$ 时,$V$ 的增量首次访问蒙特卡洛估计是什么? 135 | 4. 当 $\alpha=\frac{2}{3}$ 时,$V$ 的增量每次访问蒙特卡洛估计是什么? 136 | 137 |
138 | 139 |
140 | 图 3:火星探测器马尔可夫决策过程,这里的动作为尝试左移(Try Left)与尝试右移(Try Right)。 141 |
142 |
143 | 144 | ### 4.4 蒙特卡洛离线策略评估(Monte Carlo Off Policy Evaluation) 145 | 146 | 上一节我们讨论了在希望评估的策略 $\pi$ 下,我们能够获得许多 $G_t$ 的实现的情况。然而在许多昂贵或高风险的情况下,我们并不能以期望评估的策略去获取 $G_t$ 的 rollout,例如,我们可能有与某一项医疗政策相关的数据,但我们想要确定另一个不同的医疗策略的价值。在本节中,我们讨论蒙特卡洛离线策略评估(Monte Carlo off policy evaluation),这是一种用从一个策略中获得的数据去评估另一个不同的策略的方法。 147 | 148 | #### 4.4.1 重要性采样(Importance Sampling) 149 | 150 | 重要性采样(importance sampling)方法是离线策略评估的关键因素。重要性采样的目标是仅使用 $f(x_1), ..., f(x_n)$ 估计函数 $f(x)$ 的期望值,这里 $x$ 源于分布 $q$,$x_i$ 源于另一个不同的分布 $p$。总而言之,对于 $1\leq x_i \leq n$,给定 $q(x_i),p(x_i),f(x_i)$,我们想要估计 $\mathbb{E}_{x\sim q}[f(x)]$。我们可以通过以下近似来实现这个估计: 151 | 152 | $$ 153 | \mathbb{E}_{x\sim q}[f(x)]=\int_x {q(x)f(x)} \text{ d}x 154 | \tag{8} 155 | $$ 156 | 157 | $$ 158 | =\int_x {p(x)[\frac{q(x)}{p(x)}f(x)]} \text{ d}x 159 | \tag{9} 160 | $$ 161 | 162 | $$ 163 | =\mathbb{E}_{x\sim p}[\frac{q(x)}{p(x)}f(x)] 164 | \tag{10} 165 | $$ 166 | 167 | $$ 168 | \approx \frac{1}{n}\sum_{i=1}^{n}[\frac{q(x_i)}{p(x_i)}f(x_i)]。 169 | \tag{11} 170 | $$ 171 | 最后的式子给出了用分布 $p$ 下 $f$ 的样本对分布 $q$ 下 $f$ 的重要性采样估计。注意第一步仅在对于任意 $x$,$q(x)f(x)>0$ 表示 $p(x)>0$ 时才成立。 172 | 173 | #### 4.4.2 离线策略评估的重要性采样(Importance Sampling for Off Policy Evaluation) 174 | 175 | 我们现在在强化学习中应用重要性采样估计的一般结果。我们想要用在策略 $\pi_2$ 下生成的 $n$ 个历史 $h_1,...,h_n$ 来近似在策略 $\pi_1$ 下状态 $s$ 的值,即 $V^{\pi_1}(s)=\mathbb{E}[G_t|s_t=s]$,重要性采样估计告诉我们 176 | $$ 177 | V^{\pi_1}(s)\approx \frac{1}{n}\sum_{j=1}^{n}\frac{p(h_j|\pi_1,s)}{p(h_j|\pi_2,s)}G(h_j), 178 | \tag{12} 179 | $$ 180 | 这里 $G(h_j)=\sum_{t=1}^{L_j-1}\gamma^{t-1}r_{j,t}$ 是对于历史 $h_j$ 的衰减奖励总额。 181 | 182 | 现在,对于一个一般的策略 $\pi$,我们知道在策略 $\pi$ 下经历历史 $h_j$ 的概率是 183 | $$ 184 | p(h_j|\pi,s=s_{s,1})=\prod_{t=1}^{L_j-1}p(a_{j,t}|s_{j,t})p(r_{j,t}|s_{j,t},a_{j,t})p(s_{j,t+1}|s_{j,t},a_{j,t}) 185 | \tag{13} 186 | $$ 187 | 188 | $$ 189 | =\prod_{t=1}^{L_j-1}\pi(a_{j,t}|s_{j,t})p(r_{j,t}|s_{j,t},a_{j,t})p(s_{j,t+1}|s_{j,t},a_{j,t}) 190 | \tag{14} 191 | $$ 192 | 这里 $L_j$ 是第 $j$ 个片段的长度,第一行由每个状态转移的三个部分得到,这三个部分分别为: 193 | 1. $p(a_{j,t}|s_{j,t})$ - 在状态 $s_{j,t}$ 采取动作 $a_{j,t}$ 的概率; 194 | 2. $p(r_{j,t}|s_{j,t},a_{j,t})$ - 在状态 $s_{j,t}$ 采取动作 $a_{j,t}$ 后获得奖励 $r_{j,t}$ 的概率; 195 | 3. $p(s_{j,t+1}|s_{j,t},a_{j,t})$ - 在状态 $s_{j,t}$ 采取动作 $a_{j,t}$ 后,状态转移到 $s_{j,t+1}$ 的概率。 196 | 197 | 将对 $V^{\pi_1}(s)$ 的重要性采样估计和历史概率 $p(h_j|\pi,s=s_{j,1})$ 的分解相结合,我们得到 198 | $$ 199 | V^{\pi_1}(s)\approx \frac{1}{n}\sum_{j=1}^{n}\frac{p(h_j|\pi_1,s)}{p(h_j|\pi_2,s)}G(h_j) 200 | \tag{15} 201 | $$ 202 | 203 | $$ 204 | =\frac{1}{n}\sum_{j=1}^{n} \frac{\prod_{t=1}^{L_j-1}\pi_1(a_{j,t}|s_{j,t})p(r_{j,t}|s_{j,t},a_{j,t})p(s_{j,t+1}|s_{j,t},a_{j,t})} {\prod_{t=1}^{L_j-1}\pi_2(a_{j,t}|s_{j,t})p(r_{j,t}|s_{j,t},a_{j,t})p(s_{j,t+1}|s_{j,t},a_{j,t})} G(h_j) 205 | \tag{16} 206 | $$ 207 | 208 | 209 | $$ 210 | =\frac{1}{n}\sum_{j=1}^{n} G(h_j) \prod_{t=1}^{L_j-1} \frac{\pi_1(a_{j,t}|s_{j,t})} {\pi_2(a_{j,t}|s_{j,t})}。 211 | \tag{17} 212 | $$ 213 | 214 | 215 | 注意,现在我们可以显式地估计表达式,而不需要状态转移概率或奖励,因为式中第二步所涉及的模型动态部分都被约掉了。特别地,我们已经知道历史 $h_j$,所以我们可以计算 $G(h_j)=\sum_{t=1}^{L_j-1}\gamma^{t-1}r_{j,t}$,并且我们已经知道策略 $\pi_1$ 和 $\pi_2$,所以我们也可以计算式([17](#eq17))中的第二项。 216 | 217 | ### 4.5 时间差分学习(Temporal Difference (TD) Learning) 218 | 219 | 到目前为止,我们已经有两种方法进行策略评估:动态规划和蒙特卡洛。动态规划利用引导来帮助我们只需要一步的备份去获取价值估计。另一方面,蒙特卡洛对许多轨迹采样,使我们不需要知道模型也可以估计价值。现在我们介绍另一个无模型策略评估算法,该算法结合了引导和采样。 220 | 221 | 为了明白如何将采样和引导结合,让我们看一下增量蒙特卡洛更新: 222 | $$ 223 | V^{\pi}(s_t)\leftarrow V^{\pi}(s_t)+\alpha(G_t-V^{\pi}(s_t)) 224 | \tag{18}。 225 | $$ 226 | 227 | $G_t$ 是在状态 $s_t$ 从时间步 $t$ 直到终止执行某个策略所获得的回报。现在像在动态规划中那样,我们用一个贝尔曼备份算子替代 $G_t$,也就是用 $r_t+\gamma V^{\pi}(s_{t+1})$ 替代 $G_t$,这里 $r_t$ 是在时间步 $t$ 的奖励的采样,$V^{\pi}(s_{t+1})$ 是当前对下一状态的值的估计。通过这一替换,我们得到了时间差分学习(TD-learning)更新: 228 | $$ 229 | V^{\pi}(s_t)\leftarrow V^{\pi}(s_t)+\alpha(r_t+\gamma V^{\pi}(s_{t+1})-V^{\pi}(s_{t}))。 230 | \tag{19} 231 | $$ 232 | 233 | 差分 234 | $$ 235 | \delta_t=r_t+\gamma V^{\pi}(s_{t+1})-V^{\pi}(s_{t}) 236 | \tag{20} 237 | $$ 238 | 通常被称为时间差分(TD-error);奖励的采样加上下一状态的值的估计 239 | $$ 240 | r_t+\gamma V^{\pi}(s_{t+1}) 241 | \tag{21} 242 | $$ 243 | 则被称为时间差分目标(TD-target)。完整的时间差分学习算法如算法 6 所示。我们可以看到,使用这种方法,我们在观测到状态转移 $(s_t,a_t,r_t,s_{t+1})$ 后直接更新 $V^{\pi}(s_{t})$,而不是像蒙特卡洛那样等到结束才更新。 244 | 245 |
246 | 247 | 我们可以通过图标再次检验这个算法,如图 4。通过蓝色线表示我们从状态 $s$ 开始采样一个状态转移,然后我们用当前对下一状态的估计去估计下一状态的值,从而建立一个完整的贝尔曼备份估计。 248 | 249 |
250 | 251 |
252 | 图 4:TD 学习策略评估算法的备份示意图。 253 |
254 |
255 | 256 | 实际上,我们可以通过一种方法将蒙特卡洛和动态规划融合,这种方法就是 TD($\lambda$)。当 $\lambda=0$ 时,我们得到如上所示的TD学习公式,因此我们也可以称之为 TD($0$)。当 $\lambda=1$ 时,我们就得到了蒙特卡洛策略估计。当 $0<\lambda<1$ 时,我们得到了两种方法的融合。关于 TD($\lambda$) 更细致的讨论,感兴趣的读者请参考 Sutton and Barto [[1]](#ref1) 的 7.1 节和 12.1-12.5 节,这两部分分别介绍 $n$ 步 TD 学习和 TD(\lambda)/资格迹(eligibility traces)。 257 | 258 | **练习 4.2** 继续考虑图 3 所示的火星探测器(Mars Rover)例子。假设当前我们对各个状态的值的估计为 $0$,如果我们经历历史 259 | $$ 260 | h=(S3,TL,+0,S2,TL,+0,S2,TL,+0,S1,TL,+1,terminal), 261 | $$ 262 | 那么: 263 | 1. 当 $\alpha=1$ 时,$V$ 的 TD($0$) 估计是什么? 264 | 2. 当 $\alpha=\frac{2}{3}$ 时,$V$ 的 TD($0$) 估计是什么? 265 | 266 | ### 4.6 总结 267 | 268 | 在这节课中,我们用上节课的动态规划重新考察了策略估计,并介绍了策略估计的两种方法,分别是蒙特卡洛估计和时间差分(TD)学习。 269 | 270 | 首先,我们注意到动态规划依赖于世界的模型,从而引入蒙特卡洛和 TD 学习。也就是说,动态规划策略评估算法需要我们提供奖励和状态转移概率,蒙特卡洛和 TD 学习都不受这一限制,因此它们都是无模型方法。 271 | 272 | 在蒙特卡洛策略评估中,我们生成许多历史,然后对遇到的状态求平均回报。为了在有限的时间内生成这些历史,我们需要域是片段式的,也就是说我们需要保证观测到的历史有终点。在动态规划和时间差分学习中,我们只需要备份一次状态转移(我们只看未来一步),所以我们不需要关心历史是否有终点,而且我们可以将这些算法应用到非片段式的域中。 273 | 274 | 另一方面,在动态规划和 TD 学习中,我们可以只备份一次状态转移的原因是,我们采用了马尔可夫域假设。此外,算法 4 和算法 5 中描述的增量蒙特卡洛策略评估也可用于非马尔可夫域。 275 | 276 | 这三种方法都可以收敛到真实的值函数。上节课,我们用贝尔曼备份算子的收缩性证明了动态规划的收敛性。今天的课程中,根据大数定律,我们知道蒙特卡洛策略估计收敛于策略的值函数,TD($0$)也收敛到真值,我们将在下一部分中更详细地讨论这一内容。 277 | 278 | 在蒙特卡洛中,因为我们基于回报的真实分布取了平均值,所以我们得到的是每个状态的无偏估计值。另一方面,在 TD 学习中,我们通过引导下一状态的估计值去获取当前状态的估计值,所以这个估计值被下一状态的估计值带偏。关于此的讨论请参考 Sutton and Barto [[1]](#ref1) 的 6.2 节。 279 | 280 | 蒙特卡洛估计的方差相对高于 TD 学习,因为在蒙特卡洛估计中,我们要考虑每个片段中的许多状态转移,每个状态转移对我们的估计都会产生一些方差。另一方面,TD 学习在每次更新时仅考虑一次状态转移,因此不会像蒙特卡洛估计那么快地积累方差。 281 | 282 | 最后,蒙特卡洛通常比 TD($0$) 更加数据高效。在蒙特卡洛中,我们根据整个片段的回报来更新状态的值,因此,如果未来有很多非常高或非常低奖励的轨迹,这些奖励仍然会被立即纳入到我们对状态的值的更新中。而在 TD($0$) 中,我们只用当前时间步的奖励和一些之前的对下一状态的值的估计去更新一个状态的值,这意味着,如果未来有很多非常高或非常低奖励的轨迹,仅当奖励已经被用于更新下一状态的值的引导估计时,我们才在当前状态的值的更新中包含这些奖励。这意味着,如果一个高奖励片段的长度为 $L$,那么我们可能需要经历该片段 $L$ 次,使得高奖励片段的信息能一直返回到起始状态。 283 | 284 | 表 1 总结了以上讨论的方法的优点和局限性。 285 | 286 |
287 | 288 |
289 | 表 1:本节课提到的算法总结。 290 |
291 |
292 | 293 | ### 4.7 批蒙特卡洛和时间差分(Batch Monte Carlo and Temporal Difference) 294 | 295 | 现在我们讨论这些算法的批处理版本,这里我们有一组历史,这些历史用于多次更新。我们首先看一下 Sutton and Barto [[1]](#ref1) 中的示例6.4来更仔细地检查蒙特卡洛和 TD($0$) 之间的差异。假设 $\gamma=1$,假设我们有8个根据策略 $\pi$ 生成的历史,这些历史中在每个状态都采用动作1: 296 | $$ 297 | h_1=(A,act1,+0,B,act1,+0,terminal), 298 | $$ 299 | 300 | $$ 301 | h_j=(B,act1,+1,terminal) \quad {\rm for} \quad j=2,...,7, 302 | $$ 303 | 304 | $$ 305 | h_8=(B,act1,+0,terminal)。 306 | $$ 307 | 那么当 $\alpha=1$ 时,不论使用批蒙特卡洛还是批 TD($0$),我们都会得到 $V(B)=0.75$。然而,如果使用蒙特卡洛,我们会得到 $V(A)=0$,因为只有第一个片段访问了状态 $A$ 而且回报为 $0$。另一方面,如果使用 TD($0$),我们会得到 $V(A)=0.75$,因为用 $V(A)\leftarrow r_{1,1}+\gamma V(B)$ 来更新 $V(A)$。在图 5 所示的马尔可夫域下,由 $TD(0)$ 给出的估计更有意义。 308 | 309 |
310 | 311 |
312 | 图 5:Sutton and Barto [[1]](#ref1) 例6.4。 313 |
314 |
315 | 316 | 这一部分我们考虑批蒙特卡洛和 TD($0$)。在批处理的情况,我们有一组历史 $h_1,...,h_n$,通过蒙特卡洛或 TD($0$) 这些历史可以被使用多次。与之前的式子相比,批处理唯一的不同点是只有在处理整个批后才更新值函数,因此,在 TD($0$) 中,只有在通过整个批后才更新引导估计。 317 | 318 | 在批蒙特卡洛设定中,每个状态的值收敛到使均方误差(根据挂测到的回报)最小的值,这直接源于这样一个事实:在蒙特卡洛中,我们取了每个状态的回报的平均值,通常来说,使样本的均方误差最小的值就是样本的平均值,也就是说,已知样本 $y_1,...,y_n$,当 $\hat{y}=\sum_{i=1}^{n}y_i$ 时,$\sum_{i=1}^{n}(y_i-\hat{y})^2$ 最小。从本节开头的例子也可以看出这一点,由于仅有第一个历史访问了状态 $A$, 因此应用蒙特卡洛我们可以得到 $V(A)=0$。 319 | 320 | 在批 TD($0$) 设定中,我们不会收敛到像蒙特卡洛那样的结果,而是会收敛到 $V^{\pi}$,即最大似然 MDP 模型的策略 $\pi$ 的值,这里 321 | $$ 322 | \hat{P}(s^{'}|s,a)=\frac{1}{N(s,a)}\sum_{j=1}^{n}\sum_{t=1}^{L_j-1}1(s_{j,t}=s,a_{j,t},s_{j,t+1}=s'), 323 | \tag{22} 324 | $$ 325 | 326 | $$ 327 | \hat{r}(s,a)=\frac{1}{N(s,a)}\sum_{j=1}^{n}\sum_{t=1}^{L_j-1}1(s_{j,t}=s,a_{j,t})r_{j,t}。 328 | \tag{23} 329 | $$ 330 | 331 | 也就是说,最大似然 MDP 模型是我们根据批能建立的最简单的模型,状态转移概率 $\hat{P}(s'|s,a)$ 是在这个批中,在状态 $s$ 采取动作 $a$ 后,产生了状态转移 (s,a,s') 的频率,奖励 $\hat{r}(s,a)$ 是在这个批中,在状态 $s$ 采取动作 $a$ 后,得到的奖励的平均值。 332 | 333 | 从本节开头的例子也可以看到这个结果,在这个例子中,我们的最大似然模型为 334 | $$ 335 | \hat{P}(B|A,act1)=1, 336 | \tag{24} 337 | $$ 338 | 339 | $$ 340 | \hat{P}(terminal|B,act1), 341 | \tag{25} 342 | $$ 343 | 344 | $$ 345 | \hat{r}(A,act1)=0, 346 | \tag{26} 347 | $$ 348 | 349 | $$ 350 | \hat{r}(B,act1)=0.75。 351 | \tag{27} 352 | $$ 353 | 354 | 通过这个模型我们可以得出 $V^{\pi}(A)=0.75$,就像我们之前说的那样。 355 | 356 | 从最大似然 MDP 模型推导出的值函数被称作确定等价估计(certainty equivalence estimate)。应用这个关系,我们有另一种评估策略的方法:我们可以先根据批计算最大似然 MDP 模型,然后使用这个模型和上节课提到的有模型策略评估方法去计算 $V^{\pi}$。这个方法是非常数据高效的,但却需要较大的计算量,因为解析地求 MDP 模型需要的时间为 $O(|S|^3)$,通过动态规划求 MDP 模型所需时间为 $(|S|^2|A|)$。 357 | 358 | **练习 4.2** 继续考虑图 3 所示的火星探测器(Mars Rover)例子。假设当前我们对各个状态的值的估计为 $0$,如果我们的批包括两个历史: 359 | $$ 360 | h_1=(S3,TL,+0,S2,TL,+0,S1,TL,+1,terminal) 361 | $$ 362 | 363 | $$ 364 | h_2=(S3,TL,+0,S2,TL,+0,S2,TL,+0,S1,TL,+1,terminal) 365 | $$ 366 | 并且我们的策略为 $TL$,那么确定等价估计是什么? 367 | 368 | ## 参考文献 369 | 370 | 1. R. S. Sutton, and A. G. Barto, *Introduction to Reinforcement Learning*. 2nd ed., MIT Press, 2017. Draft. http://incompleteideas.net/book/the-book-2nd.html. -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License (CC BY-NC-SA 4.0) 2 | 3 | Copyright © 2020 ApacheCN(apachecn@163.com) 4 | 5 | By exercising the Licensed Rights (defined below), You accept and agree to be bound by the terms and conditions of this Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License ("Public License"). To the extent this Public License may be interpreted as a contract, You are granted the Licensed Rights in consideration of Your acceptance of these terms and conditions, and the Licensor grants You such rights in consideration of benefits the Licensor receives from making the Licensed Material available under these terms and conditions. 6 | 7 | Section 1 – Definitions. 8 | 9 | a. Adapted Material means material subject to Copyright and Similar Rights that is derived from or based upon the Licensed Material and in which the Licensed Material is translated, altered, arranged, transformed, or otherwise modified in a manner requiring permission under the Copyright and Similar Rights held by the Licensor. For purposes of this Public License, where the Licensed Material is a musical work, performance, or sound recording, Adapted Material is always produced where the Licensed Material is synched in timed relation with a moving image. 10 | b. Adapter's License means the license You apply to Your Copyright and Similar Rights in Your contributions to Adapted Material in accordance with the terms and conditions of this Public License. 11 | c. BY-NC-SA Compatible License means a license listed at creativecommons.org/compatiblelicenses, approved by Creative Commons as essentially the equivalent of this Public License. 12 | d. Copyright and Similar Rights means copyright and/or similar rights closely related to copyright including, without limitation, performance, broadcast, sound recording, and Sui Generis Database Rights, without regard to how the rights are labeled or categorized. For purposes of this Public License, the rights specified in Section 2(b)(1)-(2) are not Copyright and Similar Rights. 13 | e. Effective Technological Measures means those measures that, in the absence of proper authority, may not be circumvented under laws fulfilling obligations under Article 11 of the WIPO Copyright Treaty adopted on December 20, 1996, and/or similar international agreements. 14 | f. Exceptions and Limitations means fair use, fair dealing, and/or any other exception or limitation to Copyright and Similar Rights that applies to Your use of the Licensed Material. 15 | g. License Elements means the license attributes listed in the name of a Creative Commons Public License. The License Elements of this Public License are Attribution, NonCommercial, and ShareAlike. 16 | h. Licensed Material means the artistic or literary work, database, or other material to which the Licensor applied this Public License. 17 | i. Licensed Rights means the rights granted to You subject to the terms and conditions of this Public License, which are limited to all Copyright and Similar Rights that apply to Your use of the Licensed Material and that the Licensor has authority to license. 18 | j. Licensor means the individual(s) or entity(ies) granting rights under this Public License. 19 | k. NonCommercial means not primarily intended for or directed towards commercial advantage or monetary compensation. For purposes of this Public License, the exchange of the Licensed Material for other material subject to Copyright and Similar Rights by digital file-sharing or similar means is NonCommercial provided there is no payment of monetary compensation in connection with the exchange. 20 | l. Share means to provide material to the public by any means or process that requires permission under the Licensed Rights, such as reproduction, public display, public performance, distribution, dissemination, communication, or importation, and to make material available to the public including in ways that members of the public may access the material from a place and at a time individually chosen by them. 21 | m. Sui Generis Database Rights means rights other than copyright resulting from Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, as amended and/or succeeded, as well as other essentially equivalent rights anywhere in the world. 22 | n. You means the individual or entity exercising the Licensed Rights under this Public License. Your has a corresponding meaning. 23 | 24 | Section 2 – Scope. 25 | 26 | a. License grant. 27 | 1. Subject to the terms and conditions of this Public License, the Licensor hereby grants You a worldwide, royalty-free, non-sublicensable, non-exclusive, irrevocable license to exercise the Licensed Rights in the Licensed Material to: 28 | A. reproduce and Share the Licensed Material, in whole or in part, for NonCommercial purposes only; and 29 | B. produce, reproduce, and Share Adapted Material for NonCommercial purposes only. 30 | 2. Exceptions and Limitations. For the avoidance of doubt, where Exceptions and Limitations apply to Your use, this Public License does not apply, and You do not need to comply with its terms and conditions. 31 | 3. Term. The term of this Public License is specified in Section 6(a). 32 | 4. Media and formats; technical modifications allowed. The Licensor authorizes You to exercise the Licensed Rights in all media and formats whether now known or hereafter created, and to make technical modifications necessary to do so. The Licensor waives and/or agrees not to assert any right or authority to forbid You from making technical modifications necessary to exercise the Licensed Rights, including technical modifications necessary to circumvent Effective Technological Measures. For purposes of this Public License, simply making modifications authorized by this Section 2(a)(4) never produces Adapted Material. 33 | 5. Downstream recipients. 34 | A. Offer from the Licensor – Licensed Material. Every recipient of the Licensed Material automatically receives an offer from the Licensor to exercise the Licensed Rights under the terms and conditions of this Public License. 35 | B. Additional offer from the Licensor – Adapted Material. Every recipient of Adapted Material from You automatically receives an offer from the Licensor to exercise the Licensed Rights in the Adapted Material under the conditions of the Adapter’s License You apply. 36 | C. No downstream restrictions. You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, the Licensed Material if doing so restricts exercise of the Licensed Rights by any recipient of the Licensed Material. 37 | 6. No endorsement. Nothing in this Public License constitutes or may be construed as permission to assert or imply that You are, or that Your use of the Licensed Material is, connected with, or sponsored, endorsed, or granted official status by, the Licensor or others designated to receive attribution as provided in Section 3(a)(1)(A)(i). 38 | b. Other rights. 39 | 1. Moral rights, such as the right of integrity, are not licensed under this Public License, nor are publicity, privacy, and/or other similar personality rights; however, to the extent possible, the Licensor waives and/or agrees not to assert any such rights held by the Licensor to the limited extent necessary to allow You to exercise the Licensed Rights, but not otherwise. 40 | 2. Patent and trademark rights are not licensed under this Public License. 41 | 3. To the extent possible, the Licensor waives any right to collect royalties from You for the exercise of the Licensed Rights, whether directly or through a collecting society under any voluntary or waivable statutory or compulsory licensing scheme. In all other cases the Licensor expressly reserves any right to collect such royalties, including when the Licensed Material is used other than for NonCommercial purposes. 42 | 43 | Section 3 – License Conditions. 44 | 45 | Your exercise of the Licensed Rights is expressly made subject to the following conditions. 46 | 47 | a. Attribution. 48 | 1. If You Share the Licensed Material (including in modified form), You must: 49 | A. retain the following if it is supplied by the Licensor with the Licensed Material: 50 | i. identification of the creator(s) of the Licensed Material and any others designated to receive attribution, in any reasonable manner requested by the Licensor (including by pseudonym if designated); 51 | ii. a copyright notice; 52 | iii. a notice that refers to this Public License; 53 | iv. a notice that refers to the disclaimer of warranties; 54 | v. a URI or hyperlink to the Licensed Material to the extent reasonably practicable; 55 | B. indicate if You modified the Licensed Material and retain an indication of any previous modifications; and 56 | C. indicate the Licensed Material is licensed under this Public License, and include the text of, or the URI or hyperlink to, this Public License. 57 | 2. You may satisfy the conditions in Section 3(a)(1) in any reasonable manner based on the medium, means, and context in which You Share the Licensed Material. For example, it may be reasonable to satisfy the conditions by providing a URI or hyperlink to a resource that includes the required information. 58 | 3. If requested by the Licensor, You must remove any of the information required by Section 3(a)(1)(A) to the extent reasonably practicable. 59 | b. ShareAlike. 60 | In addition to the conditions in Section 3(a), if You Share Adapted Material You produce, the following conditions also apply. 61 | 1. The Adapter’s License You apply must be a Creative Commons license with the same License Elements, this version or later, or a BY-NC-SA Compatible License. 62 | 2. You must include the text of, or the URI or hyperlink to, the Adapter's License You apply. You may satisfy this condition in any reasonable manner based on the medium, means, and context in which You Share Adapted Material. 63 | 3. You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, Adapted Material that restrict exercise of the rights granted under the Adapter's License You apply. 64 | 65 | Section 4 – Sui Generis Database Rights. 66 | 67 | Where the Licensed Rights include Sui Generis Database Rights that apply to Your use of the Licensed Material: 68 | 69 | a. for the avoidance of doubt, Section 2(a)(1) grants You the right to extract, reuse, reproduce, and Share all or a substantial portion of the contents of the database for NonCommercial purposes only; 70 | b. if You include all or a substantial portion of the database contents in a database in which You have Sui Generis Database Rights, then the database in which You have Sui Generis Database Rights (but not its individual contents) is Adapted Material, including for purposes of Section 3(b); and 71 | c. You must comply with the conditions in Section 3(a) if You Share all or a substantial portion of the contents of the database. 72 | 73 | For the avoidance of doubt, this Section 4 supplements and does not replace Your obligations under this Public License where the Licensed Rights include other Copyright and Similar Rights. 74 | 75 | Section 5 – Disclaimer of Warranties and Limitation of Liability. 76 | 77 | a. Unless otherwise separately undertaken by the Licensor, to the extent possible, the Licensor offers the Licensed Material as-is and as-available, and makes no representations or warranties of any kind concerning the Licensed Material, whether express, implied, statutory, or other. This includes, without limitation, warranties of title, merchantability, fitness for a particular purpose, non-infringement, absence of latent or other defects, accuracy, or the presence or absence of errors, whether or not known or discoverable. Where disclaimers of warranties are not allowed in full or in part, this disclaimer may not apply to You. 78 | b. To the extent possible, in no event will the Licensor be liable to You on any legal theory (including, without limitation, negligence) or otherwise for any direct, special, indirect, incidental, consequential, punitive, exemplary, or other losses, costs, expenses, or damages arising out of this Public License or use of the Licensed Material, even if the Licensor has been advised of the possibility of such losses, costs, expenses, or damages. Where a limitation of liability is not allowed in full or in part, this limitation may not apply to You. 79 | c. The disclaimer of warranties and limitation of liability provided above shall be interpreted in a manner that, to the extent possible, most closely approximates an absolute disclaimer and waiver of all liability. 80 | 81 | Section 6 – Term and Termination. 82 | 83 | a. This Public License applies for the term of the Copyright and Similar Rights licensed here. However, if You fail to comply with this Public License, then Your rights under this Public License terminate automatically. 84 | b. Where Your right to use the Licensed Material has terminated under Section 6(a), it reinstates: 85 | 1. automatically as of the date the violation is cured, provided it is cured within 30 days of Your discovery of the violation; or 86 | 2. upon express reinstatement by the Licensor. 87 | For the avoidance of doubt, this Section 6(b) does not affect any right the Licensor may have to seek remedies for Your violations of this Public License. 88 | c. For the avoidance of doubt, the Licensor may also offer the Licensed Material under separate terms or conditions or stop distributing the Licensed Material at any time; however, doing so will not terminate this Public License. 89 | d. Sections 1, 5, 6, 7, and 8 survive termination of this Public License. 90 | 91 | Section 7 – Other Terms and Conditions. 92 | 93 | a. The Licensor shall not be bound by any additional or different terms or conditions communicated by You unless expressly agreed. 94 | b. Any arrangements, understandings, or agreements regarding the Licensed Material not stated herein are separate from and independent of the terms and conditions of this Public License. 95 | 96 | Section 8 – Interpretation. 97 | 98 | a. For the avoidance of doubt, this Public License does not, and shall not be interpreted to, reduce, limit, restrict, or impose conditions on any use of the Licensed Material that could lawfully be made without permission under this Public License. 99 | b. To the extent possible, if any provision of this Public License is deemed unenforceable, it shall be automatically reformed to the minimum extent necessary to make it enforceable. If the provision cannot be reformed, it shall be severed from this Public License without affecting the enforceability of the remaining terms and conditions. 100 | c. No term or condition of this Public License will be waived and no failure to comply consented to unless expressly agreed to by the Licensor. 101 | d. Nothing in this Public License constitutes or may be interpreted as a limitation upon, or waiver of, any privileges and immunities that apply to the Licensor or You, including from the legal processes of any jurisdiction or authority. -------------------------------------------------------------------------------- /docs/8&9.md: -------------------------------------------------------------------------------- 1 | # Lecture 8&9 Policy Gradient 2 | 3 | # 课时8&9 策略梯度 2019.02.04 & 2019.02.06 4 | 5 | ## 1. 策略搜索介绍(Introduction to Policy Search) 6 | 7 | 到目前为止,为学习策略,我们主要研究了基于值的(value-based)方法,在这类方法中,我们以参数 $\theta$ 的函数来近似最优状态值函数或状态-行为值函数: 8 | $$ 9 | V_{\theta}(s)\approx V^{\pi}(s), 10 | $$ 11 | 12 | $$ 13 | Q_{\theta}(s,a)\approx Q^{\pi}(s,a), 14 | $$ 15 | 然后通过 $V_{\theta}$ 或 $Q_{\theta}$ 得到策略,即 $\epsilon$-贪婪策略。我们也可以使用基于策略的(policy-based)方法来直接将策略参数化: 16 | $$ 17 | \pi_{\theta}(a|s)=\mathbb{P}[a|s;\theta]。 18 | $$ 19 | 20 | 在这种设定中,我们的目标是根据最大的值函数 $V^{\pi}$ 来直接找到策略,而不是先找到基于最优策略的值函数,再从中得到策略。我们将考虑参数化的随机策略,而不是那种从状态到动作的查表式策略。找到一个好的策略需要以下两点: 21 | 22 | $\bullet$ 好的策略参数化形式:我们的函数近似和状态/动作表达形式必须是充分的; 23 | 24 | $\bullet$ 有效的搜索:我们必须能够为我们的策略函数近似找到好的参数。 25 | 26 | 基于策略的 RL 相比基于值的 RL 而言有一些优点: 27 | 28 | $\bullet$ 更好的收敛性(见 Sutton and Barto 的第 13.3 章); 29 | 30 | $\bullet$ 高维或连续动作空间下的有效性,比如机器人,第 6.2 节介绍了处理连续动作空间的一种方法; 31 | 32 | $\bullet$ 学习随机策略的能力(见后面部分)。 33 | 34 | 基于策略的 RL 方法的缺点是: 35 | 36 | $\bullet$ 由于依赖于梯度下降,它们通常收敛于局部最优策略,而不是全局最优策略; 37 | 38 | $\bullet$ 评估一个策略通常是数据效率低下或方差很大的。 39 | 40 | ## 2. 随机策略(Stochastic Policies) 41 | 42 | 本节我们将简要介绍两种环境,这两种环境中,随机策略优于任何的确定性策略。 43 | 44 | ### 2.1 石头-剪刀-布(Rock-paper-scissors) 45 | 46 | 作为一个相关的例子,在流行的剪刀-石头-布(和为零)游戏中,任何非均匀随机的策略: 47 | $$ 48 | P(\text{rock}|s)=1/3 49 | $$ 50 | 51 | $$ 52 | P(\text{scissors}|s)=1/3 53 | $$ 54 | 55 | $$ 56 | P(\text{paper}|s)=1/3 57 | $$ 58 | 都可被利用。 59 | 60 | ### 2.2 网格世界(Aliased Gridworld) 61 | 62 |
63 | 64 |
65 | 图 1:在这个部分可观测的网格环境中,行为体无法区分灰色状态。 66 |
67 | 68 | 在图 1 的网格世界环境中,假设行为体可以向四个基本的方向移动,即动作空间为 $A={N,S,E,W}$。假设它只能感知当前位置周围的墙,具体而言,它观察每个方向的以下形式的特征: 69 | $$ 70 | \phi(s)=\begin{bmatrix} \mathbf{1}(\text{wall to N}) \\ \cdots \\ \mathbf{1}(\text{wall to W}) \\ \end{bmatrix}。 71 | $$ 72 | 73 | 注意,它的观测结果并不能完全代表环境,因为它不能区分两个灰色的方块,这也意味着它的域不是马尔可夫的。因此,确定性策略必须要么学会在灰色方块中一直向左走,要么一直向右走。由于行为体可能陷在环境中的某个角落,所以这两种策略都不是最优的: 74 | 75 |
76 | 77 |
78 | 图 2:对于这个确定性策略,行为体无法“逃离”左上的两个状态。 79 |
80 | 81 | 而随机策略则可以学习在灰色的状态下随机选择一个方向,从而保证对于任何起始位置,它最终都能获得奖励。一般来说,随机策略有助于克服对抗或非平稳域(adversarial or non-stationary domain)以及状态表示为非马尔可夫的情况。 82 | 83 |
84 | 85 |
86 | 图 3:在灰色状态下以等概率采取动作 $E$ 或 $W$ 的随即策略将在几个时间步内以高概率到达目标处。 87 |
88 | 89 | # 3. 策略优化(Policy Optimization) 90 | 91 | ## 3.1 策略目标函数(Policy Objective Function) 92 | 93 | 定义了一个策略 $\pi_{theta}(a|s)$ 后,我们需要能够衡量它的表现,以便优化它。在片段式环境(episodic environment)中,一种度量量是策略的起始价值(start value of the policy),即起始状态的期望价值: 94 | $$ 95 | J_1(\theta)=V^{\pi_{\theta}}(s_1)=\mathbb{E}_ {\pi_{\theta}}[v_1]。 96 | $$ 97 | 98 | 在连续环境中,我们可以使用策略的平均价值(average value of the policy)作为度量量,这里 $d^{\pi_{\theta}}(s)$ 为 $\pi_{\theta}$ 的稳态分布(stationary distribution): 99 | $$ 100 | J_{avV}(\theta)=\sum_s d^{\pi_{\theta}}(s)V^{\pi_{\theta}}(s), 101 | $$ 102 | 也可以使用每时间步的平均奖励(average value per time-step): 103 | $$ 104 | J_{avR}(\theta)=\sum_s d^{\pi_{\theta}}(s) \sum_a \pi_{\theta}(a|s)R(s,a)。 105 | $$ 106 | 107 | 本课程中,我们讨论片段式的情况,但我们得到的所有结果都可以很容易地被推广到非片段式的情况。我们还将关注衰减因子 $\gamma=1$ 的情况,同样地,结果也可以很容易地被推广到一般的 $\gamma$。 108 | 109 | ## 3.2 优化方法(Optimization Methods) 110 | 111 | 利用目标函数,我们可以将基于策略的强化学习问题转化为优化问题。本课程中,我们关注梯度下降,因为最近这是基于策略的强化学习方法的最常用的优化方法。但一些无梯度优化方法(gradient-free optimization methods)也值得考虑,包括: 112 | 113 | $\bullet$ 爬山(Hill climbing) 114 | 115 | $\bullet$ 单纯形法 / amoeba / Nelder Mead(Simplex / amoeba / Nelder Mead) 116 | 117 | $\bullet$ 遗传算法(Genetic algorithms) 118 | 119 | $\bullet$ 交叉熵方法(Cross-entropy methods, CEM) 120 | 121 | $\bullet$ 协方差矩阵自适应(Covariance matrix adaption, CMA) 122 | 123 | $\bullet$ 进化策略(Evolution strategies) 124 | 125 | 与基于梯度的方法相比,这些方法的优点是不需要计算目标函数的梯度,这就允许了参数化策略可以是不可导的,而且通常也很容易并行化这些方法。无梯度方法通常是个有用的基线,有时候这些方法的表现出奇的好 [[1]](#ref1)。然而,由于这些方法忽略了奖励的时间结构,即更新只考虑整个片段的总奖励,而不会将奖励分解为轨迹中的每个状态的奖励,因此它们通常不是数据高效的(见 4.3 节)。 126 | 127 | # 4. 策略梯度(Policy Gradient) 128 | 129 | 定义 $V(\theta)$ 为我们希望基于 $\theta$ 最大化的目标函数。策略梯度的方法通过提升策略的梯度来搜索基于 $\theta$ 的 $V(\theta)$ 的局部最大值: 130 | 131 | $$ 132 | \Delta\theta=\alpha\nabla_{\theta}V(\theta), 133 | $$ 134 | 135 | 这里 $\alpha$ 为步长,$\nabla_{\theta}V(\theta)$ 为策略梯度: 136 | 137 | $$ 138 | \nabla_{\theta}V(\theta)=\begin{pmatrix} \frac{\partial V(\theta)}{\partial \theta_1} \\ \cdots \\ \frac{\partial V(\theta)}{\partial \theta_n} \\ \end{pmatrix}。 139 | $$ 140 | 141 | ## 4.1 梯度计算(Computing the Gradient) 142 | 143 | 根据以上这些设定,我们需要做的事仅剩下计算目标函数 $V(\theta)$ 的梯度,然后我们就可以将其优化。微积分的有限差分法(finite difference)提供了梯度的近似: 144 | 145 | $$ 146 | \frac{\partial V(\theta)}{\partial \theta_k} \approx \frac{V(\theta + \epsilon u_k)-V(\theta)}{\epsilon}, 147 | $$ 148 | 149 | 这里 $u_k$ 为第 $k$ 个元素为 $1$,其余元素为 $0$ 的单位向量。这个方法使用 $n$ 个评价来计算 $n$ 维的策略梯度,因此效率很低,而且通常仅提供真实策略梯度的有噪声的近似。然而,它的优点是适用于不可导的策略。[[2]](#ref2) 成功使用了该方法去训练 AIBO 机器人步态。 150 | 151 | ### 4.1.1 解析梯度(Analytic Gradients) 152 | 153 | 令目标函数 $V(\theta)$ 为一个片段的期望奖励: 154 | 155 | $$ 156 | V(\theta)=\mathbb{E}_ {(s_t,a_t)\sim \pi_{\theta}}[\sum_{t=0}^{T}R(s_t,a_t)]=\mathbb{E}_ {\tau \sim \pi_{\theta}}[R(\tau)]=\sum_{\tau}P(\tau;\theta)R(\tau), 157 | $$ 158 | 159 | 这里 $\tau$ 为轨迹: 160 | 161 | $$ 162 | \tau=(s_0,a_0,r_0,...,s_{T-1},a_{T-1},r_{T-1},s_T), 163 | $$ 164 | 165 | $P(\tau;\theta)$ 为遵循策略 $\pi_{\theta}$ 时轨迹的概率,$R(\tau)$ 为一个轨迹的奖励和。注意这个目标函数与 3.1 节中提到的起始价值(start value)$J_1(\theta)$ 一样($\gamma=1$ 时)。 166 | 167 | 如果我们可以用数学方法计算策略梯度 $\nabla_{\theta}\pi_{\theta}(a|s)$,那么我们就可以直接计算这个目标函数相对于 $\theta$ 的梯度: 168 | 169 | $$ 170 | \nabla_{\theta}V(\theta)=\nabla_{\theta} \sum_{\tau}P(\tau;\theta)R(\tau) 171 | \tag{1} 172 | $$ 173 | 174 | $$ 175 | =\sum_{\tau}\nabla_{\theta}P(\tau;\theta)R(\tau) 176 | \tag{2} 177 | $$ 178 | 179 | 180 | $$ 181 | =\sum_{\tau}\frac{P(\tau;\theta)}{P(\tau;\theta)}\nabla_{\theta}P(\tau;\theta)R(\tau) 182 | \tag{3} 183 | $$ 184 | 185 | 186 | 187 | $$ 188 | =\sum_{\tau}P(\tau;\theta)R(\tau) \frac{\nabla_{\theta}P(\tau;\theta)}{P(\tau;\theta)} 189 | \tag{4} 190 | $$ 191 | 192 | 193 | $$ 194 | =\sum_{\tau}P(\tau;\theta)R(\tau) \nabla_{\theta}\log P(\tau;\theta) 195 | \tag{5} 196 | $$ 197 | 198 | 199 | $$ 200 | =\mathbb{E}_ {\tau \sim \pi_{\theta}}[R(\tau)\nabla_{\theta} \log P(\tau;\theta)]。 201 | \tag{6} 202 | $$ 203 | 204 | 205 | 式([4](#eq4))中的 $\frac{\nabla_{\theta}P(\tau;\theta)}{P(\tau;\theta)}$ 也也被称为似然比(likelihood ratio)。 206 | 207 | 式([3](#eq3))~(6)中的技巧有以下两点好处:首先,这些技巧帮助我们将梯度转化为 $\mathbb{E}_ {\tau \sim \pi_{\theta}}[...]$ 的形式,这使得我们可以通过采样轨迹 $\tau^{(i)}$ 来近似梯度: 208 | 209 | 210 | $$ 211 | \nabla_{\theta}V(\theta) \approx \hat{g} = \frac{1}{m}\sum_{i=1}^{m}R(\tau^{(i)})\nabla_{\theta}\log P(\tau^{(i)};\theta); 212 | \tag{7} 213 | $$ 214 | 215 | 216 | 其次,计算 $\nabla_{\theta}\log P(\tau^{(i)};\theta)$ 比直接计算 $P(\tau^{(i)};\theta)$ 容易: 217 | 218 | $$ 219 | \nabla_{\theta}\log P(\tau^{(i)};\theta) = \nabla_{\theta}\log [\underbrace{\mu(s_0)}_ {\text{initial state distribution}} \prod_{t=0}^{T-1} \underbrace{\pi_{\theta}(a_t|s-t)}_ {\text{policy}} \underbrace{P(s_{t+1}|s_t,a_t)}_{\text{dynamics model}}] 220 | \tag{8} 221 | $$ 222 | 223 | $$ 224 | = \nabla_{\theta} [\log \mu(s_0) + \sum_{t=0}^{T-1}\log \pi_{\theta}(a_t|s_t) + \log P(s_{t+1}|s_t,a_t)] 225 | \tag{9} 226 | $$ 227 | 228 | 229 | $$ 230 | = \sum_{t=0}^{T-1} \underbrace{\nabla_{\theta}\log \pi_{\theta}(a_t|s_t)}_{\text{no dynamics model required!}}。 231 | \tag{10} 232 | $$ 233 | 234 | 235 | 处理 $\log P(\tau^{(i)};\theta)$ 而不是 $P(\tau^{(i)};\theta)$ 使得我们可以在不参考初始状态,甚至不参考环境动态模型的情况下表示梯度! 236 | 237 | 将式([7](#eq7))和式([10](#eq10))结合,我们得到: 238 | 239 | $$ 240 | \nabla_{\theta}V(\theta) \approx \hat{g} = \frac{1}{m}\sum_{i=1}^{m}R(\tau^{(i)}) \sum_{t=0}^{T-1}\nabla_{\theta}\log \pi_{\theta}(a_t^{(i)}|s_t^{(i)}), 241 | $$ 242 | 243 | 我们可以将其转化为优化 $\pi_{\theta}$ 的具体算法(第 5 部分)。在这之前,我们要提及这个结果的广义版本,并讨论利用将 $R(\tau^(i))$ 分解为奖励项 $r_t^{(i)}$ 之和来优化上述推导(4.3 节)。 244 | 245 | ## 4.2 策略梯度定理(The Policy Gradient Theorem) 246 | 247 | **定理 4.1** 对于所有的可导策略 $\pi_{\theta}(a|s)$ 和所有的策略目标函数 $V(\theta)=J_1$,$J_{avR}$ 或 $\frac{1}{1-\gamma}J_{avV}$,策略梯度为: 248 | 249 | $$ 250 | \nabla_{\theta}V(\theta)=\mathbb{E}_ {\pi_{\theta}}[Q^{\pi_{\theta}}(s,a)\nabla_{\theta}\log \pi_{\theta}(a|s)]。 251 | $$ 252 | 253 | 我们不详细讨论这个更一般的定理的推导,但本课程中讨论的相同的概念同样适用于非片段式(连续)的环境。到目前为止,我们用片段的总奖励 $R(\tau)$ 代替了这个定理中的 Q 值,但在后面的内容中,我们将使用时间结构来把我们的结果转化为更像这个定理的形式,其中未来的回报 $G_t$(即 $Q(s,a)$ 的无偏估计)将取代 $Q^{\pi_{\theta}}(s,a)$。 254 | 255 | ## 4.3 用奖励的时间形式求策略梯度(Using Temporal Structure of Rewards for the Policy Gradient) 256 | 257 | 式([6](#eq6))可被表示为: 258 | 259 | 260 | $$ 261 | \nabla_{\theta}V(\theta) = \nabla_{\theta} \mathbb{E}_ {\tau \sim \pi_{\theta}}[R(\tau)] = \mathbb{E}_ {\tau \sim \pi_{\theta}}[R(\tau)\sum_{t=0}^{T-1}\nabla_{\theta}\log \pi_{\theta}(a_t|s_t)]。 262 | \tag{11} 263 | $$ 264 | 265 | 266 | 注意,这里奖励 $R(\tau^{(i)}$ 为整个轨迹 $\tau^{(i)}$ 的函数,我们可以将其拆分为轨迹中得到的所有奖励的和: 267 | 268 | $$ 269 | R(\tau) = \sum_{t=1}^{T-1}R(s_t,a_t)。 270 | $$ 271 | 272 | 这样,我们可以用推导式([11](#eq11))的同样的方法来推导单个奖励 $r_{t'}$ 的梯度估计: 273 | 274 | $$ 275 | \nabla_{\theta} \mathbb{E}_ {\pi_{\theta}}[r_{t'}] = \mathbb{E}_ {\pi_{\theta}}[r_{t'}\sum_{t=0}^{t'} \nabla_{\theta} \log \pi_{\theta}(a_t|s_t)]。 276 | $$ 277 | 278 | 由于 $\sum_{t'=t}^{T-1}r_{t'}^{(i)}$ 就是回报 $G_t^{(i)}$,我们可以将其随时间步累加,从而得到: 279 | 280 | 281 | $$ 282 | \nabla_{\theta} V(\theta) = \nabla_{\theta} \mathbb{E}_ {\tau \sim \pi_{\theta}}[R(\tau)] = \mathbb{E}_ {\pi_{\theta}}[\sum_{t'=0}^{T-1}r_{t'}\sum_{t=0}^{t'} \nabla_{\theta} \log \pi_{\theta}(a_t|s_t)] 283 | \tag{12} 284 | $$ 285 | 286 | 287 | 288 | $$ 289 | = \mathbb{E}_ {\pi_{\theta}}[\sum_{t=0}^{T-1} \nabla_{\theta} \log \pi_{\theta}(a_t|s_t) \sum_{t'=t}^{T-1}r_{t'}] 290 | \tag{13} 291 | $$ 292 | 293 | 294 | $$ 295 | = \mathbb{E}_ {\pi_{\theta}}[\sum_{t=0}^{T-1} G_t \nabla_{\theta} \log \pi_{\theta}(a_t|s_t)]。 296 | \tag{14} 297 | $$ 298 | 299 | 由于从式([12](#eq12))到式([13](#eq13))可能并不显而易见,所以我们来举个简单的例子。假设有一个包括三个时间步的轨迹,那么式([12](#eq12))就变成了: 300 | 301 | $$ 302 | \nabla_{\theta} V(\theta) = \mathbb{E}_ {\pi_{\theta}}[r_0 \nabla_{\theta} \log\pi_{\theta}(a_0|s_0) + 303 | $$ 304 | 305 | $$ 306 | r_1(\nabla_{\theta} \log\pi_{\theta}(a_0|s_0) + \nabla_{\theta} \log\pi_{\theta}(a_1|s_1)) + 307 | $$ 308 | 309 | $$ 310 | r_2(\nabla_{\theta} \log\pi_{\theta}(a_0|s_0) + \nabla_{\theta} \log\pi_{\theta}(a_1|s_1) + \nabla_{\theta} \log\pi_{\theta}(a_2|s_2))]。 311 | $$ 312 | 313 | 重新组合这些项,我们可以得到: 314 | 315 | $$ 316 | \nabla_{\theta} V(\theta) = \mathbb{E}_ {\pi_{\theta}}[\nabla_{\theta} \log\pi_{\theta}(a_0|s_0) (r_0+r_1+r_2) + 317 | $$ 318 | 319 | $$ 320 | \nabla_{\theta} \log\pi_{\theta}(a_1|s_1) (r_1+r_2) + 321 | $$ 322 | 323 | $$ 324 | \nabla_{\theta} \log\pi_{\theta}(a_2|s_2) (r_2)], 325 | $$ 326 | 327 | 也就是说,式([13](#eq13))成立。其主要思想是,策略在特定时间步 $t$ 的选择仅影响在这个片段中的后续时间步获得的奖励,而不会影响先前时间步获得的奖励。式([11](#eq11))中的原始表达式并没有考虑这一点。 328 | 329 | 我们将要在下一节的策略梯度算法中使用的表达式为: 330 | 331 | 332 | $$ 333 | \nabla_{\theta} V(\theta) = \nabla_{\theta} \mathbb{E}_ {\tau \sim \pi_{\theta}}[R(\tau)] \approx \frac{1}{m}\sum_{i=1}^{m}\sum_{t=0}^{T-1}G_t^{(i)} \nabla_{\theta} \log \pi_{\theta}(a_t^{(i)}|s_t^{(i)})。 334 | \tag{15} 335 | $$ 336 | 337 | 338 | # 5. REINFORCE:一个蒙特卡洛策略梯度算法(REINFORCE: A Monte Carlo Policy Gradient Algorithm) 339 | 340 | 在前面的部分中,我们已经完成了第一个策略梯度算法的大部分工作,该算法对遵循策略 $\pi_{\theta}$ 的多个轨迹采样,同时根据式([15](#eq15))的梯度估计更新 $\theta$。 341 | 342 |
343 | 344 | # 6. 可导策略类(Differentiable Policy Classes) 345 | 346 | ## 6.1 离散动作空间:软最大值策略(Discrete Action Space: Softmax Policy) 347 | 348 | 离散动作空间中,我们通常用软最大值函数(softmax function)来参数化策略: 349 | 350 | $$ 351 | \pi_{\theta}(a|s)=\frac{e^{\phi(s,a)^{\text{T}}\theta}}{\sum_{a'} e^{\phi(s,a')^{\text{T}}\theta}}。 352 | $$ 353 | 354 | 则评价函数变为: 355 | 356 | $$ 357 | \nabla_{\theta} \log \pi_{\theta}(a|s) = \nabla_{\theta} [\phi(s,a)^{\text{T}}\theta - \log \sum_{a'}e^{\phi(s,a')^{\text{T}}\theta}] 358 | $$ 359 | 360 | $$ 361 | = \phi(s,a) - \frac{1}{\sum_{a'}e^{\phi(s,a')^{\text{T}}\theta}} \nabla_{\theta} \sum_{a'}e^{\phi(s,a')^{\text{T}}\theta} 362 | $$ 363 | 364 | $$ 365 | = \phi(s,a) - \frac{1}{\sum_{a'}e^{\phi(s,a')^{\text{T}}\theta}} \sum_{a'} \phi(s,a')e^{\phi(s,a')^{\text{T}}\theta} 366 | $$ 367 | 368 | $$ 369 | = \phi(s,a) - \sum_{a'} \phi(s,a') \frac{e^{\phi(s,a')^{\text{T}}\theta}}{\sum_{a''}e^{\phi(s,a'')^{\text{T}}\theta}} 370 | $$ 371 | 372 | $$ 373 | = \phi(s,a) - \sum_{a'} \phi(s,a') \pi_{\theta}(a'|s) 374 | $$ 375 | 376 | $$ 377 | = \phi(s,a) - \mathbb{E}_ {a'\sim\pi_{\theta}(a'|s)}[\phi(s,a')] 378 | $$ 379 | 380 | ## 6.2 连续动作空间:高斯策略(Continuous Action Space: Gaussian Policy) 381 | 382 | 对于连续动作空间,一个常用的选择是高斯策略:$a \sim \cal{N} (\mu(s),\sigma^2)$。 383 | 384 | $\bullet$ 动作的平均值为状态特征的线性组合:$\mu(s)=\phi(s)^{\text{T}}\theta$; 385 | 386 | $\bullet$ 方差 $\sigma^2$ 可以是固定的,也可以是参数化的。 387 | 388 | 评价函数为: 389 | 390 | $$ 391 | \nabla_{\theta} \log \pi_{\theta}(a|s) = \frac{(a-\mu(s))\phi(s)}{\sigma^2}。 392 | $$ 393 | 394 | # 7. 根据基准减小方差(Variance Reduction with a Baseline) 395 | 396 | 蒙特卡洛策略梯度算法的一个缺点是多个片段的回报 $G_t^{(i)}$ 的方差通常很大。解决这个问题的一种方法是从每个 $G_t^{(i)}$ 减去一个基准(baseline)$b(s)$,这个基准可以是任何函数,只要它不随 $a$ 的变化而变化。 397 | 398 | $$ 399 | \nabla_{\theta} V(\theta) = \nabla_{\theta} \mathbb{E}_ {\tau \sim \pi_{\theta}}[R(\tau)] = \mathbb{E}_ {\pi_{\theta}}[\sum_{t=0}^{T-1}(G_t-b(s_t))\nabla_{\theta}\log\pi_{\theta}(a_t|s_t)]。 400 | $$ 401 | 402 | 首先,为什么我们要这样做?直观地说,我们可以认为 $(G_t-b(s_t))$ 是对时间步 $t$ 之后我们做得比预期的基准 $b(s_t)$ 要好多少的估计。所以,如果基准近似等于期望回报 $b(s_t)\approx \mathbb{E}[r_t+r_{t+1}+...+r_{T-1}]$,那么我们将按照回报 $G_t$ 比期望好多少,成比例地增大动作 $a_t$ 的对数概率(log-probability)。过去,我们按照 $G_t$ 的大小,成比例地增大对数概率,所以即使策略总是能达到期望回报,我们仍会采用梯度更新,这可能导致其发散。$(G_t-b(s_t))$ 通常被称为优势(advantage),$A_t$。我们可以根据一个采样的轨迹 $\tau^{(i)}$ 来估计真实的优势: 403 | 404 | $$ 405 | \hat{A}_t=(G_t^{(i)}-b(s_t))。 406 | $$ 407 | 408 | 第二,为什么我们可以这样做?结果表明,用这种方式减去一个基准并不会在梯度计算中引入任何偏差。$\mathbb{E}_ {\tau}[b(s_t)\nabla_{\theta}\log \pi_{\theta}(a_t|s_t)]$ 为 $0$,因此不会影响梯度更新。 409 | 410 | $$ 411 | \mathbb{E}_ {\tau\sim\pi_{\theta}}[b(s_t)\nabla_{\theta}\log \pi_{\theta}(a_t|s_t)] 412 | $$ 413 | 414 | $$ 415 | = \mathbb{E}_ {s_{0:t},a_{0:(t-1)}}[\mathbb{E}_ {s_{(t+1):T},a_{t:(T-1)}}[\nabla_{\theta} \log \pi_{\theta}(a_t|s_t)b(s_t)]] \quad \text{(将期望拆开)} 416 | $$ 417 | 418 | $$ 419 | = \mathbb{E}_ {s_{0:t},a_{0:(t-1)}}[b(s_t)\mathbb{E}_ {s_{(t+1):T},a_{t:(T-1)}}[\nabla_{\theta} \log \pi_{\theta}(a_t|s_t)]] \quad \text{(提出基准项)} 420 | $$ 421 | 422 | $$ 423 | = \mathbb{E}_ {s_{0:t},a_{0:(t-1)}}[b(s_t)\mathbb{E}_ {a_t}[\nabla_{\theta} \log \pi_{\theta}(a_t|s_t)]] \quad \text{(移除无关变量)} 424 | $$ 425 | 426 | $$ 427 | = \mathbb{E}_ {s_{0:t},a_{0:(t-1)}}[b(s_t) \sum_{a_t} \pi_{\theta}(a_t|s_t)\frac{\nabla_{\theta}\pi_{\theta}(a_t|s_t)}{\pi_{\theta}(a_t|s_t)}] \quad \text{(将期望展开 + 对数函数求导)} 428 | $$ 429 | 430 | $$ 431 | = \mathbb{E}_ {s_{0:t},a_{0:(t-1)}}[b(s_t) \sum_{a_t} \nabla_{\theta}\pi_{\theta}(a_t|s_t)] 432 | $$ 433 | 434 | $$ 435 | = \mathbb{E}_ {s_{0:t},a_{0:(t-1)}}[b(s_t) \nabla_{\theta} \sum_{a_t} \pi_{\theta}(a_t|s_t)] 436 | $$ 437 | 438 | $$ 439 | = \mathbb{E}_ {s_{0:t},a_{0:(t-1)}}[b(s_t) \nabla_{\theta} 1] 440 | $$ 441 | 442 | $$ 443 | = \mathbb{E}_ {s_{0:t},a_{0:(t-1)}}[b(s_t) \cdot 0] 444 | $$ 445 | 446 | $$ 447 | = 0。 448 | $$ 449 | 450 | ## 7.1 普通策略梯度(Vanilla Policy Gradient) 451 | 452 | 使用前面讨论的基准,这里我们介绍普通策略梯度(vanilla policy gradient)算法。假设基准函数的参数为 $\mathbf{w}$。 453 | 454 |
455 | 456 | 状态值函数是基准的一个很自然的选择,$b(s_t)=V(s_t)$,这时优势函数为 $A^{\pi}(s,a)=Q^{\pi}(s,a)-V^{\pi}(s)$。然而,由于我们不知道真实的状态值,因此我们使用估计值 $\hat{V}(s_t;\mathbf{w})$ 来代替,这里 $\mathbf{w}$ 为权重向量。我们可以通过蒙特卡洛轨迹采样来同时学习基准函数(状态值函数)的权重向量 $\mathbf{w}$ 和策略的参数 $\theta$。 457 | 458 | 注意,在算法 2 中,我们通常并不单独计算梯度 $\sum_t \hat{A}_ t \nabla_{\theta} \log \pi_{\theta}(a_t|s_t)$,而是将一个批的数据累积到损失函数中: 459 | 460 | $$ 461 | L(\theta)=\sum_t \hat{A}_ t \log \pi_{\theta}(a_t|s_t), 462 | $$ 463 | 464 | 然后通过计算 $\nabla_{\theta} L(\theta)$ 来计算梯度。我们也可以在这个损失函数中引入一个分量来拟合基准函数: 465 | 466 | $$ 467 | L(\theta,\mathbf{w})=\sum_t (\hat{A}_ t \log \pi_{\theta}(a_t|s_t) - \parallel b(s_t)-G_t^{(i)} \parallel ^2), 468 | $$ 469 | 470 | 然后我们可以计算 $L(\theta,\mathbf{w})$ 关于 $\theta$ 和 $\mathbf{w}$ 的梯度来执行 SGD 更新。 471 | 472 | ## 7.2 N 步估计(N-step Estimators) 473 | 474 | 在上面的推导中,为近似策略梯度,我们使用了奖励的蒙特卡洛估计。然而,如果我们能知道值函数(例如基准),那么我们也可以 TD 方法、或 TD 与 MC 的混合方法来进行策略梯度更新: 475 | 476 | $$ 477 | \hat{G}_{t}^{(1)} = r_t + \gamma V(s_{t+1}) 478 | $$ 479 | 480 | $$ 481 | \hat{G}_{t}^{(2)} = r_t + \gamma r_{t+1} + \gamma^2 V(s_{t+2}) 482 | $$ 483 | 484 | $$ 485 | ... 486 | $$ 487 | 488 | $$ 489 | \hat{G}_t^{(\text{inf})} = r_t + \gamma r_{t+1} + \gamma^2 r_{t+2} + ..., 490 | $$ 491 | 492 | 我们也可以用这些方法来计算优势函数: 493 | 494 | $$ 495 | \hat{A}_t^{(1)} = r_t + \gamma V(s_{t+1}) - V(s_t) 496 | $$ 497 | 498 | $$ 499 | \hat{A}_t^{(2)} = r_t + \gamma r_{t+1} + \gamma^2 V(s_{t+2}) - V(s_t) 500 | $$ 501 | 502 | $$ 503 | ... 504 | $$ 505 | 506 | $$ 507 | \hat{A}_t^{(\text{inf})} = r_t + \gamma r_{t+1} + \gamma^2 r_{t+2} - V(s_t), 508 | $$ 509 | 510 | 这里 $\hat{A}_t^{(1)}$ 是纯的 TD 估计,具有低方差、高偏移的特点,$\hat{A}_t^{(\text{inf})}$ 是纯的 MC 估计,具有零偏移、高方差的特点。如果我们选择一个中间值 $\hat{A}_t^{(k)}$,那么这个中间值的方差和偏移都是中间量。 511 | 512 | ## 参考文献 513 | 514 | 1. https://blog.openai.com/evolution-strategies/. 515 | 516 | 2. N. Kohl, and P. Stone, "Policy gradient reinforcement learning for fast quadrupedal locomotion," *Proceedings of the IEEE International Conference on Robotics and Automation*, 2004. -------------------------------------------------------------------------------- /docs/10.md: -------------------------------------------------------------------------------- 1 | # Lecture 10 Advanced Policy Gradient 2 | 3 | # 课时10 高级策略梯度 2019.02.11 4 | 5 | ## 1. 策略梯度的目标(Policy Gradient Objective) 6 | 7 | 在策略梯度中,我们将策略 $\pi_\theta$ 参数化,并使用环境中的经历直接来优化它。我们首先定义基于当前策略 $\pi_\theta$ 的轨迹的概率为 $\pi_\theta(\tau)$: 8 | 9 | $$ 10 | \pi_\theta(\tau) = \pi_\theta(s_1,a_1,...,s_T,a_T)=P(s_1)\prod_{t=1}^T \pi_\theta(a_t|s_t)P(s_{t+1}|s_t,a_t), 11 | $$ 12 | 13 | 这里 $P(s_1)$ 为起始状态为 $s_1$ 的概率,$\pi_\theta(a_t|s_t)$ 为根据当前的策略在状态 $s_t$ 选择动作 $a_t$ 的概率,$P(s_{t+1}|s_t,a_t)$ 为在状态 $s_t$ 选择动作 $a_t$ 时,状态转移到 $s_{t+1}$ 的概率。注意,$\pi_\theta(\tau)$ 为轨迹的概率而 $\pi_\theta(a|s)$ 为给定状态时选择某个动作的概率。 14 | 15 | 和到目前为止我们讨论过的大多数其他 RL 目标类似,策略梯度的目标是最大化衰减奖励总和。 16 | 17 | $$ 18 | \theta^* = \mathop{\arg\max}_{\theta} \mathbb{E} _{\tau\sim \pi _{\theta}(\tau)}[\sum_t \gamma^t r (s_t,a_t)]。 19 | $$ 20 | 21 | 我们将目标函数记为 $J(\theta)$,可以用蒙特卡洛方法估计 $J(\theta)$。我们用 $r(\tau)$ 来代表轨迹 $\tau$ 的衰减奖励总和。 22 | 23 | $$ 24 | J(\theta) = \mathbb{E}_{\tau\sim \pi _{\theta}(\tau)}[\sum_t \gamma^t r (s_t,a_t)] = \int \pi _{\theta} (\tau) r(\tau) \text{d} \tau 25 | $$ 26 | 27 | $$ 28 | \approx \frac{1}{N}\sum_{i=1}^N \sum_{t=1}^T \gamma^t r(s_{i,t},a_{i,t}), 29 | $$ 30 | 31 | $$ 32 | \theta^* = \mathop{\arg\max}_\theta J(\theta)。 33 | $$ 34 | 35 | 我们定义 $P_\theta(s,a)$ 为 $(s,a)$ 出现在轨迹中的概率。注意,若时间步无穷大而且状态的平稳分布存在时,我们可以将 $P_\theta(s,a)$ 写为 $P_\theta(s,a)=d^{\pi_{\theta}}(s)\pi_{\theta}(a|s)$,这里 $d^{\pi_{\theta}}(s)$ 为遵照策略 $\pi_{\theta}$ 时的状态的平稳分布。 36 | 37 | 在无限时间步的情况下,我们有: 38 | $$ 39 | \theta^{*} = \mathop{\arg\max}_{\theta}\sum _{t=1}^{\infty} \mathbb{E} _{(s,a) \sim P _{\theta}(s,a)}[\gamma^t r(s,a)] 40 | $$ 41 | 42 | $$ 43 | = \mathop{\arg\max}_{\theta} \frac{1}{1-\gamma}\mathbb{E} _{(s,a) \sim P _{\theta}(s,a)}[r(s,a)] 44 | $$ 45 | 46 | $$ 47 | = \mathop{\arg\max}_{\theta} \mathbb{E} _{(s,a) \sim P _{\theta}(s,a)}[r(s,a)]。 48 | $$ 49 | 50 | 在有限时间步的情况下,我们有: 51 | $$ 52 | \theta^{*} = \mathop{\arg\max}_{\theta} \sum _{t=1}^{T} \mathbb{E} _{(s_t,a_t) \sim P _{\theta}(s_t,a_t)}[\gamma^t r(s_t,a_t)]。 53 | $$ 54 | 55 | 我们可以使用基于梯度的方法来完成上述优化,也就是说,我们需要找到 $J(\theta)$ 基于 $\theta$ 的梯度。 56 | $$ 57 | \nabla_{theta}J(\theta) = \nabla_{\theta}\int \pi _{\theta} (\tau) r(\tau) \text{d} \tau 58 | $$ 59 | 60 | $$ 61 | = \int \nabla_{\theta} \pi _{\theta} (\tau) r(\tau) \text{d} \tau 62 | $$ 63 | 64 | $$ 65 | = \int \pi_{\theta}(\tau) \frac{\nabla_{theta} \pi_ {\theta} (\tau)}{\pi_{\theta}(\tau)}r(\tau)\text{d}\tau 66 | $$ 67 | 68 | $$ 69 | = \mathbb{E}_ {\tau\sim\pi_{\theta}(\tau)}[\nabla_{\theta}\log\pi_{\theta}(\tau)r(\tau)]。 70 | $$ 71 | 72 | 通过对数导数技巧,我们将梯度从期望之外转移到了期望之内。这样做的好处就是,我们不再需要对状态转移函数求梯度,正如下面我们将看到的。 73 | $$ 74 | \nabla_{theta}J(\theta) = \mathbb{E}_ {\tau\sim\pi_{\theta}(\tau)}[\nabla_{\theta}\log\pi_{\theta}(\tau)r(\tau)] 75 | $$ 76 | 77 | $$ 78 | = \mathbb{E}_ {\tau\sim\pi_{\theta}(\tau)}[\nabla_{theta} [\log P(s_1)+\sum_{t=1}^{T}(\log\pi_{\theta}(a_t|s_t) + \log P(s_{t+1}|s_t,a_t))]r(\tau)] 79 | $$ 80 | 81 | $$ 82 | = \mathbb{E}_ {\tau\sim\pi_{\theta}} [\nabla_{\theta} [\sum_{t=1}^{T}(\log\pi_{\theta}(a_t|s_t))] r(\tau)] 83 | $$ 84 | 85 | $$ 86 | = \mathbb{E}_ {\tau\sim\pi_{\theta}} [\sum_{t=1}^{T}(\nabla_{\theta}(\log\pi_{\theta}(a_t|s_t))(\sum_{t=1}^{T}\gamma^t r(s_t,a_t)))] 87 | $$ 88 | 89 | $$ 90 | \approx \frac{1}{N} \sum_{i=1}^{N} \sum_{t=1}^{T} (\nabla_{\theta} (\log\pi_{\theta}(a_{i,t}|s_{i,t}))(\sum_{t=1}^{T}\gamma^t r(s_{i,t},a_{i,t})))。 91 | $$ 92 | 93 | 在第三个等式中,不包含 $\theta$ 的项被去掉。最后一步,我们应用了蒙特卡洛估计。 94 | 95 | 注意,在监督学习的设定下,上述式子与最大似然估计(Maximum Likelihood Estimate,MLE)有很多相似之处,例如,对于监督学习中的 MLE,我们有概率 $J'(\theta)$ 和对数概率 $J(\theta)$: 96 | 97 | $$ 98 | J'(\theta) = \prod_{i=1}^{N}P(y_i|x_i), 99 | $$ 100 | 101 | $$ 102 | J(\theta) = \log J'(\theta) = \sum_{i=1}^{N}\log P(y_i|x_i), 103 | $$ 104 | 105 | $$ 106 | \nabla_{theta}J(\theta) = \sum_{i=1}^{N} \nabla_{\theta}\log P(y_i|x_i)。 107 | $$ 108 | 109 | 与策略梯度推导相比,关键的差别在于奖励的累加。我们甚至可以将 MLE 视为回报都为 1 的策略梯度。尽管这一差异看起来很小,它会使得问题变得更加困难,特别是,将奖励累加会大大增加方差。因此,在一节中,我们将讨论两种减小方差的方法。 110 | 111 | ## 2. 在策略梯度中减小方差(Reducing Variance in Policy Gradient) 112 | 113 | ### 2.1 因果关系(Causality) 114 | 115 | 首先我们注意到在时间 $t'$ 采取动作不会影响到 时间 $t$ 的奖励,对于所有的 $t < t'$ 而言,这就是所谓的因果关系,因为我们现在做的事不会影响到过去。因此,我们可以将奖励的累加 $\sum_{t=1}^{T}\gamma^{t}r(s_{i,t},a_{i,t})$ 改为 $\hat{Q}_ {i,t}=\sum_{t'=t}^{T}\gamma^{t'}r(s_{i,t'},a_{i,t'})$。这里我们用 $\hat{Q}$ 来表示它是 $Q$ 的蒙特卡洛估计。这样做有助于减小方差,因为我们可以有效地减少来自先前奖励的噪声。因此,我们的目标变为: 116 | 117 | $$ 118 | \nabla_{\theta}J(\theta) \approx \frac{1}{N} \sum_{i=1}^{N} \sum_{t=1}^{T} (\nabla_{\theta}\log \pi_{\theta}(a_{i,t}|s_{i,t}) (\sum_{t'=t}^{T}\gamma^{t'}r(s_{i,t'},a_{i,t'}))) = \frac{1}{N} \sum_{i=1}^{N} \sum_{t=1}^{T} (\nabla_{\theta}\log \pi_{\theta}(a_{i,t}|s_{i,t})\hat{Q}_{i,t})。 119 | $$ 120 | 121 | ### 2.1 基准(Baselines) 122 | 123 | 现在我们来考虑从回报中减去一个基准,也就是说,我们将目标改为如下形式: 124 | $$ 125 | \nabla_{\theta}J(\theta) \approx \frac{1}{N} \sum_{i=1}^{N} \sum_{t=1}^{T} (\nabla_{\theta}\log \pi_{\theta}(a_{i,t}|s_{i,t})((\sum_{t'=t}^{T}\gamma^{t'}r(s_{i,t'},a_{i,t'}))-b))。 126 | $$ 127 | 128 | 首先,减去的常值基准 $b$ 是无偏的: 129 | $$ 130 | \mathbb{E}_ {\tau\sim\pi_{\theta}(\tau)} [\nabla_{\theta}\log \pi_{\theta}(\tau) b] = \int \pi_{\theta}(\tau) \nabla_{\theta}\log \pi_{\theta}(\tau) b \text{d} \tau 131 | $$ 132 | 133 | $$ 134 | = \int \pi_{\theta}(\tau) \frac{\nabla_{\theta}\pi_{\theta}(\tau)}{\pi_{\theta}(\tau)} b \text{d} \tau 135 | $$ 136 | 137 | $$ 138 | = \int \nabla_{\theta}\pi_{\theta}(\tau) b \text{d} \tau 139 | $$ 140 | 141 | $$ 142 | = b \nabla_{\theta} \int \pi_{\theta}(\tau) \text{d} \tau 143 | $$ 144 | 145 | $$ 146 | = b \nabla_{\theta} 1 = 0。 147 | $$ 148 | 149 | 最后的等式中,所有轨迹的概率和为 1。在倒数第二个等式中,由于 $b$ 为常值,我们可以将提出至积分外面(例如,$b$ 可以为平均回报,$b = \frac{1}{N}\sum_{i=1}^{N}r(\tau)$)。即使 $b$ 是状态 $s$ 的函数,这一项也是无偏的: 150 | $$ 151 | \mathbb{E}_ {\tau\sim\pi_{\theta}(\tau)} [\nabla_{\theta}\log \pi_{\theta}(\tau) b(s_t)] 152 | $$ 153 | 154 | $$ 155 | = \mathbb{E}_ {s_{0:t},a_{0:(t-1)}} [\mathbb{E}_ {s_{(t+1):T},a_{t:(T-1)}} [\nabla_{\theta}\log \pi_{\theta}(a_t|s_t)b(s_t)]] 156 | $$ 157 | 158 | $$ 159 | = \mathbb{E}_ {s_{0:t},a_{0:(t-1)}} [b(s_t) \mathbb{E}_ {s_{(t+1):T},a_{t:(T-1)}}[\nabla_{\theta}\log \pi_{\theta}(a_t|s_t)]] 160 | $$ 161 | 162 | $$ 163 | = \mathbb{E}_ {s_{0:t},a_{0:(t-1)}} [b(s_t) \mathbb{E}_ {a_t}[\nabla_{\theta}\log \pi_{\theta}(a_t|s_t)]] 164 | $$ 165 | 166 | $$ 167 | = \mathbb{E}_ {s_{0:t},a_{0:(t-1)}} [b(s_t) \cdot 0] = 0。 168 | $$ 169 | 170 | 如上所述,如果对策略不做任何假设,那么基准不能是动作的函数,因为上述证明需要提出 $b(s_t)$。如果我们对策略做出一些假设,那么例外情况就出现了,参见 [[3]](#ref3) 了解与动作相关的基准的例子。 171 | 172 | 一个常用的基准是值函数 $V^{\pi_{\theta}}(s)$。因为回报估计了状态-动作值函数 $Q^{\pi_{\theta}}(s,a)$,通过减去这个基准,我们实际上是在计算优势 $A^{\pi_{\theta}}(s,a)=Q^{\pi_{\theta}}(s,a)-V^{\pi_{\theta}}(s)$。在实现方面,这意味着训练一个单独的值函数 $V_{\phi}(s)$。 173 | 174 | 另一方面,我们可以训练另一个状态-动作值函数 $Q_{\omega}(s,a)$ 来逼近策略梯度,而不是使用环境返回的实际回报来估计 $Q^{\pi_{\theta}}(s,a)$。这一方法被称为 $actor-critic$,这里 $Q_{\omega}$ 为 $critic$。本质上,$critic$ 做策略评估,$actor$ 做策略改进。 175 | 176 | 那么为了最小化方差,最优的基准是什么?事实上,最优的基准为按梯度平方加权的期望奖励,如下所示。 177 | $$ 178 | Var[X] = \mathbb{E}[X^2] - \mathbb{E}[X]^2, 179 | $$ 180 | 181 | $$ 182 | \nabla_{\theta}J(\theta) = \mathbb{E}_ {\tau\sim\pi_{\theta}(\tau)} [\nabla_{\theta} \log \pi_{\theta}(\tau)(r(\tau)-b)], 183 | $$ 184 | 185 | $$ 186 | Var = \mathbb{E}_ {\tau\sim\pi_{\theta}(\tau)}[(\nabla_{\theta} \log \pi_{\theta}(\tau)(r(\tau)-b))^2] - (\mathbb{E}_ {\tau\sim\pi_{\theta}(\tau)}[\nabla_{\theta} \log \pi_{\theta}(\tau)(r(\tau)-b)])^2 187 | $$ 188 | 189 | $$ 190 | = \mathbb{E}_ {\tau\sim\pi_{\theta}(\tau)}[(\nabla_{\theta} \log \pi_{\theta}(\tau)(r(\tau)-b))^2] - (\mathbb{E}_ {\tau\sim\pi_{\theta}(\tau)}[\nabla_{\theta} \log \pi_{\theta}(\tau)r(\tau)])^2。 191 | $$ 192 | 193 | 上述等式中,因为我们已经证明 $b$ 在期望中是无偏的,我们可以将 $b$ 去掉。为了最小化方差,我们将方差关于 $b$ 的导数设为 0,第二项与 $b$ 无关,因此只需要对第一项求导: 194 | $$ 195 | \frac{{\text d}Var}{{\text d}b} = \frac{{\text d}}{{\text d}b} \mathbb{E}_ {\tau\sim\pi_{\theta}(\tau)} [(\nabla_{\theta} \log \pi_{\theta}(\tau)(r(\tau)-b))^2] 196 | $$ 197 | 198 | $$ 199 | = \frac{{\text d}}{{\text d}b} (\mathbb{E}_ {\tau\sim\pi_{\theta}(\tau)}[(\nabla_{\theta} \log \pi_{\theta}(\tau))^2 b^2] - 2\mathbb{E}_ {\tau\sim\pi_{\theta}(\tau)}[(\nabla_{\theta} \log \pi_{\theta}(\tau))^2 r(\tau)b]) 200 | $$ 201 | 202 | $$ 203 | = 2\mathbb{E} [(\nabla_{\theta} \log \pi_{\theta}(\tau))^2 b] - 2\mathbb{E} [(\nabla_{\theta} \log \pi_{\theta}(\tau))^2 r(\tau)] = 0, 204 | $$ 205 | 206 | $$ 207 | b = \frac{\mathbb{E} [(\nabla_{\theta} \log \pi_{\theta}(\tau))^2 r(\tau)]}{\mathbb{E} [(\nabla_{\theta} \log \pi_{\theta}(\tau))^2]}。 208 | $$ 209 | 210 | ## 3. 离线策略策略梯度(Off Policy Policy Gradient) 211 | 212 | 上面的分析中,我们的目标为对基于 $\pi_{\theta}(\tau)$ 的轨迹求期望,这意味着具有上述目标的策略梯度将产生在线策略算法。不论何时改变参数 $\theta$,我们的策略都改变,并且过去的轨迹无法再被使用。而 DQN 为离线策略算法,因为它能够存储并利用过去的经历。这意味着一般情况下,上述策略梯度的样本有效性低于 Q-学习。为了解决这一问题,我们将讨论重要性采样(Importance Sampling)来生成离线策略策略梯度(Off Policy Policy Gradient)算法。具体来说,我们需要做出哪些改变,如果我们想用基于先前策略 $\pi_{\theta}$ 生成的轨迹,而不是基于当前策略 $\pi_{\theta}'$ 生成的轨迹来估计 $J(\theta)$。 213 | 214 | $$ 215 | \theta^* = \mathop{\arg\max}_{\theta'} J(\theta') 216 | $$ 217 | 218 | $$ 219 | = \mathop{\arg\max}_ {\theta'} \mathbb{E}_ {\tau\sim\pi_{\theta'}(\tau)} [r(\tau)] 220 | $$ 221 | 222 | $$ 223 | = \mathop{\arg\max}_ {\theta'} \mathbb{E}_ {\tau\sim\pi_{\theta}(\tau)} [\frac{\pi_{\theta'}(\tau)}{\pi_{\theta}(\tau)} r(\tau)] 224 | $$ 225 | 226 | $$ 227 | = \mathop{\arg\max}_ {\theta'} \mathbb{E}_ {\tau\sim\pi_{\theta}(\tau)} [\frac{P(s_1)\prod_{t=1}^{T}\pi_{\theta'}(a_t|s_t)P(s_{t+1}|s_t,a_t)}{P(s_1)\prod_{t=1}^{T}\pi_{\theta}(a_t|s_t)P(s_{t+1}|s_t,a_t)} r(\tau)] 228 | $$ 229 | 230 | $$ 231 | = \mathop{\arg\max}_ {\theta'} \mathbb{E}_ {\tau\sim\pi_{\theta}(\tau)} [\frac{\prod_{t=1}^{T}\pi_{\theta'}(a_t|s_t)}{\prod_{t=1}^{T}\pi_{\theta}(a_t|s_t)}]。 (????感觉有问题) 232 | $$ 233 | 234 | 因此,对于旧的参数 $\theta$,我们有: 235 | $$ 236 | J(\theta) = \mathbb{E}_ {\tau\sim\pi_{\theta}(\tau)}[r(\tau)], 237 | $$ 238 | 239 | 对于新的参数 $\theta'$,我们有: 240 | $$ 241 | J(\theta') = \mathbb{E}_ {\tau\sim\pi_{\theta}(\tau)}[\frac{\pi_{\theta'}(\tau)}{\pi_{\theta}(\tau)} r(\tau)]。 242 | $$ 243 | 244 | $$ 245 | \nabla_{\theta'}J(\theta') = \mathbb{E}_ {\tau\sim\pi_{\theta}(\tau)} [\frac{\nabla_{\theta'}\pi_{\theta'}(\tau)}{\pi_{\theta}(\tau)} r(\tau)] 246 | $$ 247 | 248 | $$ 249 | = \mathbb{E}_ {\tau\sim\pi_{\theta}(\tau)} [\frac{\pi_{\theta'}(\tau)}{\pi_{\theta}(\tau)} \nabla_{\theta'}\log\pi_{\theta'}(\tau) r(\tau)] 250 | $$ 251 | 252 | $$ 253 | = \mathbb{E}_ {\tau\sim\pi_{\theta}(\tau)} [(\prod_{t=1}^{T} \frac{\pi_{\theta'}(a_t|s_t)}{\pi_{\theta}(a_t|s_t)}) (\sum_{t=1}^{T}\nabla_{\theta'}(\log\pi_{\theta'}(a_t|s_t))) (\sum_{t=1}^{T}\gamma^{t}r(s_t,a_t))] 254 | $$ 255 | 256 | $$ 257 | = \mathbb{E}_ {\tau\sim\pi_{\theta}(\tau)} [\sum_{t=1}^{T}(\nabla_{\theta'}(\log\pi_{\theta'}(a_t|s_t)) (\prod_{t'=1}^{t} \frac{\pi_{\theta'}(a_{t'}|s_{t'})}{\pi_{\theta}(a_{t'}|s_{t'})}) (\sum_{t'=t}^{T}\gamma^{t'}r(s_{t'},a_{t'})))]。 258 | $$ 259 | 260 | 最后一个等式中,我们应用了因果关系,前 $k$ 次状态转移只依赖于前 $k$ 个动作而不依赖于将来的动作。 261 | 262 | ## 4. 相对策略表现恒等式(Relative Policy Performance Identity) 263 | 264 | 根据目标 $J(\theta)$ 相对于参数 $\theta$ 的梯度直接采取梯度步骤的一个问题是,在参数空间中移动与在策略空间中移动是不同的。这导致了步长的选择问题,小的步长使得学习较为缓慢,而大的步长可能导致策略变差。 265 | 266 | 在监督学习的情况下,这通常没关系,因为以下更新一般会解决这个问题。然而,在强化学习中,坏的策略将导致在坏的策略下收集下一批数据。因此,执行坏的策略可能会引起无法恢复的性能崩溃。在梯度方向上执行简单的线搜索可能缓解此问题,例如,我们可以为每次更新尝试多个学习率,并选择表现最佳的学习率。然而,这样做属实有点简单,并且在一阶近似(梯度)不好的时候会导致收敛很慢。 267 | 268 | 下一节将讨论的信任区域策略优化(Trust Region Policy Optimization)算法尝试去解决这个问题。在此之前,我们首先推导一个关于相对策略表现 $J(\pi')-J(\pi)$ 的恒等式,这里我们使用如下符号:$J(\pi')=J(\theta')$,$J(\pi)=J(\theta)$,$\pi'=\pi_{\theta'}$ 与 $\pi=\pi_{\theta}$。 269 | 270 | **引理 4.1** 271 | 272 | $$ 273 | J(\pi')-J(\pi) = \mathbb{E}_ {\tau\sim\pi'}[\sum_{t=0}^{\infty}\gamma^{t} A^{\pi}(s_t,a_t)]。 274 | $$ 275 | 276 | 证明: 277 | 278 | $$ 279 | \mathbb{E}_ {\tau\sim\pi'} [\sum_{t=0}^{\infty}\gamma^{t} A^{\pi}(s_t,a_t)] = \mathbb{E}_ {\tau\sim\pi'} [\sum_{t=0}^{\infty}\gamma^{t} [r(s_t,a_t)+\gamma V^{\pi}(s_{t+1}) - V^{\pi}(s_t)]] 280 | $$ 281 | 282 | $$ 283 | = \mathbb{E}_ {\tau\sim\pi'} [\sum_{t=0}^{\infty} \gamma^{t}r(s_t,a_t)] + \mathbb{E}_ {\tau\sim\pi'}[\sum_{t=0}^{\infty}\gamma^{t}[\gamma V^{\pi}(s_{t+1}) - V^{\pi}(s_t)]] 284 | $$ 285 | 286 | $$ 287 | = J(\pi') - \mathbb{E}_ {\tau\sim\pi'}[V^{\pi}(s_0)] 288 | $$ 289 | 290 | $$ 291 | = J(\pi')-J(\pi)。 292 | $$ 293 | 294 | 证明完毕。$\diamondsuit$ 295 | 296 | 因此,我们有: 297 | 298 | $$ 299 | \mathop{\max}_ {\pi'} J(\pi') = \mathop{\max}_{\pi'} (J(\pi')-J(\pi)) 300 | $$ 301 | 302 | $$ 303 | = \mathop{\max}_ {\pi'} \mathbb{E}_ {\tau\sim\pi'}[\sum_{t=0}^{\infty}\gamma^{t} A^{\pi}(s_t,a_t)]。 304 | $$ 305 | 306 | 上述表达式需要根据 $\pi'$ 的轨迹,然而我们还没有 $\pi'$,这就导致无法进行优化。我们再一次使用可能性来规避这个问题。 307 | 308 | $$ 309 | J(\pi')-J(\pi) = \mathbb{E}_ {\tau\sim\pi'}[\sum_{t=0}^{\infty}\gamma^{t} A^{\pi}(s_t,a_t)] 310 | $$ 311 | 312 | $$ 313 | = \frac{1}{1-\gamma} \mathbb{E}_ {s\sim d^{\pi'},a\sim\pi'}[A^{\pi}(s,a)] 314 | $$ 315 | 316 | $$ 317 | = \frac{1}{1-\gamma} \mathbb{E}_ {s\sim d^{\pi'},a\sim\pi} [\frac{\pi'(a|s)}{\pi(a|s)}A^{\pi}(s,a)] 318 | $$ 319 | 320 | $$ 321 | \approx \frac{1}{1-\gamma} \mathbb{E}_ {s\sim d^{\pi},a\sim\pi} [\frac{\pi'(a|s)}{\pi(a|s)}A^{\pi}(s,a)] 322 | $$ 323 | 324 | $$ 325 | = \frac{1}{1-\gamma} L_{\pi}(\pi')。 326 | $$ 327 | 328 | 我们称 $L_{\pi}(\pi')$ 为替代目标。一个关键问题是什么时候我们可以做出上述近似。显然,当 $\pi=\pi'$ 时,近似变成了相等。然而这并不是有用的,因为我们希望将当前的策略 $\pi$ 改进为更好的策略 $\pi'$。在下面的信任区域策略优化(TRPO)的推导中,我们给出做出近似的界。 329 | 330 | ## 5. 信任区域策略优化(Trust Region Policy Optimization) 331 | 332 | TRPO [[3]](#ref3) 的关键思想是定义一个限制策略更新的信任区域。这个约束在策略空间中而不是在参数空间中,并且称为算法的新步长。通过这种方式,我们可以大致确保策略更新后的新策略比旧策略表现得更好。 333 | 334 | ### 5.1 问题设定(Problem Setup) 335 | 336 | 考虑一个有限状态和动作的 MDP,$\cal{M}=(S,A,M,R,\gamma)$,这里 $M$ 为状态转移函数。在这一节中,我们假设 $|S|$ 和 $|A|$ 都是有限的,并且假设 $0<\gamma<1$。尽管推导是基于有限状态和动作的,但算法对连续状态和动作同样有效。我们定义 337 | 338 | $$ 339 | d^{\pi}(s) = (1-\gamma)\sum_{t=0)}^{\infty}\gamma^{t}M(s_t=s|\pi) 340 | \tag{1} 341 | $$ 342 | 343 | 为始于状态 $s$,遵循策略 $\pi$ 与转移函数 $M$ 的衰减状态访问分布,并且定义 344 | 345 | 346 | $$ 347 | V^{\pi} = \frac{1}{1-\gamma} \mathbb{E}_{s\sim d^{\pi},a\sim\pi(\cdot|s),s'\sim M(\cdot|s,a)} [R(s,a,s')] 348 | \tag{2} 349 | $$ 350 | 351 | 352 | 为遵循 $\pi$ 与 $M$ 的期望衰减奖励和。注意这里的 $V^{\pi}$ 与前面提到的 $J(\theta)$ 定义相同。 353 | 354 | 令 $\rho_{\pi}^{t}\in\mathbb{R}^{|S|}$,$\rho_{\pi}^{t}(s)=M(s_t=s|\pi)$,这是遵循 $\pi$ 与 $M$ 时,在时间步 $t$ 时状态为 $s$ 的概率。 355 | 356 | 令 $P_{\pi}\in\mathbb{R}^{|S|\times|S|}$,这里 $P_{\pi}(s'|s)=\sum_a M(s'|s,a)\pi(a|s)$,这是遵循 $\pi$ 与 $M$ 时,在状态 $s$ 用一步转移到状态 $s'$ 的概率。 357 | 358 | 令 $\mu$ 为起始状态分布。我们有: 359 | 360 | $$ 361 | d^{\pi} = (1-\gamma) \sum_{t=0}^{\infty}\gamma^{t}M(s_t=s|\pi) 362 | $$ 363 | 364 | $$ 365 | = (1-\gamma) \sum_{t=0}^{\infty}\gamma^{t} P_{\pi}\mu 366 | $$ 367 | 368 | 369 | $$ 370 | = (1-\gamma) (I-\gamma P_{\pi})^{-1}\mu, 371 | \tag{3} 372 | $$ 373 | 374 | 375 | 第二个等号是因为 $\rho_{\pi}^{t}=P_{\pi}\rho_{\pi}^{t-1}$,第三个等号可以由几何级数推导得到。 376 | 377 | 我们的证明的目的是给出 $V^{\pi'}-V^{\pi}$ 的下界。我们从一个关于奖励调整的引理开始证明。 378 | 379 | **引理 5.1** 对于任意函数 $f:S\mapsto\mathbb{E}$ 和任意策略 $\pi$,我们有: 380 | 381 | $$ 382 | (1-\gamma) \mathbb{E}_{s\sim\mu}[f(s)] + \mathbb{E} _{s\sim d^{\pi},a\sim\pi(\cdot|s),s'\sim M(\cdot|s,a)} [\gamma f(s')] - \mathbb{E} _{s\sim d^{\pi}}[f(s)] = 0。 383 | \tag{4} 384 | $$ 385 | 386 | 这一引理的证明可以参见 [[4]](#ref4) 以及[附录 A.1](#lemma51p)。 387 | 388 | 我们可以将这一项添加到式([2](#eq2))的右侧: 389 | 390 | $$ 391 | V^{\pi}(s) = \frac{1}{1-\gamma} (\mathbb{E}_ {s\sim d^{\pi},a\sim\pi(\cdot|s),s'\sim M(\cdot|s,a)} [R(s,a,s')+\gamma f(s')-f(s)]) + \mathbb{E}_{s\sim\mu}[f(s)]。 392 | \tag{5} 393 | $$ 394 | 395 | 这可以被看作奖励调整的一种形式,改变函数是状态的函数而不是动作的函数。注意如果我们令 $f(s)=V^{\pi}(s)$,那么我们就得到了优势函数。 396 | 397 | ### 5.2 状态分布差异限制(Bounding Difference in State Distributions) 398 | 399 | 在更新 $\pi\to\pi'$ 时,我们有不同的衰减状态访问分布 $d^{\pi}$ 和 $d^{\pi'}$,现在我们来限制它们之间的差异。 400 | 401 | **引理 5.2** 402 | 403 | $$ 404 | \lVert d^{\pi'}-d^{\pi} \rVert_1 \leq \frac{2\gamma}{1-\gamma} (\mathbb{E}_ {s\sim d^{\pi}} [D_{TV}(\pi'\lVert \pi)[s]]) 405 | $$ 406 | 407 | 这一引理的证明可以参见 [[4]](#ref4) 以及[附录 A.2](#lemma52p)。 408 | 409 | ### 5.3 回报差异限制(Bounding Difference in Returns) 410 | 411 | 现在我们来限制 $V^{\pi'}-V^{\pi}$。 412 | 413 | **引理 5.3** 定义如下项: 414 | 415 | $$ 416 | L_{\pi}(\pi') = \mathbb{E}_{s\sim d^{\pi},a\sim\pi(\cdot|s)} [\frac{\pi'(a|s)}{\pi(a|s)} A^{\pi}(s,a)], 417 | $$ 418 | 419 | $$ 420 | \epsilon_f^{\pi'} = \mathop{\max}_ {s} |\mathbb{E}_{a\sim\pi'(\cdot|s),s'\sim M(\cdot|s,a)} [R(s,a,s')+\gamma f(s') - f(s)]|, 421 | $$ 422 | 423 | 我们有如下上界 424 | 425 | 426 | $$ 427 | V^{\pi'}-V^{\pi} \leq \frac{1}{1-\gamma}(L_{\pi}(\pi') + \lVert d^{\pi'}-d^{\pi} \rVert_1 \epsilon_f^{\pi'}) 428 | \tag{6} 429 | $$ 430 | 431 | 432 | 和如下下界 433 | 434 | 435 | $$ 436 | V^{\pi'}-V^{\pi} \geq \frac{1}{1-\gamma}(L_{\pi}(\pi') - \lVert d^{\pi'}-d^{\pi} \rVert_1 \epsilon_f^{\pi'})。 437 | \tag{7} 438 | $$ 439 | 440 | 441 | 这一引理的证明可以参见 [[4]](#ref4) 以及[附录 A.3](#lemma53p)。 442 | 443 | 注意,$\lVert d^{\pi'}-d^{\pi} \rVert_1$ 的上界已经由[引理 5.2](#lemma52) 给出,因此可以代入式([6](#eq6))和式([7](#eq7))。 444 | 445 | ### 5.4 最大优势限制(Bounding Maximum Advantage) 446 | 447 | 现在我们考虑这一项: 448 | 449 | $$ 450 | \epsilon_f^{\pi'} = \mathop{\max}_ {s} |\mathbb{E}_{a\sim\pi'(\cdot|s),s'\sim M(\cdot|s,a)} [R(s,a,s')+\gamma f(s') - f(s)]|。 451 | \tag{8} 452 | $$ 453 | 454 | 令 $f(s)=V^{\pi}(s)$,即 $f(s)$ 为遵循策略 $\pi$ 的状态 $s$ 的值函数。因此,我们有 455 | 456 | $$ 457 | \epsilon_{V^{\pi}}^{\pi'} = \mathop{\max}_ s |\mathbb{E}_{a\sim\pi'(\cdot|s)}[A^{\pi}(s,a)]|。 458 | \tag{9} 459 | $$ 460 | 461 | 这使得我们可以做出如下表述: 462 | 463 | **引理 5.4** 464 | 465 | $$ 466 | \epsilon_{V^{\pi}}^{\pi'} \leq 2\mathop{\max}_ s D_{TV}(\pi\lVert \pi') \mathop{\max}_{s,a}|A^{\pi}(s,a)|。 467 | $$ 468 | 469 | 这一引理的证明可以参见 [[5]](#ref5) 以及[附录 A.4](#lemma53p)。 470 | 471 | ### 5.5 信任区域策略优化(TRPO) 472 | 473 | 回忆一下,令 $f=V^{\pi}$,我们有 474 | 475 | $$ 476 | \frac{1}{1-\gamma} L_{\pi}(\pi') - \frac{4\epsilon\gamma}{(1-\gamma)^2}\alpha^2 \leq V^{\pi'}-V^{\pi} \leq \frac{1}{1-\gamma} L_{\pi}(\pi') + \frac{4\epsilon\gamma}{(1-\gamma)^2}\alpha^2, 477 | \tag{10} 478 | $$ 479 | 480 | 这里 481 | 482 | $$ 483 | L_{\pi}(\pi') = \mathbb{E}_{s\sim d^{\pi},a\sim\pi(\cdot|s)} [\frac{\pi'(a|s)}{\pi(a|s)} A^{\pi}(s,a)], 484 | $$ 485 | 486 | $$ 487 | \epsilon = \mathop{\max}_{s,a} |A^{\pi}(s,a)|, 488 | $$ 489 | 490 | $$ 491 | \alpha = \mathop{\max}_ s D_{TV}(\pi\lVert \pi')。 492 | $$ 493 | 494 | 将上述式子与上一节关于相对策略表现恒等式的式子相比,我们这一次给出了下界和上界,而不只是一个近似。通过优化 $V^{\pi'}-V^{\pi}$ 的下界,我们得到了一个保证策略改进的优化问题。具体来说,我们要解决以下优化问题: 495 | 496 | $$ 497 | \mathop{\max}_ {\pi'} L_{\pi}(\pi') - \frac{4\epsilon\gamma}{(1-\gamma)^2}\alpha^2。 498 | $$ 499 | 500 | 不幸的是,解决这个优化问题会导致非常小的步长。在 [[5]](#ref5) 中,在实现实用的算法时,作者将该优化问题转化为一个有约束优化问题来增大步长。具体来说,优化问题变为如下形式: 501 | 502 | $$ 503 | \mathop{\max}_ {\pi'} L_{\pi}(\pi') 504 | $$ 505 | 506 | $$ 507 | \text{s.t.} \quad \alpha^2 \leq \delta, 508 | $$ 509 | 510 | 这里 $\delta$ 为超参数。 511 | 512 | 由于存在大量的状态,$alpha$ 的最大约束无法求解。因此在 [[5]](#ref5) 中,作者使用了仅考虑平均 KL 散度的启发式近似。这样近似是有用的,因为我们可以用样本来近似期望而无法用样本来近似最大值。因此我们有: 513 | 514 | $$ 515 | \mathop{\max}_ {\pi'} L_{\pi}(\pi') 516 | $$ 517 | 518 | $$ 519 | \text{s.t.} \quad \overline{D}_{KL}(\pi,\pi') \leq \delta, 520 | $$ 521 | 522 | 这里 $\overline{D}_ {KL}(\pi,\pi') = \mathbb{E}_ {s\sim d^{\pi}}[D_{TV}(\pi\lVert \pi')[s]]$。 523 | 524 | ## 6. 练习 525 | 526 | **练习 6.1** 在课程幻灯片中,目标函数为 $J(\theta)=V^{\pi_{\theta}}(s_{start})$。对于这个目标函数,我们做了什么假设? 527 | 528 | **解答** 我们假设只有 $s_{start}$ 这一个起始状态。一般来说,可能有多个起始状态,这种情况下我们应该使用开始状态分布($\mu$)的期望。因此,更一般的目标函数为 $J(\theta) = \mathbb{E}_ {start\sim \mu}[V^{\pi_{\theta}}(s_{start})]$。 529 | 530 | **练习 6.2** 在有限时间步的设定下,我们讨论了一个可能的目标函数 $J(\theta)=\mathbb{E}_ {(s,a)\sim P_{\theta}(s,a)} [r(s,a)]$。课程幻灯片中,我们讨论了两个目标函数,我们可以使用平均价值 $J_{avV}(\theta)=\sum_{s} d^{\pi_{\theta}}(s) V^{\pi_{\theta}}(s)$,也可以使用每时间步的平均奖励 $J_{avR}(\theta) = \sum_{s} d^{\pi_{\theta}}(s) \sum_{a} \pi_{\theta}(a|s)r(s,a)$。$J(\theta)$ 与 $J_{avV}(\theta)$ 或 $J_{avR}(\theta)$ 是等价的吗? 531 | 532 | **解答** $J(\theta)$ 与每时间步的平均奖励等价。由 $P_{\theta}(s,a)$ 引出的 $(s,a)$ 的期望可以扩展为由状态分布 $d^{\pi_{\theta}}(s)$ 引出的 $s$ 的期望与由策略 $\pi_{\theta}(a|s)$ 引出的 $a$ 的期望。 533 | 534 | **练习 6.3** 使用有限差异来估计策略梯度的主要优点是什么? 535 | 536 | **解答** 这种方法适用于任何策略,即使策略不可导也可以。 537 | 538 | **练习 6.4** 在策略梯度中,对数求导技巧的意义是什么? 539 | 540 | **解答** 对数求导技巧使得梯度估计不依赖于动态模型,一般来说,动态模型是未知的。 541 | 542 | **练习 6.5** 在证明以状态为变量的基准函数无偏的推导中,我们利用了 $\mathbb{E}_ {a_{t}}[\nabla_{\theta} \log\pi_{\theta}(a_{t}|s_{t})]=0$ 这一事实。如何证明这个期望为 $0$? 543 | 544 | **解答** 545 | 546 | $$ 547 | \mathbb{E}_ {a_{t}}[\nabla_{\theta} \log\pi_{\theta}(a_t|s_t)] = \int_{a} \pi_{\theta}(a_t|s_t) 548 | \frac{\nabla_{\theta}\pi_{\theta}(a_t|s_t)}{\pi_{\theta}(a_t|s_t)} \text{d}a 549 | $$ 550 | 551 | $$ 552 | = \nabla_{\theta} \int_{a} \pi_{\theta}(a_t|s_t) \text{d}a = \nabla_{\theta}1 = 0。 553 | $$ 554 | 555 | **练习 6.6** 为什么我们不能直接进行下列优化? 556 | 557 | $$ 558 | \mathop{\max}_ {\pi'}J(\pi') = \mathop{\max}_ {\pi'}J(\pi') - J(\pi) = \mathop{\max}_ {\pi'}\mathbb{E}_ {\tau\sim\pi'}[\sum_{t=0}^{\infty}\gamma^{t}A^{\pi}(s_t,a_t)]。 559 | $$ 560 | 561 | **解答** 我们希望找到 $\pi'$,但为了达到这一目标,我们需要用 $\pi'$ 执行 rollout,这一过程过于缓慢。我们需要使用重要性采样。 562 | 563 | **练习 6.7** 这里是对离散动作空间使用自动微分来执行最大似然估计的伪代码。 564 | 565 | $\text{logits = policy.predictions(states)}$ 566 | 567 | $\text{negative_likelihoods = tf.nn.softmax_cross_entropy_with_logits(}$ 568 | 569 | $\quad\quad\text{labels=actions, logits=logits)}$ 570 | 571 | $\text{loss = tf.reduce_mean(negative_likelihoods)}$ 572 | 573 | $\text{gradients = loss.gradients(loss, variables)}$ 574 | 575 | 假设我们执行了 $N$ 个片段(episode),每个时间步都为 $T$,并且有 $d_a$ 个不同的动作和 $d_s$ 个状态维度,那么 $\text{actions}$ 和 $\text{states}$ 的形状是什么? 576 | 577 | 还已知 $\text{q_values}$ 的一个张量,这个张量的形状是什么? 578 | 579 | 已知 $\text{q_values}$,应该如何修改上述伪代码以执行策略梯度训练? 580 | 581 | **解答** $\text{actions}$ 的形状为 $(N\ast T,d_{a})$,$\text{states}$ 的形状为 $(N\ast T,d_{s})$,$\text{q_values}$ 的形状为 $(N\ast T,1)$。 582 | 583 | $\text{logits = policy.predictions(states)}$ 584 | 585 | $\text{negative_likelihoods = tf.nn.softmax_cross_entropy_with_logits(}$ 586 | 587 | $\quad\quad\text{labels=actions, logits=logits)}$ 588 | 589 | $\color{red}{\text{weighted_negative_likelihoods = tf.multiply(negative_likelihoods, q_values)}}$ 590 | 591 | $\text{loss = tf.reduce_mean(} \color{red}{\text{weighted_negative_likelihoods}})$ 592 | 593 | $\text{gradients = loss.gradients(loss, variables)}$ 594 | 595 | 因此,策略梯度可以被视为一种加权的最大似然估计,这里的权重为在状态 $s$ 执行动作 $a$ 的期望衰减回报,这个期望衰减回报值越高,权重就越大,梯度就越大,因此更新也就越大。 596 | 597 | ## 参考文献 598 | 599 | 1. http://rail.eecs.berkeley.edu/deeprlcourse/static/slides/lec-5.pdf. 600 | 601 | 2. http://rail.eecs.berkeley.edu/deeprlcourse/static/slides/lec-9.pdf. 602 | 603 | 3. C. Wu et al, "Variance reduction for policy gradient with action-dependent factorized baselines," *ICLR*, 2018. 604 | 605 | 4. J. Achiam, D. Held, A. Tamar, and P. Abbeel, "Constrained policy optimization," *ICML*, 2017. 606 | 607 | 5. J. Schulman et al, "Trust region policy optimization," *ICML*, 2015. 608 | 609 | ## A. TRPO 证明(TRPO Proofs) 610 | 611 | ### A.1 奖励调整(Reward Shaping) 612 | 613 | 这里我们证明[引理 5.1](#lemma51)。 614 | 615 | 证明: 616 | 617 | $$ 618 | d^{\pi} = (1-\gamma)(I-\gamma P_{\pi})^{-1}\mu, 619 | $$ 620 | 621 | $$ 622 | (1-\gamma)(I-\gamma P_{\pi}) d^{\pi} = (1-\gamma)\mu, 623 | $$ 624 | 625 | 与 $f(s)$ 取点乘,我们得到: 626 | 627 | $$ 628 | \mathbb{E}_ {s\sim d^{\pi}}[f(s)] - \mathbb{E}_ {s\sim d^{\pi},a\sim\pi(\cdot|s),s'\sim M(\cdot|s,a)}[\gamma f(s')] = (1-\gamma)\mathbb{E}_{s\sim\mu}[f(s)]。 629 | \tag{11} 630 | $$ 631 | 632 | 证明完毕。$\diamondsuit$ 633 | 634 | ### A.2 状态分布差异限制(Bounding Difference in State Distributions) 635 | 636 | 这里我们证明[引理 5.2](#lemma52)。 637 | 638 | 证明: 639 | 640 | 回忆式([3](#eq3))我们有 $d^{\pi}=(1-\gamma)(I-\gamma P_{\pi})^{-1}\mu$。 641 | 642 | 定义 $G=(I-\gamma P_{\pi})^{-1}$,$\overline{G}=(I-\gamma P_{\pi'})^{-1}$,$\Delta=P_{\pi'}-P_{\pi}$。我们有: 643 | 644 | $$ 645 | G^{-1}-\overline{G}^{-1} = (I-\gamma P_{\pi})-(I-\gamma P_{\pi'}) 646 | $$ 647 | 648 | $$ 649 | = \gamma (P_{\pi'}-P_{\pi}) 650 | $$ 651 | 652 | $$ 653 | = \gamma \Delta, 654 | $$ 655 | 656 | $$ 657 | \Rightarrow \overline{G}-G = \overline{G}(G^{-1}-\overline{G}^{-1})G = \gamma \overline{G} \Delta G, 658 | $$ 659 | 660 | 661 | $$ 662 | d^{\pi'}-d^{\pi} = (1-\gamma)(\overline{G}-G)\mu 663 | $$ 664 | 665 | 666 | $$ 667 | =(1-\gamma)\gamma\overline{G} \Delta G \mu 668 | $$ 669 | 670 | $$ 671 | = \gamma \overline{G} \Delta (1-\gamma) G \mu 672 | $$ 673 | 674 | $$ 675 | = \gamma \overline{G} \Delta d^{\pi}, 676 | \tag{12} 677 | $$ 678 | 679 | 取式([12](#eq12))的 $l_1$ 范数,根据范数的性质,我们有: 680 | 681 | $$ 682 | \lVert d^{\pi'}-d^{\pi} \rVert_{1} = \gamma \lVert \overline{G} \Delta d^{\pi} \rVert_{1} \leq \lVert\overline{G}\rVert_{1} \lVert \Delta d^{\pi} \rVert_{1}。 683 | \tag{13} 684 | $$ 685 | 686 | 我们首先限制 $\lVert\overline{G}\rVert_{1}$。 687 | 688 | 689 | $$ 690 | \lVert\overline{G}\rVert_{1} = \lVert (I-\gamma P_{\pi'})^{-1} \rVert_{1} = \lVert \sum_{t=0}^{\infty}\gamma^{t} P_{\pi'}^{t} \rVert_{1} \leq \sum_{t=0}^{\infty} \gamma^{t} \lVert P 691 | _ {\pi'} \rVert_{1}^{t} = \frac{1}{1-\gamma}。 692 | \tag{14} 693 | $$ 694 | 695 | 696 | 接下来限制 $\lVert \Delta d^{\pi} \rVert_{1}$。 697 | 698 | 699 | $$ 700 | \lVert \Delta d^{\pi} \rVert_{1} = \sum_{s'} |\sum_{s} \Delta(s'|s)d^{\pi}(s)| 701 | $$ 702 | 703 | $$ 704 | \leq \sum_{s',s}|\Delta(s'|s)|d^{\pi}(s) 705 | $$ 706 | 707 | $$ 708 | = \sum_{s',s}|\sum_{a} (M(s'|s,a)\pi'(a|s)-M(s'|s,a)\pi(a|s))|d^{\pi}(s) 709 | $$ 710 | 711 | $$ 712 | = \sum_{s',s}|\sum_{a} M(s'|s,a)(\pi'(a|s)-\pi(a|s))|d^{\pi}(s) 713 | $$ 714 | 715 | $$ 716 | \leq \sum_{s',s,a} M(s'|s,a)|\pi'(a|s)-\pi(a|s)|d^{\pi}(s) 717 | $$ 718 | 719 | $$ 720 | = \sum_{s,a}|\pi'(a|s)-\pi(a|s)|d^{\pi}(s) 721 | $$ 722 | 723 | $$ 724 | =\sum_{s} d^{\pi}(s) \sum_{a} |\pi'(a|s)-\pi(a|s)| 725 | $$ 726 | 727 | $$ 728 | =2 \mathbb{E}_ {s\sim d^{\pi}}[D_{TV}(\pi'||\pi)[s]] 729 | \tag{15}。 730 | $$ 731 | 732 | 733 | 结合式([13](#eq13)),([14](#eq14))和([15](#eq15)),我们有: 734 | 735 | $$ 736 | \lVert d^{\pi'}-d^{\pi} \rVert_{1} \leq \frac{2\gamma}{1-\gamma}(\mathbb{E}_ {s\sim d^{\pi}}[D_{TV}(\pi'||\pi)[s]])。 737 | \tag{16} 738 | $$ 739 | 740 | 证明完毕。$\diamondsuit$ 741 | 742 | ### A.3 回报差异限制(Bounding Difference in Returns) 743 | 744 | 这里我们证明[引理 5.3](#lemma53)。 745 | 746 | 证明: 747 | 748 | 定义 $\delta_{f}(s,a,s')=R(s,a,s')+\gamma f(s')-f(s)$。 749 | 750 | 由于 751 | 752 | $$ 753 | V^{\pi}=\frac{1}{1-\gamma}(\mathbb{E}_ {s\sim d^{\pi},a\sim\pi(\cdot|s),s'\sim M(\cdot|s,a)}[\delta_{f}(s,a,s')]) + \mathbb{E}_{s\sim\mu}[f(s)], 754 | $$ 755 | 756 | 我们有: 757 | 758 | 759 | $$ 760 | V^{\pi'}-V^{\pi} = \frac{1}{1-\gamma}(\mathbb{E}_ {s\sim d^{\pi'},a\sim\pi'(\cdot|s),s'\sim M(\cdot|s,a)}[\delta_{f}(s,a,s')] - \mathbb{E}_ {s\sim d^{\pi},a\sim\pi(\cdot|s),s'\sim M(\cdot|s,a)}[\delta_{f}(s,a,s')])。 761 | \tag{17} 762 | $$ 763 | 764 | 765 | 我们先来关注第一项。 766 | 767 | 令 $\overline{\delta}_ {f}^{\pi'}(s)=\mathbb{E}_ {a\sim\pi'(\cdot|s),s'\sim M(\cdot|s,a)}[\delta_{f}(s,a,s')] \in \mathbb{R}^{|S|}$,那么: 768 | 769 | 770 | $$ 771 | \mathbb{E}_ {s\sim d^{\pi'},a\sim\pi',s'\sim M}[\delta_{f}(s,a,s')] = \langle d^{\pi'}, \overline{\delta}_ {f}^{\pi'}\rangle 772 | $$ 773 | 774 | $$ 775 | = \langle d^{\pi}, \overline{\delta}_ {f}^{\pi'} \rangle + \langle d^{\pi'}-d^{\pi}, \overline{\delta}_ {f}^{\pi'} \rangle 776 | $$ 777 | 778 | $$ 779 | \leq \langle d^{\pi}, \overline{\delta}_ {f}^{\pi'} \rangle + \lVert d^{\pi'}-d^{\pi} \rVert_{1} \lVert \overline{\delta}_ {f}^{\pi'} \rVert_{\infty} 780 | $$ 781 | 782 | $$ 783 | = \langle d^{\pi}, \overline{\delta}_ {f}^{\pi'} \rangle + \lVert d^{\pi'}-d^{\pi} \rVert_{1} \epsilon_{f}^{\pi'}, 784 | \tag{18} 785 | $$ 786 | 787 | 788 | 这里 $\epsilon_{f}^{\pi'}=\mathop{\max}_ {s} \mathbb{E}_{a\sim\pi'(\cdot|s),s'\sim M(\cdot|s,a)}[R(s,a,s')+\gamma f(s') - f(s)]$; 789 | 790 | 并且: 791 | 792 | 793 | $$ 794 | \mathbb{E}_ {s\sim d^{\pi'},a\sim\pi',s'\sim M}[\delta_{f}(s,a,s')] = \langle d^{\pi'}, \overline{\delta}_ {f}^{\pi'}\rangle 795 | $$ 796 | 797 | $$ 798 | = \langle d^{\pi}, \overline{\delta}_ {f}^{\pi'} \rangle - \langle d^{\pi}-d^{\pi'}, \overline{\delta}_ {f}^{\pi'} \rangle 799 | $$ 800 | 801 | $$ 802 | \geq \langle d^{\pi}, \overline{\delta}_ {f}^{\pi'} \rangle - \lVert d^{\pi}-d^{\pi'} \rVert_{1} \lVert \overline{\delta}_ {f}^{\pi'} \rVert_{\infty} 803 | $$ 804 | 805 | $$ 806 | = \langle d^{\pi}, \overline{\delta}_ {f}^{\pi'} \rangle - \lVert d^{\pi'}-d^{\pi} \rVert_{1} \epsilon_{f}^{\pi'}, 807 | \tag{19} 808 | $$ 809 | 810 | 811 | 将式([18](#eq18))代入式([17](#eq17)),我们得到: 812 | 813 | 814 | $$ 815 | (1-\gamma)(V^{\pi'}-V^{\pi}) \leq \langle d^{\pi}, \overline{\delta}_ {f}^{\pi'} \rangle + \lVert d^{\pi'}-d^{\pi} \rVert_{1} \epsilon_{f}^{\pi'} - \mathbb{E}_ {s\sim d^{\pi},a\sim\pi(\cdot|s),s'\sim M(\cdot|s,a)}[\delta_{f}(s,a,s')] 816 | $$ 817 | 818 | $$ 819 | = \mathbb{E}_ {s\sim d^{\pi'},a\sim\pi'(\cdot|s),s'\sim M(\cdot|s,a)}[\delta_{f}(s,a,s')] - \mathbb{E}_ {s\sim d^{\pi},a\sim\pi(\cdot|s),s'\sim M(\cdot|s,a)}[\delta_{f}(s,a,s')] + \lVert d^{\pi'}-d^{\pi} \rVert_{1} \epsilon_{f}^{\pi'} 820 | $$ 821 | 822 | $$ 823 | = \mathbb{E}_ {s\sim d^{\pi},a\sim\pi(\cdot|s),s'\sim M(\cdot|s,a)}[(\frac{\pi'(a|s)}{\pi(a|s)}-1)\delta_{f}(s,a,s')] + \lVert d^{\pi'}-d^{\pi} \rVert_{1} \epsilon_{f}^{\pi'} 824 | \tag{20}。 825 | $$ 826 | 827 | 828 | 定义 829 | 830 | $$ 831 | L_{\pi}(\pi') = \mathbb{E}_ {s\sim d^{\pi},a\sim\pi(\cdot|s),s'\sim M(\cdot|s,a)}[(\frac{\pi'(a|s)}{\pi(a|s)}-1)(R(s,a,s')+\gamma f(s')-f(s))], 832 | $$ 833 | 834 | 若我们选择 $f=V^{\pi}$,那么: 835 | 836 | $$ 837 | L_{\pi}(\pi') = \mathbb{E}_ {s\sim d^{\pi},a\sim\pi(\cdot|s)}[\frac{\pi'(a|s)}{\pi(a|s)}A^{\pi}(s,a)], 838 | $$ 839 | 840 | 这是因为根据相同策略选择动作的优势为 $0$: 841 | 842 | $$ 843 | \mathbb{E}_ {s\sim d^{\pi},a\sim\pi(\cdot|s)}[A^{\pi}(s,a)]=0。 844 | $$ 845 | 846 | 根据式([20](#eq20))我们有: 847 | 848 | $$ 849 | V^{\pi'}-V^{\pi}\leq \frac{1}{1-\gamma}(L_{\pi}(\pi')+\lVert d^{\pi'}-d^{\pi} \rVert_{1}\epsilon_{f}^{\pi'})。 850 | \tag{21} 851 | $$ 852 | 853 | 同理,根据式([17](#eq17))和式([19](#19))我们能推出下界: 854 | 855 | $$ 856 | V^{\pi'}-V^{\pi}\geq \frac{1}{1-\gamma}(\mathbb{E}_ {s\sim d^{\pi},a\sim\pi(\cdot|s),s'\sim M(\cdot|s,a)}[(\frac{\pi'(a|s)}{\pi(a|s)}-1)\delta_{f}(s,a,s')] - \lVert d^{\pi'}-d^{\pi} \rVert_{1}\epsilon_{f}^{\pi'}) 857 | $$ 858 | 859 | $$ 860 | = \frac{1}{1-\gamma}(L_{\pi}(\pi')-\lVert d^{\pi'}-d^{\pi} \rVert_{1}\epsilon_{f}^{\pi'})。 861 | \tag{22} 862 | $$ 863 | 864 | 证明完毕。$\diamondsuit$ 865 | 866 | ### A.4 最大优势(Maximum Advantage) 867 | 868 | 这里我们证明[引理 5.4](#lemma54)。 869 | 870 | 证明: 871 | 872 | 像 [[5]](#ref5) 那样,我们称 $(\pi,\pi')$ 为 $\alpha$-coupled 策略对,如果对于任意 $s$ 都有 $P[a\neq\hat{a}|s]\leq\alpha$,这里 $(a,\hat{a})|s$ 为 $(\pi,\pi')$ 定义的联合分布。定义 $\alpha_{\pi,\pi'}$ 使得 $(\pi,\pi')$ 为 $\alpha_{\pi,\pi'}$-coupled 策略对。令 $\overline{A}(s)$ 为在状态 $s$ 时 $A^{\pi}(s,\hat{a})$ 的期望,这里 $\hat{a}$ 遵循策略 $\pi'$: 873 | 874 | 875 | $$ 876 | \overline{A}(s) = \mathbb{E}_{\hat{a}\sim\pi'(\cdot|s)}[A^{\pi}(s,\hat{a})]。 877 | \tag{23} 878 | $$ 879 | 880 | 881 | 由于 $\mathbb{E}_{a\sim\pi(\cdot|s)}[A^{\pi}(s,a)|s]=0$,我们将式([23](#eq23))写为: 882 | 883 | $$ 884 | \overline{A}(s) = \mathbb{E}_{(a,\hat{a})\sim(\pi,\pi')}[A^{\pi}(s,\hat{a})-A^{\pi}(s,a)] 885 | $$ 886 | 887 | $$ 888 | = P[a\neq\hat{a}|s] \mathbb{E}_{(a,\hat{a})\sim(\pi,\pi')}[A^{\pi}(s,\hat{a})-A^{\pi}(s,a)|a\neq\hat{a}] 889 | $$ 890 | 891 | $$ 892 | \text{+} P[a=hat{a}|s] \mathbb{E}_{(a,\hat{a})\sim(\pi,\pi')}[A^{\pi}(s,\hat{a})-A^{\pi}(s,a)] 893 | $$ 894 | 895 | $$ 896 | \leq \alpha_{\pi,\pi'} \mathbb{E}_{(a,\hat{a})\sim(\pi,\pi')}[A^{\pi}(s,\hat{a})-A^{\pi}(s,a)|a\neq\hat{a}] + P[a=\hat{a}|s]\ast 0 897 | $$ 898 | 899 | $$ 900 | \leq 2 \alpha_{\pi,\pi'} \mathop{\max}_{a} |A^{\pi}(s,a)|。 901 | \tag{24} 902 | $$ 903 | 904 | 因此,我们有: 905 | 906 | $$ 907 | \epsilon_{V^{\pi}}^{\pi'} = \mathop{\max}_ {s} |\mathbb{E}_ {s\sim d^{\pi'},a\sim\pi'(\cdot|s),s'\sim M(\cdot|s,a)} [R(s,a,s')+\gamma f(s')-f(s)]| 908 | $$ 909 | 910 | $$ 911 | \leq \mathop{\max}_ {s} |2\alpha_{\pi,\pi'} \mathop{\max}_{a} |A^{\pi}(s,a)|| 912 | $$ 913 | 914 | $$ 915 | \leq 2 \alpha_{\pi,\pi'} \mathop{\max}_{s,a}|A^{\pi}(s,a)|。 916 | \tag{25} 917 | $$ 918 | 919 | 假设 $p_{X}$ 和 $p_{Y}$ 为概率分布且满足 $D_{TV}(p_{X}\lVert p_{Y})=\alpha$,那么存在边界为 $p_{X}$ 和 $p_{Y}$ 的联合分布 $(X,Y)$ 且 $X=Y$ 的概率为 $1-\alpha$ [[5]](#ref5)。取 $\alpha_{\pi,\pi'}=\mathop{\max}_ {s} D_{TV}(\pi\lVert\pi')$,我们有: 920 | 921 | $$ 922 | \epsilon_{V^{\pi}}^{\pi'} \leq 2 \mathop{\max}_ {s} D_{TV}(\pi\lVert\pi') \mathop{\max}_{s,a}|A^{\pi}(s,a)|。 923 | \tag{26} 924 | $$ 925 | 926 | 证明完毕。$\diamondsuit$ --------------------------------------------------------------------------------