PPO算法实现的37个实现细节(3/3)9 details for continuous action domains

博客标题:The 37 Implementation Details of Proximal Policy
Optimization
作者:Huang, Shengyi; Dossa, Rousslan Fernand Julien; Raffin, Antonin; Kanervisto, Anssi; Wang, Weixun
博客地址:https://iclr-blog-track.github.io/2022/03/25/ppo-implementation-details/
代码github仓库地址:https://github.com/vwxyzjn/ppo-implementation-details

本文接上篇PPO算法实现的37个实现细节(2/3)9 Atari-specific implementation details继续,本篇主要介绍与Mujoco类型的环境的场景下,实现PPO算法的9个实现细节。

1. Continuous actions via normal distributions 【Theory】

Policy gradient(包括 PPO)假定连续动作是从正态分布normal distribution中采样的。因此,要创建这样的分布,神经网络需要输出连续动作的平均值和标准偏差。
当强化学习算法应用在连续动作空间环境中时,选择高斯分布Gaussian distribution来表示动作分布是非常流行的。

2. State-independent log standard deviation【Theory】

The implementation outputs the logits for the mean, but instead of outputting the logits for the standard deviation, it outputs the logarithm of the standard deviation. In addition, this log std is set to be state-independent and initialized to be 0.

Schulman et al., (2015) and Duan et al., (2016) use state-independent standard deviation, while Haarnoja et al., (2018) uses the state-dependent standard deviation, that is, the mean and standard deviation are output at the same time. Andrychowicz, et al. (2021) compared two different implementations and found that the performance is very close.

3. Independent action components 【Theory】

在许多机器人任务中,通常使用多个标量值来表示一个连续动作。比如下面的公式也许含义是向左移动 2.4 米,再向上移动 3.5 米。
a t = [ a t 1 , a t 2 ] = [ 2.4 , 3.5 ] \begin{array}{c} a_{t} = \left[a_{t}^{1}, a_{t}^{2}\right] = [2.4,3.5] \end{array} at=[at1,at2]=[2.4,3.5]
然而,大多数关于Policy gradient的文献都认为 a t a_{t} at 应该是是一个单一的标量值。考虑到这一差异,PPO 采用下面的公式计算: prob ⁡ ( a t ) = prob ⁡ ( a t 1 ) ⋅ prob ⁡ ( a t 2 ) \begin{array}{c} \operatorname{prob}\left(a_{t}\right) = \operatorname{prob}\left(a_{t}^{1}\right) \cdot \operatorname{prob}\left(a_{t}^{2}\right) \end{array} prob(at)=prob(at1)prob(at2)

这种方法源自目前常用的假设: 使用具有全协方差的高斯分布来表示策略,这意味着每个维度的行动选择都是独立进行的。面对多维行动空间环境,Tavakoli 等人(2018)也认为每个行动维度都应独立选择,并通过设计网络结构来实现这一目标。尽管我们的直觉告诉我们,在某些环境中,不同policy维度的行动选择之间可能存在依赖关系,但什么是最优选择仍然是一个悬而未决的问题。值得注意的是,这个问题已经引起了社会各界的关注,并开始尝试从不同维度对行动的依赖性进行建模,例如使用自动回归策略。

4. Separate MLP networks for policy and value functions 【Neural Network】

对于连续控制任务,PPO 使用由两层 64 个神经元组成的简单 MLP 网络,使用 Tanh()作为策略网络和价值网络的激活函数。下面是一个伪代码(也结合了前面 3 个细节):

 value_network = Sequential(
      layer_init(Linear(np.array(envs.single_observation_space.shape).prod(), 64)),
      Tanh(),
      layer_init(Linear(64, 64)),
      Tanh(),
      layer_init(Linear(64, 1), std=1.0),
  )
  policy_mean = Sequential(
      layer_init(Linear(np.array(envs.single_observation_space.shape).prod(), 64)),
      Tanh(),
      layer_init(Linear(64, 64)),
      Tanh(),
      layer_init(Linear(64, envs.single_action_space.n), std=0.01),
  )
  policy_logstd = nn.Parameter(torch.zeros(1, np.prod(envs.single_action_space.shape)))
  value = value_network(observation)
  probs = Normal(
      policy_mean(x),
      policy_logstd.expand_as(action_mean).exp(),
  )
  action = probs.sample()
  logprob = probs.log_prob(action).sum(1)

