docstr: Calculate KL divergence between old and new distributions
      See: https://en.wikipedia.org/wiki/Multivariate_normal_distribution#Kullback.E2.80.93Leibler_divergence
  
  Args:
      means: shape (batch_size, act_dim)
      logvars: shape (act_dim)
      old_means: shape (batch_size, act_dim)
      old_logvars: shape (act_dim)
  
  Returns:
      kl: shape (batch_size)
docstr_tok: ["Calculate","KL","divergence","between","old","and","new","distributions","See",":","https",":","\/\/en.wikipedia.org\/wiki\/Multivariate_normal_distribution","#","Kullback.E2.80.93Leibler_divergence","Args",":","means",":","shape","(","batch_size",",","act_dim",")","logvars",":","shape","(","act_dim",")","old_means",":","shape","(","batch_size",",","act_dim",")","old_logvars",":","shape","(","act_dim",")","Returns",":","kl",":","shape","(","batch_size",")"]
code: def _calc_kl(self, means, logvars, old_means, old_logvars):
      """Calculate KL divergence between old and new distributions
          See: https://en.wikipedia.org/wiki/Multivariate_normal_distribution#Kullback.E2.80.93Leibler_divergence
  
      Args:
          means: shape (batch_size, act_dim)
          logvars: shape (act_dim)
          old_means: shape (batch_size, act_dim)
          old_logvars: shape (act_dim)
  
      Returns:
          kl: shape (batch_size)
      """
      log_det_cov_old = layers.reduce_sum(old_logvars)
      log_det_cov_new = layers.reduce_sum(logvars)
      tr_old_new = layers.reduce_sum(layers.exp(old_logvars - logvars))
      kl = 0.5 * (
          layers.reduce_sum(
              layers.square(means - old_means) / layers.exp(logvars),
              dim=1,
          )
          + (log_det_cov_new - log_det_cov_old)
          + tr_old_new
          - self.act_dim
      )
      return kl
  
file_path: PARL/parl/algorithms/fluid/ppo.py
imports: import warnings
  import numpy
  from copy import deepcopy
  from paddle import fluid
  from parl.core.fluid import layers
  from parl.core.fluid.algorithm import Algorithm
label_desc: Use image node_id 3 for calling the PPO obj's underlying member method code with example usage: obj._calc_kl(means, logvars, old_means, old_logvars) and returns: kl