class_docstr: PyTorch version of the CustomLossModel above.
class_docstr_tok: ["PyTorch","version","of","the","CustomLossModel","above","."]
docstr: Calculates a custom loss on top of the given policy_loss(es).
  
  Args:
      policy_loss (List[TensorType]): The list of already calculated
          policy losses (as many as there are optimizers).
      loss_inputs: Struct of np.ndarrays holding the
          entire train batch.
  
  Returns:
      List[TensorType]: The altered list of policy losses. In case the
          custom loss should have its own optimizer, make sure the
          returned list is one larger than the incoming policy_loss list.
          In case you simply want to mix in the custom loss into the
          already calculated policy losses, return a list of altered
          policy losses (as done in this example below).
docstr_tok: ["Calculates","a","custom","loss","on","top","of","the","given","policy_loss","(","es",")",".","Args",":","policy_loss","(","List","[","TensorType","]",")",":","The","list","of","already","calculated","policy","losses","(","as","many","as","there","are","optimizers",")",".","loss_inputs",":","Struct","of","np.ndarrays","holding","the","entire","train","batch",".","Returns",":","List","[","TensorType","]",":","The","altered","list","of","policy","losses",".","In","case","the","custom","loss","should","have","its","own","optimizer",",","make","sure","the","returned","list","is","one","larger","than","the","incoming","policy_loss","list",".","In","case","you","simply","want","to","mix","in","the","custom","loss","into","the","already","calculated","policy","losses",",","return","a","list","of","altered","policy","losses","(","as","done","in","this","example","below",")","."]
code: def custom_loss(self, policy_loss, loss_inputs):
      """Calculates a custom loss on top of the given policy_loss(es).
  
      Args:
          policy_loss (List[TensorType]): The list of already calculated
              policy losses (as many as there are optimizers).
          loss_inputs: Struct of np.ndarrays holding the
              entire train batch.
  
      Returns:
          List[TensorType]: The altered list of policy losses. In case the
              custom loss should have its own optimizer, make sure the
              returned list is one larger than the incoming policy_loss list.
              In case you simply want to mix in the custom loss into the
              already calculated policy losses, return a list of altered
              policy losses (as done in this example below).
      """
      # Get the next batch from our input files.
      batch = self.reader.next()
  
      # Define a secondary loss by building a graph copy with weight sharing.
      obs = restore_original_dimensions(
          torch.from_numpy(batch["obs"])
          .float()
          .to(policy_loss[0].device),
          self.obs_space,
          tensorlib="torch",
      )
      logits, _ = self.forward({"obs": obs}, [], None)
  
      # Compute the IL loss.
      action_dist = TorchCategorical(logits, self.model_config)
      imitation_loss = torch.mean(
          -action_dist.logp(
              torch.from_numpy(batch["actions"]).to(
                  policy_loss[0].device
              )
          )
      )
      self.imitation_loss_metric = imitation_loss.item()
      self.policy_loss_metric = np.mean(
          [loss.item() for loss in policy_loss]
      )
  
      # Add the imitation loss to each already calculated policy loss term.
      # Alternatively (if custom loss has its own optimizer):
      # return policy_loss + [10 * self.imitation_loss]
      return [loss_ + 10 * imitation_loss for loss_ in policy_loss]
  
imports: import numpy
  from ray.rllib.models.modelv2 import ModelV2, restore_original_dimensions
  from ray.rllib.models.tf.tf_action_dist import Categorical
  from ray.rllib.models.tf.tf_modelv2 import TFModelV2
  from ray.rllib.models.tf.fcnet import FullyConnectedNetwork
  from ray.rllib.models.torch.torch_action_dist import TorchCategorical
  from ray.rllib.models.torch.torch_modelv2 import TorchModelV2
  from ray.rllib.models.torch.fcnet import FullyConnectedNetwork
  from ray.rllib.utils.annotations import override
  from ray.rllib.utils.framework import try_import_tf, try_import_torch
  from ray.rllib.offline import JsonReader
label_desc: Use image node_id 4 for calling the TorchCustomLossModel obj's underlying member method code with example usage: obj.custom_loss(policy_loss, loss_inputs) and returns: unknown