class_docstr: Proximal Policy Optimization algorithm (GPU version).
  Paper: https://arxiv.org/abs/1707.06347
  
  :param policy: (ActorCriticPolicy or str) The policy model to use (MlpPolicy, CnnPolicy, CnnLstmPolicy, ...)
  :param env: (Gym environment or str) The environment to learn from (if registered in Gym, can be str)
  :param gamma: (float) Discount factor
  :param n_steps: (int) The number of steps to run for each environment per update
      (i.e. batch size is n_steps * n_env where n_env is number of environment copies running in parallel)
  :param ent_coef: (float) Entropy coefficient for the loss calculation
  :param learning_rate: (float or callable) The learning rate, it can be a function
  :param vf_coef: (float) Value function coefficient for the loss calculation
  :param max_grad_norm: (float) The maximum value for the gradient clipping
  :param lam: (float) Factor for trade-off of bias vs variance for Generalized Advantage Estimator
  :param nminibatches: (int) Number of training minibatches per update. For recurrent policies,
      the number of environments run in parallel should be a multiple of nminibatches.
  :param noptepochs: (int) Number of epoch when optimizing the surrogate
  :param cliprange: (float or callable) Clipping parameter, it can be a function
  :param cliprange_vf: (float or callable) Clipping parameter for the value function, it can be a function.
      This is a parameter specific to the OpenAI implementation. If None is passed (default),
      then `cliprange` (that is used for the policy) will be used.
      IMPORTANT: this clipping depends on the reward scaling.
      To deactivate value function clipping (and recover the original PPO implementation),
      you have to pass a negative value (e.g. -1).
  :param verbose: (int) the verbosity level: 0 none, 1 training information, 2 tensorflow debug
  :param tensorboard_log: (str) the log location for tensorboard (if None, no logging)
  :param _init_setup_model: (bool) Whether or not to build the network at the creation of the instance
  :param policy_kwargs: (dict) additional arguments to be passed to the policy on creation
  :param full_tensorboard_log: (bool) enable additional logging when using tensorboard
      WARNING: this logging can take a lot of space quickly
  :param seed: (int) Seed for the pseudo-random generators (python, numpy, tensorflow).
      If None (default), use random seed. Note that if you want completely deterministic
      results, you must set `n_cpu_tf_sess` to 1.
  :param n_cpu_tf_sess: (int) The number of threads for TensorFlow operations
      If None, the number of cpu of the current machine will be used.
