天天看點

Stable-Baselines 3 部分源代碼解讀 3 ppo.pyStable-Baselines 3 部分源代碼解讀 ./ppo/ppo.py

Stable-Baselines 3 部分源代碼解讀 ./ppo/ppo.py

前言

閱讀PPO相關的源碼,了解一下标準庫是如何建立PPO算法以及各種tricks的,以便于自己的複現。

在Pycharm裡面一直跳轉,可以看到PPO類是最終繼承于基類,也就是這個py檔案的内容。

是以閱讀源碼就先從這裡開始。: )

import 包

import warnings
from typing import Any, Dict, Optional, Type, TypeVar, Union

import numpy as np
import torch as th
from gym import spaces
from torch.nn import functional as F

from stable_baselines3.common.on_policy_algorithm import OnPolicyAlgorithm
from stable_baselines3.common.policies import ActorCriticCnnPolicy, ActorCriticPolicy, BasePolicy, MultiInputActorCriticPolicy
from stable_baselines3.common.type_aliases import GymEnv, MaybeCallback, Schedule
from stable_baselines3.common.utils import explained_variance, get_schedule_fn
           

PPO 類

這是面向使用者的淺層的PPO,也就是能直接調用的類

作者在源碼中給出了PPO的論文、引用了誰的源代碼編寫的以及一個引進PPO的講解。

policy

env

learning_rate

三者與基類

base-class.py

的一緻

n_steps

表示每次更新前需要經過的時間步,作者在這裡給出了

n_steps * n_envs

的例子,可能的意思是,如果環境是重複的多個,打算做并行訓練的話,那麼就是每個子環境的時間步乘以環境的數量

batch_size

經驗回放的最小批次資訊

gamma

gae_lambda

clip_range

clip_range_vf

均是具有預設值的參數,分别代表“折扣因子”、“GAE獎勵中平衡偏置和方差的參數”、“為網絡參數而限制幅度的範圍”、“為值函數網絡參數而限制幅度的範圍”

normalize_advantage

标志是否需要歸一化優勢(advantage)

ent_coef

vf_coef

損失計算的熵系數

max_grad_norm

最大的梯度長度,梯度下降的限幅

use_sde

sde_sample_freq

是狀态獨立性探索,隻适用于連續環境,與基類

base-class.py

的一緻

target_kl

限制每次更新時KL散度不能太大,因為clipping限幅不能防止大量更新

剩下的參數與基類

base-class.py

的一緻

