class_docstr: Ranking agent class.
docstr: Computes loss for training the reward and constraint networks.
  
  Args:
    experience: A batch of experience data in the form of a `Trajectory` or
      `Transition`.
    weights: Optional scalar or elementwise (per-batch-entry) importance
      weights.  The output batch loss will be scaled by these weights, and the
      final scalar loss is the mean of these values.
    training: Whether the loss is being used for training.
  
  Returns:
    A `LossInfo` containing the loss for the training step.
  
  Raises:
    ValueError:
      if the number of actions is greater than 1.
docstr_tok: ["Computes","loss","for","training","the","reward","and","constraint","networks",".","Args",":","experience",":","A","batch","of","experience","data","in","the","form","of","a","`","Trajectory","`","or","`","Transition","`",".","weights",":","Optional","scalar","or","elementwise","(","per-batch-entry",")","importance","weights",".","The","output","batch","loss","will","be","scaled","by","these","weights",",","and","the","final","scalar","loss","is","the","mean","of","these","values",".","training",":","Whether","the","loss","is","being","used","for","training",".","Returns",":","A","`","LossInfo","`","containing","the","loss","for","the","training","step",".","Raises",":","ValueError",":","if","the","number","of","actions","is","greater","than","1","."]
code: def _loss(
      self,
      experience: types.NestedTensor,
      weights: Optional[types.Tensor] = None,
      training: bool = False,
  ) -> tf_agent.LossInfo:
      """Computes loss for training the reward and constraint networks.
  
      Args:
        experience: A batch of experience data in the form of a `Trajectory` or
          `Transition`.
        weights: Optional scalar or elementwise (per-batch-entry) importance
          weights.  The output batch loss will be scaled by these weights, and the
          final scalar loss is the mean of these values.
        training: Whether the loss is being used for training.
  
      Returns:
        A `LossInfo` containing the loss for the training step.
  
      Raises:
        ValueError:
          if the number of actions is greater than 1.
      """
      flat_obs, _ = nest_utils.flatten_multi_batched_nested_tensors(
          experience.observation, self._training_data_spec.observation
      )
      flat_reward, _ = nest_utils.flatten_multi_batched_nested_tensors(
          experience.reward, self._training_data_spec.reward
      )
      if self._feedback_model == FeedbackModel.CASCADING:
          chosen_index = flat_reward[CHOSEN_INDEX]
          chosen_value = flat_reward[CHOSEN_VALUE]
          score = compute_score_tensor_for_cascading(
              chosen_index,
              chosen_value,
              self._num_slots,
              non_click_score=self._non_click_score,
          )
      elif self._feedback_model == FeedbackModel.SCORE_VECTOR:
          score = flat_reward
      weights = self._construct_sample_weights(
          flat_reward, flat_obs, weights
      )
      if self._positional_bias_positive_only:
          weights = tf.where(score > 0, weights, 1.0)
  
      est_reward = self._scoring_network(flat_obs, training)[0]
      loss_output = self._error_loss_fn(
          score,
          est_reward,
          reduction=tf.compat.v1.losses.Reduction.NONE,
      )
      if len(list(loss_output.shape)) == 1:
          # In case the loss is an aggregate over all slots, we only use one weight
          # per iteration.
          weights = weights[:, 0]
      return tf_agent.LossInfo(
          tf.reduce_sum(tf.multiply(loss_output, weights))
          / tf.reduce_sum(weights),
          extra=(),
      )
  
imports: import enum
  from typing import Optional, Text
  import tensorflow
  from tf_agents.agents import tf_agent
  from tf_agents.bandits.policies import ranking_policy
  from tf_agents.specs import bandit_spec_utils
  from tf_agents.trajectories import policy_step
  from tf_agents.trajectories import time_step
  from tf_agents.trajectories import trajectory
  from tf_agents.typing import types
  from tf_agents.utils import common
  from tf_agents.utils import eager_utils
  from tf_agents.utils import nest_utils
label_desc: Use image node_id 3 for calling the RankingAgent obj's underlying member method code with example usage: obj._loss(experience, weights, training) and returns: tf_agent