docstr: Reward regression from observations and transitions
  
  :param observation_space: (gym.spaces)
  :param action_space: (gym.spaces)
  :param hidden_size: ([int]) the hidden dimension for the MLP
  :param entcoeff: (float) the entropy loss weight
  :param scope: (str) tensorflow variable scope
  :param normalize: (bool) Whether to normalize the reward or not
docstr_tok: ["Reward","regression","from","observations","and","transitions",":","param","observation_space",":","(","gym.spaces",")",":","param","action_space",":","(","gym.spaces",")",":","param","hidden_size",":","(","[","int","]",")","the","hidden","dimension","for","the","MLP",":","param","entcoeff",":","(","float",")","the","entropy","loss","weight",":","param","scope",":","(","str",")","tensorflow","variable","scope",":","param","normalize",":","(","bool",")","Whether","to","normalize","the","reward","or","not"]
code: def __init__(
      self,
      observation_space,
      action_space,
      hidden_size,
      entcoeff=0.001,
      scope="adversary",
      normalize=True,
  ):
      """
      Reward regression from observations and transitions
  
      :param observation_space: (gym.spaces)
      :param action_space: (gym.spaces)
      :param hidden_size: ([int]) the hidden dimension for the MLP
      :param entcoeff: (float) the entropy loss weight
      :param scope: (str) tensorflow variable scope
      :param normalize: (bool) Whether to normalize the reward or not
      """
      # TODO: support images properly (using a CNN)
      self.scope = scope
      self.observation_shape = observation_space.shape
      self.actions_shape = action_space.shape
  
      if isinstance(action_space, gym.spaces.Box):
          # Continuous action space
          self.discrete_actions = False
          self.n_actions = action_space.shape[0]
      elif isinstance(action_space, gym.spaces.Discrete):
          self.n_actions = action_space.n
          self.discrete_actions = True
      else:
          raise ValueError(
              "Action space not supported: {}".format(action_space)
          )
  
      self.hidden_size = hidden_size
      self.normalize = normalize
      self.obs_rms = None
  
      # Placeholders
      self.generator_obs_ph = tf.placeholder(
          observation_space.dtype,
          (None,) + self.observation_shape,
          name="observations_ph",
      )
      self.generator_acs_ph = tf.placeholder(
          action_space.dtype,
          (None,) + self.actions_shape,
          name="actions_ph",
      )
      self.expert_obs_ph = tf.placeholder(
          observation_space.dtype,
          (None,) + self.observation_shape,
          name="expert_observations_ph",
      )
      self.expert_acs_ph = tf.placeholder(
          action_space.dtype,
          (None,) + self.actions_shape,
          name="expert_actions_ph",
      )
      # Build graph
      generator_logits = self.build_graph(
          self.generator_obs_ph, self.generator_acs_ph, reuse=False
      )
      expert_logits = self.build_graph(
          self.expert_obs_ph, self.expert_acs_ph, reuse=True
      )
      # Build accuracy
      generator_acc = tf.reduce_mean(
          tf.cast(tf.nn.sigmoid(generator_logits) < 0.5, tf.float32)
      )
      expert_acc = tf.reduce_mean(
          tf.cast(tf.nn.sigmoid(expert_logits) > 0.5, tf.float32)
      )
      # Build regression loss
      # let x = logits, z = targets.
      # z * -log(sigmoid(x)) + (1 - z) * -log(1 - sigmoid(x))
      generator_loss = tf.nn.sigmoid_cross_entropy_with_logits(
          logits=generator_logits,
          labels=tf.zeros_like(generator_logits),
      )
      generator_loss = tf.reduce_mean(generator_loss)
      expert_loss = tf.nn.sigmoid_cross_entropy_with_logits(
          logits=expert_logits, labels=tf.ones_like(expert_logits)
      )
      expert_loss = tf.reduce_mean(expert_loss)
      # Build entropy loss
      logits = tf.concat([generator_logits, expert_logits], 0)
      entropy = tf.reduce_mean(logit_bernoulli_entropy(logits))
      entropy_loss = -entcoeff * entropy
      # Loss + Accuracy terms
      self.losses = [
          generator_loss,
          expert_loss,
          entropy,
          entropy_loss,
          generator_acc,
          expert_acc,
      ]
      self.loss_name = [
          "generator_loss",
          "expert_loss",
          "entropy",
          "entropy_loss",
          "generator_acc",
          "expert_acc",
      ]
      self.total_loss = generator_loss + expert_loss + entropy_loss
      # Build Reward for policy
      self.reward_op = -tf.log(
          1 - tf.nn.sigmoid(generator_logits) + 1e-8
      )
      var_list = self.get_trainable_variables()
      self.lossandgrad = tf_util.function(
          [
              self.generator_obs_ph,
              self.generator_acs_ph,
              self.expert_obs_ph,
              self.expert_acs_ph,
          ],
          self.losses + [tf_util.flatgrad(self.total_loss, var_list)],
      )
  
file_path: stable-baselines/stable_baselines/gail/adversary.py
imports: import gym
  import tensorflow
  import numpy
  from stable_baselines.common.mpi_running_mean_std import RunningMeanStd
  from stable_baselines.common import tf_util
label_desc: Use image node_id 1 to create a new TransitionClassifier object from inherited base classes: object with example: obj = TransitionClassifier(observation_space, action_space, hidden_size, entcoeff, scope, normalize)