class PPO(OnPolicyAlgorithm):
    """
    Proximal Policy Optimization algorithm (PPO) (clip version)

    Paper: https://arxiv.org/abs/1707.06347
    Code: This implementation borrows code from OpenAI Spinning Up (https://github.com/openai/spinningup/)
    https://github.com/ikostrikov/pytorch-a2c-ppo-acktr-gail and
    Stable Baselines (PPO2 from https://github.com/hill-a/stable-baselines)

    Introduction to PPO: https://spinningup.openai.com/en/latest/algorithms/ppo.html

    :param policy: The policy model to use (MlpPolicy, CnnPolicy, ...)
    :param env: The environment to learn from (if registered in Gym, can be str)
    :param learning_rate: The learning rate, it can be a function
        of the current progress remaining (from 1 to 0)
    :param n_steps: The number of steps to run for each environment per update
        (i.e. rollout buffer size is n_steps * n_envs where n_envs is number of environment copies running in parallel)
        NOTE: n_steps * n_envs must be greater than 1 (because of the advantage normalization)
        See https://github.com/pytorch/pytorch/issues/29372
    :param batch_size: Minibatch size
    :param n_epochs: Number of epoch when optimizing the surrogate loss
    :param gamma: Discount factor
    :param gae_lambda: Factor for trade-off of bias vs variance for Generalized Advantage Estimator
    :param clip_range: Clipping parameter, it can be a function of the current progress
        remaining (from 1 to 0).
    :param clip_range_vf: Clipping parameter for the value function,
        it can be a function of the current progress remaining (from 1 to 0).
        This is a parameter specific to the OpenAI implementation. If None is passed (default),
        no clipping will be done on the value function.
        IMPORTANT: this clipping depends on the reward scaling.
    :param normalize_advantage: Whether to normalize or not the advantage
    :param ent_coef: Entropy coefficient for the loss calculation
    :param vf_coef: Value function coefficient for the loss calculation
    :param max_grad_norm: The maximum value for the gradient clipping
    :param use_sde: Whether to use generalized State Dependent Exploration (gSDE)
        instead of action noise exploration (default: False)
    :param sde_sample_freq: Sample a new noise matrix every n steps when using gSDE
        Default: -1 (only sample at the beginning of the rollout)
    :param target_kl: Limit the KL divergence between updates,
        because the clipping is not enough to prevent large update
        see issue #213 (cf https://github.com/hill-a/stable-baselines/issues/213)
        By default, there is no limit on the kl div.
    :param tensorboard_log: the log location for tensorboard (if None, no logging)
    :param policy_kwargs: additional arguments to be passed to the policy on creation
    :param verbose: Verbosity level: 0 for no output, 1 for info messages (such as device or wrappers used), 2 for
        debug messages
    :param seed: Seed for the pseudo random generators
    :param device: Device (cpu, cuda, ...) on which the code should be run.
        Setting it to auto, the code will be run on the GPU if possible.
    :param _init_setup_model: Whether or not to build the network at the creation of the instance
    """

    # PPO政策中限制的可以用字元串的政策就是下面三個
    # 連續環境使用時一般會提示選擇"MultiInputPolicy"
    policy_aliases: Dict[str, Type[BasePolicy]] = {
        "MlpPolicy": ActorCriticPolicy,
        "CnnPolicy": ActorCriticCnnPolicy,
        "MultiInputPolicy": MultiInputActorCriticPolicy,
    }

    # 輸入的一些預設參數表列可以為調參提供參考
    def __init__(
        self,
        policy: Union[str, Type[ActorCriticPolicy]],
        env: Union[GymEnv, str],
        learning_rate: Union[float, Schedule] = 3e-4,
        n_steps: int = 2048,
        batch_size: int = 64,
        n_epochs: int = 10,
        gamma: float = 0.99,
        gae_lambda: float = 0.95,
        clip_range: Union[float, Schedule] = 0.2,
        clip_range_vf: Union[None, float, Schedule] = None,
        normalize_advantage: bool = True,
        ent_coef: float = 0.0,
        vf_coef: float = 0.5,
        max_grad_norm: float = 0.5,
        use_sde: bool = False,
        sde_sample_freq: int = -1,
        target_kl: Optional[float] = None,
        tensorboard_log: Optional[str] = None,
        policy_kwargs: Optional[Dict[str, Any]] = None,
        verbose: int = 0,
        seed: Optional[int] = None,
        device: Union[th.device, str] = "auto",
        _init_setup_model: bool = True,
    ):
        super().__init__(
            policy,
            env,
            learning_rate=learning_rate,
            n_steps=n_steps,
            gamma=gamma,
            gae_lambda=gae_lambda,
            ent_coef=ent_coef,
            vf_coef=vf_coef,
            max_grad_norm=max_grad_norm,
            use_sde=use_sde,
            sde_sample_freq=sde_sample_freq,
            tensorboard_log=tensorboard_log,
            policy_kwargs=policy_kwargs,
            verbose=verbose,
            device=device,
            seed=seed,
            _init_setup_model=False,
            supported_action_spaces=(
                spaces.Box,
                spaces.Discrete,
                spaces.MultiDiscrete,
                spaces.MultiBinary,
            ),
        )

        # 合理性、完整性檢查,如果需要normalize的話,需要保證batch_size參數大于1
        # Sanity check, otherwise it will lead to noisy gradient and NaN
        # because of the advantage normalization
        if normalize_advantage:
            assert (
                batch_size > 1
            ), "`batch_size` must be greater than 1. See https://github.com/DLR-RM/stable-baselines3/issues/440"

        if self.env is not None:
            # Check that `n_steps * n_envs > 1` to avoid NaN
            # when doing advantage normalization
            buffer_size = self.env.num_envs * self.n_steps
            # 如果buffer_size等于1但是又需要normalize_advantage标志時
            # 報錯,輸出目前的需要運作的時間步和目前的環境數量
            assert buffer_size > 1 or (
                not normalize_advantage
            ), f"`n_steps * n_envs` must be greater than 1. Currently n_steps={self.n_steps} and n_envs={self.env.num_envs}"
            # rollouts的池子大小必須與最小池子數量mini-batch一緻,也就是能整除
            # 這樣才能一份一份的導入(我的了解是這樣)
            # Check that the rollout buffer size is a multiple of the mini-batch size
            untruncated_batches = buffer_size // batch_size
            # 不是整除的話,就爆出警告
            if buffer_size % batch_size > 0:
                warnings.warn(
                    f"You have specified a mini-batch size of {batch_size},"
                    f" but because the `RolloutBuffer` is of size `n_steps * n_envs = {buffer_size}`,"
                    f" after every {untruncated_batches} untruncated mini-batches,"
                    f" there will be a truncated mini-batch of size {buffer_size % batch_size}\n"
                    f"We recommend using a `batch_size` that is a factor of `n_steps * n_envs`.\n"
                    f"Info: (n_steps={self.n_steps} and n_envs={self.env.num_envs})"
                )
        self.batch_size = batch_size
        self.n_epochs = n_epochs
        self.clip_range = clip_range
        self.clip_range_vf = clip_range_vf
        self.normalize_advantage = normalize_advantage
        self.target_kl = target_kl

        if _init_setup_model:
            self._setup_model()

    def _setup_model(self) -> None:
        super()._setup_model()

        # Transform (if needed) learning rate and clip range (for PPO) to callable.
        # 将輸入的限制幅度轉變成可以調用的變量
        # Initialize schedules for policy/value clipping
        self.clip_range = get_schedule_fn(self.clip_range)
        # 對self.clip_range_vf參數做資料類型和正值檢查
        if self.clip_range_vf is not None:
            if isinstance(self.clip_range_vf, (float, int)):
                assert self.clip_range_vf > 0, "`clip_range_vf` must be positive, " "pass `None` to deactivate vf clipping"

            self.clip_range_vf = get_schedule_fn(self.clip_range_vf)

    def train(self) -> None:
        """
        Update policy using the currently gathered rollout buffer.
        """
        # 将模型設定成訓練模式,這會影響到batch norm和正則化
        # Switch to train mode (this affects batch norm / dropout)
        self.policy.set_training_mode(True)
        # 更新學習率,如果學習率是與目前進度有關的數值
        # Update optimizer learning rate
        self._update_learning_rate(self.policy.optimizer)
        # 計算限幅參數,輸入的是與目前進度有關的學習率,動态變化包括clip_range和clip_range_vf
        # 也就是政策網絡和價值網絡
        # Compute current clip range
        clip_range = self.clip_range(self._current_progress_remaining)
        # Optional: clip range for the value function
        if self.clip_range_vf is not None:
            clip_range_vf = self.clip_range_vf(self._current_progress_remaining)

        # 初始化各種損失的記錄
        # 熵損失、政策梯度損失、價值損失和限制參數
        entropy_losses = []
        pg_losses, value_losses = [], []
        clip_fractions = []

        # 設定continue_training為True,表示現在處于持續性訓練狀态
        continue_training = True
        # train for n_epochs epochs
        # self.n_epochs是訓練次數
        for epoch in range(self.n_epochs):
            # 記錄近似的KL散度數值
            approx_kl_divs = []
            # Do a complete pass on the rollout buffer
            # 将rollout_buffer池子用batch_size做分割,周遊每一個循環
            for rollout_data in self.rollout_buffer.get(self.batch_size):
                # 取出每一小批的動作資料
                actions = rollout_data.actions
                # 如果是離散動作空間的話,專程浮點型資料并拉直
                if isinstance(self.action_space, spaces.Discrete):
                    # Convert discrete action from float to long
                    actions = rollout_data.actions.long().flatten()

                # 判斷是否使用了狀态獨立性探索,如果使用了狀态獨立性探索
                # 那麼就重置噪聲
                # Re-sample the noise matrix because the log_std has changed
                if self.use_sde:
                    self.policy.reset_noise(self.batch_size)

                # 根據政策、觀測的資料和動作,輸出價值、對數機率以及熵
                values, log_prob, entropy = self.policy.evaluate_actions(rollout_data.observations, actions)
                # 再把價值再做拉直
                values = values.flatten()
                # 對經驗池子的advantages做Normalize
                # Normalize的公式是advantages裡面的數值減去advantages的均值
                # 然後再除以advantages的均值的方差(自己複現的話,也可以調用其他庫的方法)
                # Normalize advantage
                advantages = rollout_data.advantages
                # Normalization does not make sense if mini batchsize == 1, see GH issue #325
                if self.normalize_advantage and len(advantages) > 1:
                    advantages = (advantages - advantages.mean()) / (advantages.std() + 1e-8)

                # 輸出先後動作機率的差異值
                # ratio between old and new policy, should be one at the first iteration
                ratio = th.exp(log_prob - rollout_data.old_log_prob)

                # 政策的損失是優勢數值乘以比率,以及限制幅度的優勢之間的負最小值
                # 就是論文的公式
                # clipped surrogate loss
                policy_loss_1 = advantages * ratio
                policy_loss_2 = advantages * th.clamp(ratio, 1 - clip_range, 1 + clip_range)
                policy_loss = -th.min(policy_loss_1, policy_loss_2).mean()

                # 記錄在剛才初始化的日志記錄器裡面
                # Logging
                pg_losses.append(policy_loss.item())
                clip_fraction = th.mean((th.abs(ratio - 1) > clip_range).float()).item()
                clip_fractions.append(clip_fraction)

                # 如果價值沒有限幅的話,就直接輸出
                # 有限制幅度的話,那麼就是限幅增量th.clamp()+原來的數值
                if self.clip_range_vf is None:
                    # No clipping
                    values_pred = values
                else:
                    # Clip the difference between old and new value
                    # NOTE: this depends on the reward scaling
                    values_pred = rollout_data.old_values + th.clamp(
                        values - rollout_data.old_values, -clip_range_vf, clip_range_vf
                    )
                # 建構損失函數并記錄下來
                # Value loss using the TD(gae_lambda) target
                value_loss = F.mse_loss(rollout_data.returns, values_pred)
                value_losses.append(value_loss.item())

                # 如果沒有熵損失,那麼就直接取得均值的副對數機率,如果有熵損失那麼就是熵損失的均值
                # Entropy loss favor exploration
                if entropy is None:
                    # Approximate entropy when no analytical form
                    entropy_loss = -th.mean(-log_prob)
                else:
                    entropy_loss = -th.mean(entropy)

                entropy_losses.append(entropy_loss.item())

                # 最終的損失就是政策損失和加了系數的熵損失和價值函數損失
                loss = policy_loss + self.ent_coef * entropy_loss + self.vf_coef * value_loss

                # Calculate approximate form of reverse KL Divergence for early stopping
                # see issue #417: https://github.com/DLR-RM/stable-baselines3/issues/417
                # and discussion in PR #419: https://github.com/DLR-RM/stable-baselines3/pull/419
                # and Schulman blog: http://joschu.net/blog/kl-approx.html
                # 計算近似KL散度并記錄在清單中
                with th.no_grad():
                    log_ratio = log_prob - rollout_data.old_log_prob
                    approx_kl_div = th.mean((th.exp(log_ratio) - 1) - log_ratio).cpu().numpy()
                    approx_kl_divs.append(approx_kl_div)

                # 如果target_kl存在且近似KL散度太大了,也就是更新程度太大,就提前結束,并報錯
                if self.target_kl is not None and approx_kl_div > 1.5 * self.target_kl:
                    continue_training = False
                    if self.verbose >= 1:
                        print(f"Early stopping at step {epoch} due to reaching max kl: {approx_kl_div:.2f}")
                    break

                # 對損失做優化
                # Optimization step
                self.policy.optimizer.zero_grad()
                loss.backward()
                # 限制幅度避免較大的更新
                # Clip grad norm
                th.nn.utils.clip_grad_norm_(self.policy.parameters(), self.max_grad_norm)
                self.policy.optimizer.step()

            self._n_updates += 1
            if not continue_training:
                break

        explained_var = explained_variance(self.rollout_buffer.values.flatten(), self.rollout_buffer.returns.flatten())

        # Logs
        self.logger.record("train/entropy_loss", np.mean(entropy_losses))
        self.logger.record("train/policy_gradient_loss", np.mean(pg_losses))
        self.logger.record("train/value_loss", np.mean(value_losses))
        self.logger.record("train/approx_kl", np.mean(approx_kl_divs))
        self.logger.record("train/clip_fraction", np.mean(clip_fractions))
        self.logger.record("train/loss", loss.item())
        self.logger.record("train/explained_variance", explained_var)
        if hasattr(self.policy, "log_std"):
            self.logger.record("train/std", th.exp(self.policy.log_std).mean().item())

        self.logger.record("train/n_updates", self._n_updates, exclude="tensorboard")
        self.logger.record("train/clip_range", clip_range)
        if self.clip_range_vf is not None:
            self.logger.record("train/clip_range_vf", clip_range_vf)

    def learn(
        self: SelfPPO,
        total_timesteps: int,
        callback: MaybeCallback = None,
        log_interval: int = 1,
        tb_log_name: str = "PPO",
        reset_num_timesteps: bool = True,
        progress_bar: bool = False,
    ) -> SelfPPO:
        # 這個函數主要是給使用者做調用,在初始化PPO類之後,将這些參數引進來就可以
        # total_timesteps總的需要的時間步
        return super().learn(
            total_timesteps=total_timesteps,
            callback=callback,
            log_interval=log_interval,
            tb_log_name=tb_log_name,
            reset_num_timesteps=reset_num_timesteps,
            progress_bar=progress_bar,
        )
           

繼續閱讀