class_docstr: PyTorch policy class used with APPO.
docstr: Stats function for APPO. Returns a dict with important loss stats.
  
  Args:
      policy: The Policy to generate stats for.
      train_batch: The SampleBatch (already) used for training.
  
  Returns:
      Dict[str, TensorType]: The stats dict.
code: def stats_fn(self, train_batch: SampleBatch) -> Dict[str, TensorType]:
      """Stats function for APPO. Returns a dict with important loss stats.
  
      Args:
          policy: The Policy to generate stats for.
          train_batch: The SampleBatch (already) used for training.
  
      Returns:
          Dict[str, TensorType]: The stats dict.
      """
      stats_dict = {
          "cur_lr": self.cur_lr,
          "total_loss": torch.mean(
              torch.stack(self.get_tower_stats("total_loss"))
          ),
          "policy_loss": torch.mean(
              torch.stack(self.get_tower_stats("mean_policy_loss"))
          ),
          "entropy": torch.mean(
              torch.stack(self.get_tower_stats("mean_entropy"))
          ),
          "entropy_coeff": self.entropy_coeff,
          "var_gnorm": global_norm(self.model.trainable_variables()),
          "vf_loss": torch.mean(
              torch.stack(self.get_tower_stats("mean_vf_loss"))
          ),
          "vf_explained_var": torch.mean(
              torch.stack(self.get_tower_stats("vf_explained_var"))
          ),
      }
  
      if self.config["vtrace"]:
          is_stat_mean = torch.mean(self._is_ratio, [0, 1])
          is_stat_var = torch.var(self._is_ratio, [0, 1])
          stats_dict["mean_IS"] = is_stat_mean
          stats_dict["var_IS"] = is_stat_var
  
      if self.config["use_kl_loss"]:
          stats_dict["kl"] = torch.mean(
              torch.stack(self.get_tower_stats("mean_kl_loss"))
          )
          stats_dict["KL_Coeff"] = self.kl_coeff
  
      return convert_to_numpy(stats_dict)
  
imports: import gymnasium
  import numpy
  import logging
  from typing import Any, Dict, List, Optional, Type, Union
  import ray
  from ray.rllib.algorithms.appo.utils import make_appo_models
  import ray.rllib.algorithms.impala.vtrace_torch
  from ray.rllib.algorithms.impala.impala_torch_policy import make_time_major, VTraceOptimizer
  from ray.rllib.evaluation.episode import Episode
  from ray.rllib.evaluation.postprocessing import compute_bootstrap_value, compute_gae_for_sample_batch, Postprocessing
  from ray.rllib.models.action_dist import ActionDistribution
  from ray.rllib.models.modelv2 import ModelV2
  from ray.rllib.models.torch.torch_action_dist import TorchDistributionWrapper, TorchCategorical
  from ray.rllib.models.torch.torch_modelv2 import TorchModelV2
  from ray.rllib.policy.sample_batch import SampleBatch
  from ray.rllib.policy.torch_mixins import EntropyCoeffSchedule, LearningRateSchedule, KLCoeffMixin, ValueNetworkMixin, TargetNetworkMixin
  from ray.rllib.policy.torch_policy_v2 import TorchPolicyV2
  from ray.rllib.utils.annotations import override
  from ray.rllib.utils.framework import try_import_torch
  from ray.rllib.utils.numpy import convert_to_numpy
  from ray.rllib.utils.torch_utils import apply_grad_clipping, explained_variance, global_norm, sequence_mask
  from ray.rllib.utils.typing import TensorType
label_desc: Use image node_id 5 for calling the APPOTorchPolicy obj's underlying member method code with example usage: obj.stats_fn(train_batch) and returns: convert_to_numpy