code: def __init__(
      self,
      policy,
      ob_space,
      ac_space,
      nenvs,
      total_timesteps,
      nprocs=32,
      nsteps=20,
      ent_coef=0.01,
      vf_coef=0.5,
      vf_fisher_coef=1.0,
      lr=0.25,
      max_grad_norm=0.5,
      kfac_clip=0.001,
      lrschedule="linear",
      is_async=True,
  ):
      self.sess = sess = get_session()
      nbatch = nenvs * nsteps
      with tf.variable_scope("acktr_model", reuse=tf.AUTO_REUSE):
          self.model = step_model = policy(nenvs, 1, sess=sess)
          self.model2 = train_model = policy(
              nenvs * nsteps, nsteps, sess=sess
          )
  
      A = train_model.pdtype.sample_placeholder([None])
      ADV = tf.placeholder(tf.float32, [nbatch])
      R = tf.placeholder(tf.float32, [nbatch])
      PG_LR = tf.placeholder(tf.float32, [])
      VF_LR = tf.placeholder(tf.float32, [])
  
      neglogpac = train_model.pd.neglogp(A)
      self.logits = train_model.pi
  
      ##training loss
      pg_loss = tf.reduce_mean(ADV * neglogpac)
      entropy = tf.reduce_mean(train_model.pd.entropy())
      pg_loss = pg_loss - ent_coef * entropy
      vf_loss = tf.losses.mean_squared_error(
          tf.squeeze(train_model.vf), R
      )
      train_loss = pg_loss + vf_coef * vf_loss
  
      ##Fisher loss construction
      self.pg_fisher = pg_fisher_loss = -tf.reduce_mean(neglogpac)
      sample_net = train_model.vf + tf.random_normal(
          tf.shape(train_model.vf)
      )
      self.vf_fisher = (
          vf_fisher_loss
      ) = -vf_fisher_coef * tf.reduce_mean(
          tf.pow(train_model.vf - tf.stop_gradient(sample_net), 2)
      )
      self.joint_fisher = joint_fisher_loss = (
          pg_fisher_loss + vf_fisher_loss
      )
  
      self.params = params = find_trainable_variables("acktr_model")
  
      self.grads_check = grads = tf.gradients(train_loss, params)
  
      with tf.device("/gpu:0"):
          self.optim = optim = kfac.KfacOptimizer(
              learning_rate=PG_LR,
              clip_kl=kfac_clip,
              momentum=0.9,
              kfac_update=1,
              epsilon=0.01,
              stats_decay=0.99,
              is_async=is_async,
              cold_iter=10,
              max_grad_norm=max_grad_norm,
          )
  
          # update_stats_op = optim.compute_and_apply_stats(joint_fisher_loss, var_list=params)
          optim.compute_and_apply_stats(
              joint_fisher_loss, var_list=params
          )
          train_op, q_runner = optim.apply_gradients(
              list(zip(grads, params))
          )
      self.q_runner = q_runner
      self.lr = Scheduler(
          v=lr, nvalues=total_timesteps, schedule=lrschedule
      )
  
      def train(obs, states, rewards, masks, actions, values):
          advs = rewards - values
          for step in range(len(obs)):
              cur_lr = self.lr.value()
  
          td_map = {
              train_model.X: obs,
              A: actions,
              ADV: advs,
              R: rewards,
              PG_LR: cur_lr,
              VF_LR: cur_lr,
          }
          if states is not None:
              td_map[train_model.S] = states
              td_map[train_model.M] = masks
  
          policy_loss, value_loss, policy_entropy, _ = sess.run(
              [pg_loss, vf_loss, entropy, train_op], td_map
          )
          return policy_loss, value_loss, policy_entropy
  
      self.train = train
      self.save = functools.partial(save_variables, sess=sess)
      self.load = functools.partial(load_variables, sess=sess)
      self.train_model = train_model
      self.step_model = step_model
      self.step = step_model.step
      self.value = step_model.value
      self.initial_state = step_model.initial_state
      tf.global_variables_initializer().run(session=sess)
  
file_path: baselines/baselines/acktr/acktr.py
imports: import os.path
  import time
  import functools
  import tensorflow
  from baselines import logger
  from baselines.common import set_global_seeds, explained_variance
  from baselines.common.policies import build_policy
  from baselines.common.tf_util import get_session, save_variables, load_variables
  from baselines.a2c.runner import Runner
  from baselines.a2c.utils import Scheduler, find_trainable_variables
  from baselines.acktr import kfac
  from baselines.ppo2.ppo2 import safemean
  from collections import deque
label_desc: Use image node_id 1 to create a new Model object from inherited base classes: object with example: obj = Model(policy, ob_space, ac_space, nenvs, total_timesteps, nprocs, nsteps, ent_coef, vf_coef, vf_fisher_coef, lr, max_grad_norm, kfac_clip, lrschedule, is_async)