深度强化学习入门难?这份资料手把手教会你

机器之心整理

参与:一鸣

深度强化学习在机器学习领域的热度一直很高。最近,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(128, 128),
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

发表评论
留言与评论(共有 0 条评论)
   
验证码:

相关文章

推荐文章

'); })();