Andrychowicz 等人(2021 年)发现,独立的策略和价值网络通常会带来更好的效果。

5. Handling of action clipping to valid range and storage 【Code-level Optimizations】

在对连续动作进行采样后,该动作可能无效,因为它可能超出环境中连续动作的有效范围。为避免出现这种情况,可添加应用 clip 将动作夹入有效范围内。但是,未进行clip操作的原始动作会作为episodic 数据的一部分存储。

由于高斯分布的采样没有边界,而环境通常对行动空间有一定的限制。因此,Duan 等人(2016 年)在其界限中采用了剪切采样动作,Haarnoja 等人(2018 年)对高斯采样采用了invertible squashing functiontanh)来满足约束条件。Andrychowicz 等人(2021 年)比较了两种实现方法,发现 tanh 方法更好。但为了获得一致的性能,我们选择了clip 的实现方式。值得注意的是,Chou 2017 和 Fujita 等人(2018)指出了clip 法带来的偏差,并提出了不同的解决方案。

6. Normalization of Observation【Environment Preprocessing】

在每个 timestepVecNormalize wrapper都会对观测数据进行预处理,然后再将其输入 PPO agent中。原始观测值通过减去运行平均值再除以方差进行归一化处理。

对输入进行归一化处理已成为一种众所周知的神经网络训练技术。Duan 等人(2016 年)采用移动平均归一化 moving average normalization观测值来处理网络输入,这也成为后续实施的默认选择。Andrychowicz 等人(2021 年)通过实验确定,观察结果的标准化对性能非常有帮助。

7. Observation Clipping【Environment Preprocessing】

在观测值归一化之后,归一化后的观测值会被 VecNormalize 进一步clip到一个范围,通常是 [-10,10]。

Andrychowicz 等人(2021 年)发现,在对观察结果进行归一化处理后,使用观察结果剪切法对成绩并无帮助,但他们猜测,在观察结果范围较大的环境中,使用剪切法可能会有所帮助。

8. Reward Scaling 【Environment Preprocessing】

VecNormalize 还应用了某种certain discount-based scaling,即用折扣奖励总和的标准差(不减去和重新加上平均值)来除以奖励。

The VecNormalize also applies a certain discount-based scaling scheme, where the rewards are divided by the standard deviation of a rolling discounted sum of the rewards (without subtracting and re-adding the mean).

Engstrom、Ilyas 等人(2020 年)报告称,奖励缩放reward scaling 会显著影响算法的性能,并建议使用奖励缩放reward scaling

9. Reward Clipping【Environment Preprocessing】

在对奖励进行缩放后,VecNormalize 会将缩放后的奖励进一步剪切到一个范围,通常为 [-10,10]。

Mnih 等人(2015)中也有类似的方法。目前还没有明确的证据表明,在奖励缩放之后进行奖励剪切有助于学习。

我们对 ppo.py 进行了约 25 行代码的修改,以纳入这 9 个细节,最终生成了一个独立的 ppo_continuous_action.py(链接),其中包含 331 行代码。下图显示了 ppo.py(左)和 ppo_continuous_action.py(右)的文件差异。见原博客。

需要注意的是,value_network='copy’表示对策略和价值函数分别使用不同的 MLP 网络(即本节的第 4 个实现细节)。此外,环境参数 N(即 num_envs)设为 1(common/cmd_util.py#L167)。以下是基准测试结果。

PPO算法实现的37个实现细节(3/3)9 details for continuous action domains_第1张图片

你可能感兴趣的:(强化学习,深度强化学习代码实践,深度强化学习,PPO算法,深度学习,人工智能)