手把手教你入门深度强化学习(附链接&代码)

2019 年 7 月 16 日 THU数据派

来源:机器之心

本文约2600字,建议阅读10分钟。

GitHub 开源了一份深度强化学习的教程,总结了从 DQN 到彩虹模型的理论和代码实现。


深度强化学习在机器学习领域的热度一直很高。最近,GitHub 开源了一份深度强化学习的教程,总结了从 DQN 到彩虹模型的理论和代码实现。读者朋友可以根据需要学习研究。教程代码基于 PyTorch,可在 Colab 中运行。


深度强化学习是强化学习中的重要研究领域。这一技术使用深度神经网络,提升智能体在训练中的表现。而目前深度强化学习教程较为零散,使得入门这一领域较为困难。近日,两位来自韩国的机器学习研究员整理了深度强化学习的相关教程和代码,并在 GitHub 上开源。代码基于 PyTorch,用户可以在 Colab 中运行。这一项目在 Reddit 上获得高赞。


教程地址:https://github.com/Curt-Park/rainbow-is-all-you-need


教程里有什么


根据 GitHub 页面的介绍,教程总共分为八个章节,从 DQN 开始,逐渐深入,最终一章为彩虹模型。每个章节都包括理论介绍和面向对象的实现。


以第一章的 DQN 代码为例:


1. 定义一个由三个全连接层组成的网络,将这一网络作为智能体的主体:


class Network(nn.Module):    def __init__(self, in_dim: int, out_dim: int):

            """Initialization."""

            super(Network, self).__init__()

            self.layers = nn.Sequential(
                nn.Linear(in_dim, 128), 
                nn.ReLU(),
                nn.Linear(128128), 
                nn.ReLU(), 
                nn.Linear(128, out_dim)

                )

    def forward(self, x: torch.Tensor) -> torch.Tensor:

            """Forward method implementation."""

            return self.layers(x)


2. 定义智能体等,其中“select_action”函数定义选择策略的方式,“step”定义每一步智能体和环境交互获得的 reward,“update_model”定义梯度下降的方法。


class DQNAgent:        def select_action(self, state: np.ndarray) -> np.ndarray:

                """Select an action from the input state."""

                # epsilon greedy policy

                if self.epsilon > np.random.random():
                    selected_action = self.env.action_space.sample()
                else:
                    selected_action = self.dqn(
                    torch.FloatTensor(state).to(self.device)).argmax()
                    selected_action = selected_action.detach().cpu().numpy()
                if not self.is_test:
                    self.transition = [state, selected_action]

                return selected_action

        def step(self, action: np.ndarray) -> Tuple[np.ndarray, np.float64, bool]:

            """Take an action and return the response of the env."""

            next_state, reward, done, _ = self.env.step(action)

            if not self.is_test:
                self.transition += [reward, next_state, done]
                self.memory.store(*self.transition)

            return next_state, reward, done

        def update_model(self) -> torch.Tensor:

            """Update the model by gradient descent."""

            samples = self.memory.sample_batch()
            loss = self._compute_dqn_loss(samples)
            self.optimizer.zero_grad()
            loss.backward()
            # gradient clipping
            # https://pytorch.org/docs/stable/nn.html#torch.nn.utils.clip_grad_norm_
            clip_grad_norm_(self.dqn.parameters(), 1.0, norm_type=1)
            self.optimizer.step()
            return loss.item()


教程目录


1. DQN

2. DoubleDQN

3. 优先经验回放(PrioritizedExperienceReplay)

4. DuelingNet

5. NoisyNet

6. CategoricalDQN

7. N-步学习(N-stepsLearning)

8. 彩虹模型(Rainbow)


安装方法


怎样使用这份教程呢?首先,用户需要从 GitHub 克隆代码并进入项目目录:


git clone https://github.com/Curt-Park/raibow-is-all-you-need.git

cd rainbow-is-all-you-need


接下来安装项目依赖即可:


make dep


编辑:王菁

校对:林亦霖


登录查看更多
75

相关内容