class_docstr_tok: ["Proximal","Policy","Optimization","algorithm","(","GPU","version",")",".","Paper",":","https",":","\/\/arxiv.org\/abs\/1707.06347",":","param","policy",":","(","ActorCriticPolicy","or","str",")","The","policy","model","to","use","(","MlpPolicy",",","CnnPolicy",",","CnnLstmPolicy",",","...",")",":","param","env",":","(","Gym","environment","or","str",")","The","environment","to","learn","from","(","if","registered","in","Gym",",","can","be","str",")",":","param","gamma",":","(","float",")","Discount","factor",":","param","n_steps",":","(","int",")","The","number","of","steps","to","run","for","each","environment","per","update","(","i.e",".","batch","size","is","n_steps","*","n_env","where","n_env","is","number","of","environment","copies","running","in","parallel",")",":","param","ent_coef",":","(","float",")","Entropy","coefficient","for","the","loss","calculation",":","param","learning_rate",":","(","float","or","callable",")","The","learning","rate",",","it","can","be","a","function",":","param","vf_coef",":","(","float",")","Value","function","coefficient","for","the","loss","calculation",":","param","max_grad_norm",":","(","float",")","The","maximum","value","for","the","gradient","clipping",":","param","lam",":","(","float",")","Factor","for","trade-off","of","bias","vs","variance","for","Generalized","Advantage","Estimator",":","param","nminibatches",":","(","int",")","Number","of","training","minibatches","per","update",".","For","recurrent","policies",",","the","number","of","environments","run","in","parallel","should","be","a","multiple","of","nminibatches",".",":","param","noptepochs",":","(","int",")","Number","of","epoch","when","optimizing","the","surrogate",":","param","cliprange",":","(","float","or","callable",")","Clipping","parameter",",","it","can","be","a","function",":","param","cliprange_vf",":","(","float","or","callable",")","Clipping","parameter","for","the","value","function",",","it","can","be","a","function",".","This","is","a","parameter","specific","to","the","OpenAI","implementation",".","If","None","is","passed","(","default",")",",","then","`","cliprange","`","(","that","is","used","for","the","policy",")","will","be","used",".","IMPORTANT",":","this","clipping","depends","on","the","reward","scaling",".","To","deactivate","value","function","clipping","(","and","recover","the","original","PPO","implementation",")",",","you","have","to","pass","a","negative","value","(","e.g",".","-1",")",".",":","param","verbose",":","(","int",")","the","verbosity","level",":","0","none",",","1","training","information",",","2","tensorflow","debug",":","param","tensorboard_log",":","(","str",")","the","log","location","for","tensorboard","(","if","None",",","no","logging",")",":","param","_init_setup_model",":","(","bool",")","Whether","or","not","to","build","the","network","at","the","creation","of","the","instance",":","param","policy_kwargs",":","(","dict",")","additional","arguments","to","be","passed","to","the","policy","on","creation",":","param","full_tensorboard_log",":","(","bool",")","enable","additional","logging","when","using","tensorboard","WARNING",":","this","logging","can","take","a","lot","of","space","quickly",":","param","seed",":","(","int",")","Seed","for","the","pseudo-random","generators","(","python",",","numpy",",","tensorflow",")",".","If","None","(","default",")",",","use","random","seed",".","Note","that","if","you","want","completely","deterministic","results",",","you","must","set","`","n_cpu_tf_sess","`","to","1.",":","param","n_cpu_tf_sess",":","(","int",")","The","number","of","threads","for","TensorFlow","operations","If","None",",","the","number","of","cpu","of","the","current","machine","will","be","used","."]
code: def learn(
      self,
      total_timesteps,
      callback=None,
      log_interval=1,
      tb_log_name="PPO2",
      reset_num_timesteps=True,
  ):
      # Transform to callable if needed
      self.learning_rate = get_schedule_fn(self.learning_rate)
      self.cliprange = get_schedule_fn(self.cliprange)
      cliprange_vf = get_schedule_fn(self.cliprange_vf)
  
      new_tb_log = self._init_num_timesteps(reset_num_timesteps)
      callback = self._init_callback(callback)
  
      with SetVerbosity(self.verbose), TensorboardWriter(
          self.graph, self.tensorboard_log, tb_log_name, new_tb_log
      ) as writer:
          self._setup_learn()
  
          t_first_start = time.time()
          n_updates = total_timesteps // self.n_batch
  
          callback.on_training_start(locals(), globals())
  
          for update in range(1, n_updates + 1):
              assert self.n_batch % self.nminibatches == 0, (
                  "The number of minibatches (`nminibatches`) "
                  "is not a factor of the total number of samples "
                  "collected per rollout (`n_batch`), "
                  "some samples won't be used."
              )
              batch_size = self.n_batch // self.nminibatches
              t_start = time.time()
              frac = 1.0 - (update - 1.0) / n_updates
              lr_now = self.learning_rate(frac)
              cliprange_now = self.cliprange(frac)
              cliprange_vf_now = cliprange_vf(frac)
  
              callback.on_rollout_start()
              # true_reward is the reward without discount
              rollout = self.runner.run(callback)
              # Unpack
              (
                  obs,
                  returns,
                  masks,
                  actions,
                  values,
                  neglogpacs,
                  states,
                  ep_infos,
                  true_reward,
              ) = rollout
  
              callback.on_rollout_end()
  
              # Early stopping due to the callback
              if not self.runner.continue_training:
                  break
  
              self.ep_info_buf.extend(ep_infos)
              mb_loss_vals = []
              if states is None:  # nonrecurrent version
                  update_fac = max(
                      self.n_batch
                      // self.nminibatches
                      // self.noptepochs,
                      1,
                  )
                  inds = np.arange(self.n_batch)
                  for epoch_num in range(self.noptepochs):
                      np.random.shuffle(inds)
                      for start in range(0, self.n_batch, batch_size):
                          timestep = (
                              self.num_timesteps // update_fac
                              + (
                                  (epoch_num * self.n_batch + start)
                                  // batch_size
                              )
                          )
                          end = start + batch_size
                          mbinds = inds[start:end]
                          slices = (
                              arr[mbinds]
                              for arr in (
                                  obs,
                                  returns,
                                  masks,
                                  actions,
                                  values,
                                  neglogpacs,
                              )
                          )
                          mb_loss_vals.append(
                              self._train_step(
                                  lr_now,
                                  cliprange_now,
                                  *slices,
                                  writer=writer,
                                  update=timestep,
                                  cliprange_vf=cliprange_vf_now,
                              )
                          )
              else:  # recurrent version
                  update_fac = max(
                      self.n_batch
                      // self.nminibatches
                      // self.noptepochs
                      // self.n_steps,
                      1,
                  )
                  assert self.n_envs % self.nminibatches == 0
                  env_indices = np.arange(self.n_envs)
                  flat_indices = np.arange(
                      self.n_envs * self.n_steps
                  ).reshape(self.n_envs, self.n_steps)
                  envs_per_batch = batch_size // self.n_steps
                  for epoch_num in range(self.noptepochs):
                      np.random.shuffle(env_indices)
                      for start in range(
                          0, self.n_envs, envs_per_batch
                      ):
                          timestep = (
                              self.num_timesteps // update_fac
                              + (
                                  (epoch_num * self.n_envs + start)
                                  // envs_per_batch
                              )
                          )
                          end = start + envs_per_batch
                          mb_env_inds = env_indices[start:end]
                          mb_flat_inds = flat_indices[
                              mb_env_inds
                          ].ravel()
                          slices = (
                              arr[mb_flat_inds]
                              for arr in (
                                  obs,
                                  returns,
                                  masks,
                                  actions,
                                  values,
                                  neglogpacs,
                              )
                          )
                          mb_states = states[mb_env_inds]
                          mb_loss_vals.append(
                              self._train_step(
                                  lr_now,
                                  cliprange_now,
                                  *slices,
                                  update=timestep,
                                  writer=writer,
                                  states=mb_states,
                                  cliprange_vf=cliprange_vf_now,
                              )
                          )
  
              loss_vals = np.mean(mb_loss_vals, axis=0)
              t_now = time.time()
              fps = int(self.n_batch / (t_now - t_start))
  
              if writer is not None:
                  total_episode_reward_logger(
                      self.episode_reward,
                      true_reward.reshape((self.n_envs, self.n_steps)),
                      masks.reshape((self.n_envs, self.n_steps)),
                      writer,
                      self.num_timesteps,
                  )
  
              if self.verbose >= 1 and (
                  update % log_interval == 0 or update == 1
              ):
                  explained_var = explained_variance(values, returns)
                  logger.logkv(
                      "serial_timesteps", update * self.n_steps
                  )
                  logger.logkv("n_updates", update)
                  logger.logkv("total_timesteps", self.num_timesteps)
                  logger.logkv("fps", fps)
                  logger.logkv(
                      "explained_variance", float(explained_var)
                  )
                  if (
                      len(self.ep_info_buf) > 0
                      and len(self.ep_info_buf[0]) > 0
                  ):
                      logger.logkv(
                          "ep_reward_mean",
                          safe_mean(
                              [
                                  ep_info["r"]
                                  for ep_info in self.ep_info_buf
                              ]
                          ),
                      )
                      logger.logkv(
                          "ep_len_mean",
                          safe_mean(
                              [
                                  ep_info["l"]
                                  for ep_info in self.ep_info_buf
                              ]
                          ),
                      )
                  logger.logkv("time_elapsed", t_start - t_first_start)
                  for loss_val, loss_name in zip(
                      loss_vals, self.loss_names
                  ):
                      logger.logkv(loss_name, loss_val)
                  logger.dumpkvs()
  
          callback.on_training_end()
          return self
  
imports: import time
  import gym
  import numpy
  import tensorflow
  from stable_baselines import logger
  from stable_baselines.common import explained_variance, ActorCriticRLModel, tf_util, SetVerbosity, TensorboardWriter
  from stable_baselines.common.runners import AbstractEnvRunner
  from stable_baselines.common.policies import ActorCriticPolicy, RecurrentActorCriticPolicy
  from stable_baselines.common.schedules import get_schedule_fn
  from stable_baselines.common.tf_util import total_episode_reward_logger
  from stable_baselines.common.math_util import safe_mean
label_desc: Use image node_id 7 for calling the PPO2 obj's underlying member method code with example usage: obj.learn(total_timesteps, callback, log_interval, tb_log_name, reset_num_timesteps) and returns: self