深度强化学习 (DRL) 是一种使用深度学习技术扩展传统强化学习方法的一种机器学习方法。 传统强化学习方法的主要任务是使得主体根据从环境中获得的奖赏能够学习到最大化奖赏的行为。然而,传统无模型强化学习方法需要使用函数逼近技术使得主体能够学习出值函数或者策略。在这种情况下,深度学习强大的函数逼近能力自然成为了替代人工指定特征的最好手段并为性能更好的端到端学习的实现提供了可能。
【IJCAI2020-华为诺亚】面向深度强化学习的策略迁移框架
专知会员服务
25+阅读 · 2020年5月25日
深度强化学习策略梯度教程,53页ppt
专知会员服务
176+阅读 · 2020年2月1日
《动手学深度学习》(Dive into Deep Learning)PyTorch实现
专知会员服务
116+阅读 · 2019年12月31日
【书籍】深度学习框架:PyTorch入门与实践(附代码)
专知会员服务
160+阅读 · 2019年10月28日
【开源书】PyTorch深度学习起步,零基础入门(附pdf下载)
专知会员服务
107+阅读 · 2019年10月26日
机器学习入门的经验与建议
专知会员服务
90+阅读 · 2019年10月10日
TensorFlow 2.0 学习资源汇总
专知会员服务
66+阅读 · 2019年10月9日
专知会员服务
198+阅读 · 2019年8月30日
深度强化学习入门难?这份资料手把手教会你
机器之心
9+阅读 · 2019年7月11日
从入门到精通-Tensorflow深度强化学习课程
深度学习与NLP
23+阅读 · 2019年3月7日
最新翻译的官方 PyTorch 简易入门教程
人工智能头条
10+阅读 · 2019年1月10日
深度强化学习入门,这一篇就够了!
机器学习算法与Python学习
26+阅读 · 2018年8月17日
论文结果难复现?本文教你完美实现深度强化学习算法DQN
中国人工智能学会
5+阅读 · 2017年11月24日
手把手教TensorFlow(附代码)
深度学习世界
15+阅读 · 2017年10月17日
手把手教你由TensorFlow上手PyTorch(附代码)
数据派THU
5+阅读 · 2017年10月1日
强化学习 cartpole_a3c
CreateAMind
9+阅读 · 2017年7月21日
Arxiv
4+阅读 · 2019年9月26日
Arxiv
6+阅读 · 2019年7月29日
Image Captioning based on Deep Reinforcement Learning
Arxiv
6+阅读 · 2018年4月3日
Arxiv
16+阅读 · 2018年4月2日
VIP会员
相关VIP内容
【IJCAI2020-华为诺亚】面向深度强化学习的策略迁移框架
专知会员服务
25+阅读 · 2020年5月25日
深度强化学习策略梯度教程,53页ppt
专知会员服务
176+阅读 · 2020年2月1日
《动手学深度学习》(Dive into Deep Learning)PyTorch实现
专知会员服务
116+阅读 · 2019年12月31日
【书籍】深度学习框架:PyTorch入门与实践(附代码)
专知会员服务
160+阅读 · 2019年10月28日
【开源书】PyTorch深度学习起步,零基础入门(附pdf下载)
专知会员服务
107+阅读 · 2019年10月26日
机器学习入门的经验与建议
专知会员服务
90+阅读 · 2019年10月10日
TensorFlow 2.0 学习资源汇总
专知会员服务
66+阅读 · 2019年10月9日
专知会员服务
198+阅读 · 2019年8月30日
相关资讯
深度强化学习入门难?这份资料手把手教会你
机器之心
9+阅读 · 2019年7月11日
从入门到精通-Tensorflow深度强化学习课程
深度学习与NLP
23+阅读 · 2019年3月7日
最新翻译的官方 PyTorch 简易入门教程
人工智能头条
10+阅读 · 2019年1月10日
深度强化学习入门,这一篇就够了!
机器学习算法与Python学习
26+阅读 · 2018年8月17日
论文结果难复现?本文教你完美实现深度强化学习算法DQN
中国人工智能学会
5+阅读 · 2017年11月24日
手把手教TensorFlow(附代码)
深度学习世界
15+阅读 · 2017年10月17日
手把手教你由TensorFlow上手PyTorch(附代码)
数据派THU
5+阅读 · 2017年10月1日
强化学习 cartpole_a3c
CreateAMind
9+阅读 · 2017年7月21日
Top
微信扫码咨询专知VIP会员